Menu : déplacement dans celui du module semantics
[OpenERP/todolist.git] / todolist.py
1 #-*- coding: utf8 -*-
2
3 from openerp.osv import osv, fields
4
5
6
7
8 class Container(osv.Model):
9     """TODO List : Tasks container"""
10
11     def _get_nb_tasks(self, cr, uid, ids, field, arg, context=None):
12         result = {}
13         for container in self.browse(cr, uid, ids, context=context):
14             result[container.id] = len(container.tasks)
15         return result
16 #       OR : return dict((c.id, len(c.tasks)) for c in self.browse(cr, uid, ids, context=context))
17
18
19     def _get_nb_tasks_done(self, cr, uid, ids, field, arg, context=None):
20         result={}
21         for c in self.browse(cr, uid, ids, context=context):
22             result[c.id] = len([t for t in c.tasks if t.state == "done"])
23         return result
24
25
26     def _tasks_progress(self, cr, uid, ids, field, arg, context=None):
27         result = {}
28         for c in self.browse(cr, uid, ids, context=context):
29             result[c.id] = c.number_tasks and c.number_tasks_done*100./c.number_tasks or 0.
30         return result
31
32
33     #================================================================================
34     # def copy(self, cr, uid, id, default, context=None):
35     #    container = self.browse(cr, uid, id, context=context)
36     #    new_name =  "Copy of %s" % container.name
37     #    # =like is the original LIKE operator from SQL
38     #    others_count = self.search(cr,  uid, [('name', '=like', new_name+'%')],
39     #                               count=True, context=context)
40     #    if others_count > 0:
41     #        new_name = "%s (%s)" % (new_name, others_count+1)
42     #    default['name'] = new_name
43     #    return osv.Model.copy(self, cr, uid, id, default, context=context)
44     #================================================================================
45
46
47     def copy(self, cr, uid, id, default, context=None):
48         cr.execute("SELECT copierContainer (%s);", (id,))
49         return cr.fetchone()[0]
50
51
52     def _get_manday(self, cr, uid, ids, field, arg, context=None):
53         result={}
54         for container in self.browse(cr, uid, ids, context=context):
55             result[container.id] = sum([t.manday for t in container.tasks if t.state != "done"])
56         return result
57
58
59     _name = "todolist.container"
60
61     _inherit = "mail.thread"
62
63     _status = [("draft", "Draft"), ("pending", "Pending"), ("done", "Done")]
64
65     _columns = {
66         "name": fields.char(string="Title", size=64, required=True),
67         "description": fields.text(string="Description"),
68         "target": fields.date(string="Target", help="Target Date"),
69         "milestone": fields.date(string="Milestone", help="Due date"),
70         "manday": fields.function(_get_manday, type="integer", string="Man-Days"),
71         "state": fields.selection(_status, string="State", select=True),
72         "tasks": fields.one2many("todolist.task", "container_id", string="Tasks"),
73         "topics_id": fields.many2many("todolist.topic", "todolist_container_topic_rel", "container_id", "topic_id", string="Topics", domain=[("activated", "=","Active")]),
74         "number_tasks": fields.function(_get_nb_tasks, type="integer", string="Number of tasks"),
75         "number_tasks_done": fields.function(_get_nb_tasks_done, type="integer", string="Number of tasks done"),
76         "progress_tasks": fields.function(_tasks_progress, type="float", string="Progression"),
77     }
78
79     _defaults = {
80         "state": "draft",
81     }
82
83     _sql_constraints = [
84         (
85             "name_different_from_description_constraint",
86             "CHECK(name <> description)",
87             "Fields name and description should be different",
88         ),
89         (
90             "target_before_milestone_constraint",
91             "CHECK(target <= milestone)",
92             "The target date should be previous milestone date",
93         ),
94     ]
95
96     _order = "name"
97
98     def action_start(self, cr, uid, ids, context=None):
99         self.write(cr, uid, ids, {"state": "pending"}, context=context)
100         return self
101
102
103     def action_stop(self, cr, uid, ids, context=None):
104         self.write(cr, uid, ids, {"state": "done"}, context=context)
105         return self
106
107
108     def action_restart(self, cr, uid, ids, context=None):
109         self.write(cr, uid, ids, {"state": "draft"}, context=context)
110         return self
111
112
113     def search(self, cr, user, args=[], offset=0, limit=None, order=None, context=None, count=False):
114         args.append(("create_uid", "=", user))
115         if len(args) != 1:
116             args.insert(0, "&")
117         return osv.Model.search(self, cr, user, args, offset, limit, order, context, count)
118
119
120 class Task(osv.Model):
121     """TODO List : A task (something to do in a to do list)"""
122
123     _name = "todolist.task"
124
125     _inherit = "mail.thread"
126
127     _priorities = [("Useful", "Useful"), ("Necessary", "Necessary"), ("Essential", "Essential")]
128
129     _states = [("draft", "Draft"), ("proposal", "Proposal"), ("approved", "Approved"), ("started", "Started"), ("done", "Done")]
130
131     _columns = {
132         "name": fields.char(string="Title", size=64, required=True),
133         "description": fields.text(string="Description"),
134         "planned": fields.date(string="Planed"),
135         "milestone": fields.date(string="Milestone", required=True),
136         "manday": fields.integer(string="Man-Days", required=True),
137         "priority": fields.selection(_priorities, string="Priority", select=True, required=True),
138         "state": fields.selection(_states, string="State", select=True),
139         "container_id": fields.many2one("todolist.container", string="To do list", required=True),
140     }
141
142     _defaults = {
143         "state": "draft",
144         "priority": "useful"
145     }
146
147     _order = "planned"
148
149     _sql_constraints = [
150         (
151             "name_different_from_description_constraint",
152             "CHECK(name <> description)",
153             "Fields name and description should be different",
154         ),
155         (
156             "planned_before_milestone_constraint",
157             "CHECK(planned <= milestone)",
158             "The planned date should be previous milestone date",
159         ),
160         (
161             "manday_sup_0_constraint",
162             "CHECK(manday >= 0)",
163             "The manday should be positive",
164         ),
165     ]
166
167
168     def action_draft(self, cr, uid, ids, context=None):
169         self.write(cr, uid, ids, {"state": "draft"}, context=context)
170         return self
171
172     def action_propose(self, cr, uid, ids, context=None):
173         self.write(cr, uid, ids, {"state": "proposal"}, context=context)
174         return self
175
176     def action_approve(self, cr, uid, ids, context=None):
177         self.write(cr, uid, ids, {"state": "approved"}, context=context)
178         return self
179
180     def action_start(self, cr, uid, ids, context=None):
181         self.write(cr, uid, ids, {"state": "started"}, context=context)
182         return self
183
184     def action_done(self, cr, uid, ids, context=None):
185         self.write(cr, uid, ids, {"state": "done"}, context=context)
186         return self
187
188     #chaque utilisateur voit seulement ces taches
189     def search(self, cr, user, args=[], offset=0, limit=None, order=None, context=None, count=False):
190         args.append(("create_uid", "=", user))
191         if len(args) != 1:
192             args.insert(0, "&")
193         return osv.Model.search(self, cr, user, args, offset, limit, order, context, count)
194
195     def write(self, cr, user, ids, vals, context=None):
196         if "milestone" in vals.keys():
197             for task in self.browse(cr, user, ids, context=context):
198                 if task.container_id.milestone < vals["milestone"]:
199                     vals["milestone"] = task.container_id.milestone
200         return osv.Model.write(self, cr, user, ids, vals, context)
201
202     def create(self, cr, user, vals, context=None):
203         container_model = self.pool.get("todolist.container")
204         container = container_model.read(cr, user, vals["container_id"], context=context)
205         milestone = container["milestone"]
206         if milestone < vals["milestone"]:
207             vals["milestone"] = milestone
208         return osv.Model.create(self, cr, user, vals, context=context)
209
210
211 class Topic(osv.Model):
212     """TODO List : Container"s Topic"""
213
214     def _get_nb_lists(self, cr, uid, ids, field, arg, context=None):
215         result = {}
216         for topic in self.browse(cr, uid, ids, context=context):
217             result[topic.id] = len(topic.todolist_ids)
218         return result
219
220
221     def _get_number_tasks(self, cr, uid, ids, field, arg, context=None):
222         result = {}
223         for topic in self.browse(cr, uid, ids, context=context):
224             result[topic.id] = sum([t.number_tasks for t in topic.todolist_ids])
225         return result
226
227
228     def _get_number_tasks_done(self, cr, uid, ids, field, arg, context=None):
229         result = {}
230         for topic in self.browse(cr, uid, ids, context=context):
231             result[topic.id] = sum([t.number_tasks_done for t in topic.todolist_ids])
232         return result
233
234
235     def _progress_tasks(self, cr, uid, ids, field, arg, context=None):
236         result = {}
237         for t in self.browse(cr, uid, ids, context=context):
238             result[t.id] = t.number_tasks and t.number_tasks_done*100./t.number_tasks or 0.
239         return result
240
241
242     _name = "todolist.topic"
243
244     _inherit = "mail.thread"
245
246     _states = [("Active", "Active"), ("Inactive", "Inactive")]
247
248     _columns = {
249         "name": fields.char(string="Title", size=64, required=True),
250         "description": fields.text(string="Description"),
251         "activated": fields.selection(_states, string="State", select=True),
252         "todolist_ids": fields.many2many("todolist.container", "todolist_container_topic_rel", "topic_id", "Container_id", string="TO DO Lists"),
253         "nb_lists": fields.function(_get_nb_lists, type="integer", string="Number of lists"),
254         "number_tasks": fields.function(_get_number_tasks, type="integer", string="Number of tasks"),
255         "number_tasks_done": fields.function(_get_number_tasks_done, type="integer", string="Number of tasks done"),
256         "progress_tasks": fields.function(_progress_tasks, type="float", string="Number of lists"),
257     }
258
259     _defaults = {
260         "activated": "Active",
261     }
262
263
264     _sql_constraints = [
265         (
266             "name_different_from_description_constraint",
267             "CHECK(name <> description)",
268             "Fields name and description should be different",
269         ),
270     ]