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