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