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