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