Sur task, ajout du chatter
[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     _status = [("draft", "Draft"), ("pending", "Pending"), ("done", "Done")]
62
63     _columns = {
64         "name": fields.char(string="Title", size=64, required=True),
65         "description": fields.text(string="Description"),
66         "target": fields.date(string="Target", help="Target Date"),
67         "milestone": fields.date(string="Milestone", help="Due date"),
68         "manday": fields.function(_get_manday, type="integer", string="Man-Days"),
69         "state": fields.selection(_status, string="State", select=True),
70         "tasks": fields.one2many("todolist.task", "container_id", string="Tasks"),
71         "topics_id": fields.many2many("todolist.topic", "todolist_container_topic_rel", "container_id", "topic_id", string="Topics", domain=[("activated", "=","Active")]),
72         "number_tasks": fields.function(_get_nb_tasks, type="integer", string="Number of tasks"),
73         "number_tasks_done": fields.function(_get_nb_tasks_done, type="integer", string="Number of tasks done"),
74         "progress_tasks": fields.function(_tasks_progress, type="float", string="Progression"),
75     }
76
77     _defaults = {
78         "state": "draft",
79     }
80
81     _sql_constraints = [
82         (
83             "name_different_from_description_constraint",
84             "CHECK(name <> description)",
85             "Fields name and description should be different",
86         ),
87         (
88             "target_before_milestone_constraint",
89             "CHECK(target < milestone)",
90             "The target date should be previous milestone date",
91         ),
92     ]
93
94     _order = "name"
95
96     def action_start(self, cr, uid, ids, context=None):
97         self.write(cr, uid, ids, {"state": "pending"}, context=context)
98         return self
99
100
101     def action_stop(self, cr, uid, ids, context=None):
102         self.write(cr, uid, ids, {"state": "done"}, context=context)
103         return self
104
105
106     def action_restart(self, cr, uid, ids, context=None):
107         self.write(cr, uid, ids, {"state": "draft"}, context=context)
108         return self
109
110
111     def search(self, cr, user, args=[], offset=0, limit=None, order=None, context=None, count=False):
112         args.append(("create_uid", "=", user))
113         if len(args) != 1:
114             args.insert(0, "&")
115         return osv.Model.search(self, cr, user, args, offset, limit, order, context, count)
116
117
118 class Task(osv.Model):
119     """TODO List : A task (something to do in a to do list)"""
120
121     _name = "todolist.task"
122
123     _inherit = "mail.thread"
124
125     _priorities = [("Useful", "Useful"), ("Necessary", "Necessary"), ("Essential", "Essential")]
126
127     _states = [("draft", "Draft"), ("proposal", "Proposal"), ("approved", "Approved"), ("started", "Started"), ("done", "Done")]
128
129     _columns = {
130         "name": fields.char(string="Title", size=64, required=True),
131         "description": fields.text(string="Description"),
132         "planned": fields.date(string="Planed"),
133         "milestone": fields.date(string="Milestone", required=True),
134         "manday": fields.integer(string="Man-Days", required=True),
135         "priority": fields.selection(_priorities, string="Priority", select=True, required=True),
136         "state": fields.selection(_states, string="State", select=True),
137         "container_id": fields.many2one("todolist.container", string="To do list", required=True),
138     }
139
140     _defaults = {
141         "state": "draft",
142         "priority": "useful"
143     }
144
145     _order = "planned"
146
147     _sql_constraints = [
148         (
149             "name_different_from_description_constraint",
150             "CHECK(name <> description)",
151             "Fields name and description should be different",
152         ),
153         (
154             "planned_before_milestone_constraint",
155             "CHECK(planned < milestone)",
156             "The planned date should be previous milestone date",
157         ),
158         (
159             "manday_sup_0_constraint",
160             "CHECK(manday >= 0)",
161             "The manday should be positive",
162         ),
163     ]
164
165
166     def action_draft(self, cr, uid, ids, context=None):
167         self.write(cr, uid, ids, {"state": "draft"}, context=context)
168         return self
169
170     def action_propose(self, cr, uid, ids, context=None):
171         self.write(cr, uid, ids, {"state": "proposal"}, context=context)
172         return self
173
174     def action_approve(self, cr, uid, ids, context=None):
175         self.write(cr, uid, ids, {"state": "approved"}, context=context)
176         return self
177
178     def action_start(self, cr, uid, ids, context=None):
179         self.write(cr, uid, ids, {"state": "started"}, context=context)
180         return self
181
182     def action_done(self, cr, uid, ids, context=None):
183         self.write(cr, uid, ids, {"state": "done"}, context=context)
184         return self
185
186     #chaque utilisateur voit seulement ces taches
187     def search(self, cr, user, args=[], offset=0, limit=None, order=None, context=None, count=False):
188         args.append(("create_uid", "=", user))
189         if len(args) != 1:
190             args.insert(0, "&")
191         return osv.Model.search(self, cr, user, args, offset, limit, order, context, count)
192
193     def write(self, cr, user, ids, vals, context=None):
194         if "milestone" in vals.keys():
195             for task in self.browse(cr, user, ids, context=context):
196                 if task.container_id.milestone < vals["milestone"]:
197                     vals["milestone"] = task.container_id.milestone
198         return osv.Model.write(self, cr, user, ids, vals, context)
199
200     def create(self, cr, user, vals, context=None):
201         container_model = self.pool.get("todolist.container")
202         container = container_model.read(cr, user, vals["container_id"], context=context)
203         milestone = container["milestone"]
204         if milestone < vals["milestone"]:
205             vals["milestone"] = milestone
206         return osv.Model.create(self, cr, user, vals, context=context)
207
208
209 class Topic(osv.Model):
210     """TODO List : Container"s Topic"""
211
212     def _get_nb_lists(self, cr, uid, ids, field, arg, context=None):
213         result = {}
214         for topic in self.browse(cr, uid, ids, context=context):
215             result[topic.id] = len(topic.todolist_ids)
216         return result
217
218
219     def _get_number_tasks(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] = sum([t.number_tasks for t in topic.todolist_ids])
223         return result
224
225
226     def _get_number_tasks_done(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_done for t in topic.todolist_ids])
230         return result
231
232
233     def _progress_tasks(self, cr, uid, ids, field, arg, context=None):
234         result = {}
235         for t in self.browse(cr, uid, ids, context=context):
236             result[t.id] = t.number_tasks and t.number_tasks_done*100./t.number_tasks or 0.
237         return result
238
239
240     _name = "todolist.topic"
241
242     _states = [("Active", "Active"), ("Inactive", "Inactive")]
243
244     _columns = {
245         "name": fields.char(string="Title", size=64, required=True),
246         "description": fields.text(string="Description"),
247         "activated": fields.selection(_states, string="State", select=True),
248         "todolist_ids": fields.many2many("todolist.container", "todolist_container_topic_rel", "topic_id", "Container_id", string="TO DO Lists"),
249         "nb_lists": fields.function(_get_nb_lists, type="integer", string="Number of lists"),
250         "number_tasks": fields.function(_get_number_tasks, type="integer", string="Number of tasks"),
251         "number_tasks_done": fields.function(_get_number_tasks_done, type="integer", string="Number of tasks done"),
252         "progress_tasks": fields.function(_progress_tasks, type="float", string="Number of lists"),
253     }
254
255     _defaults = {
256         "activated": "Active",
257     }
258
259
260     _sql_constraints = [
261         (
262             "name_different_from_description_constraint",
263             "CHECK(name <> description)",
264             "Fields name and description should be different",
265         ),
266     ]