Modification de la dupplication des container
[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     _priorities = [("Useful", "Useful"), ("Necessary", "Necessary"), ("Essential", "Essential")]
124
125     _states = [("draft", "Draft"), ("proposal", "Proposal"), ("approved", "Approved"), ("started", "Started"), ("done", "Done")]
126
127     _columns = {
128         "name": fields.char(string="Title", size=64, required=True),
129         "description": fields.text(string="Description"),
130         "planned": fields.date(string="Planed"),
131         "milestone": fields.date(string="Milestone", required=True),
132         "manday": fields.integer(string="Man-Days", required=True),
133         "priority": fields.selection(_priorities, string="Priority", select=True, required=True),
134         "state": fields.selection(_states, string="State", select=True),
135         "container_id": fields.many2one("todolist.container", string="To do list", required=True),
136     }
137
138     _defaults = {
139         "state": "draft",
140         "priority": "useful"
141     }
142
143     _order = "planned"
144
145     _sql_constraints = [
146         (
147             "name_different_from_description_constraint",
148             "CHECK(name <> description)",
149             "Fields name and description should be different",
150         ),
151         (
152             "planned_before_milestone_constraint",
153             "CHECK(planned < milestone)",
154             "The planned date should be previous milestone date",
155         ),
156         (
157             "manday_sup_0_constraint",
158             "CHECK(manday > 0)",
159             "The manday should be positive",
160         ),
161     ]
162
163
164     def action_draft(self, cr, uid, ids, context=None):
165         self.write(cr, uid, ids, {"state": "draft"}, context=context)
166         return self
167
168     def action_propose(self, cr, uid, ids, context=None):
169         self.write(cr, uid, ids, {"state": "proposal"}, context=context)
170         return self
171
172     def action_approve(self, cr, uid, ids, context=None):
173         self.write(cr, uid, ids, {"state": "approved"}, context=context)
174         return self
175
176     def action_start(self, cr, uid, ids, context=None):
177         self.write(cr, uid, ids, {"state": "started"}, context=context)
178         return self
179
180     def action_done(self, cr, uid, ids, context=None):
181         self.write(cr, uid, ids, {"state": "done"}, context=context)
182         return self
183
184     #chaque utilisateur voit seulement ces taches
185     def search(self, cr, user, args=[], offset=0, limit=None, order=None, context=None, count=False):
186         args.append(("create_uid", "=", user))
187         if len(args) != 1:
188             args.insert(0, "&")
189         return osv.Model.search(self, cr, user, args, offset, limit, order, context, count)
190
191     def write(self, cr, user, ids, vals, context=None):
192         if "milestone" in vals.keys():
193             for task in self.browse(cr, user, ids, context=context):
194                 if task.container_id.milestone < vals["milestone"]:
195                     vals["milestone"] = task.container_id.milestone
196         return osv.Model.write(self, cr, user, ids, vals, context)
197
198     def create(self, cr, user, vals, context=None):
199         container_model = self.pool.get("todolist.container")
200         container = container_model.read(cr, user, vals["container_id"], context=context)
201         milestone = container["milestone"]
202         if milestone < vals["milestone"]:
203             vals["milestone"] = milestone
204         return osv.Model.create(self, cr, user, vals, context=context)
205
206
207 class Topic(osv.Model):
208     """TODO List : Container"s Topic"""
209
210     def _get_nb_lists(self, cr, uid, ids, field, arg, context=None):
211         result = {}
212         for topic in self.browse(cr, uid, ids, context=context):
213             result[topic.id] = len(topic.todolist_ids)
214         return result
215
216
217     def _get_number_tasks(self, cr, uid, ids, field, arg, context=None):
218         result = {}
219         for topic in self.browse(cr, uid, ids, context=context):
220             result[topic.id] = sum([t.number_tasks for t in topic.todolist_ids])
221         return result
222
223
224     def _get_number_tasks_done(self, cr, uid, ids, field, arg, context=None):
225         result = {}
226         for topic in self.browse(cr, uid, ids, context=context):
227             result[topic.id] = sum([t.number_tasks_done for t in topic.todolist_ids])
228         return result
229
230
231     def _progress_tasks(self, cr, uid, ids, field, arg, context=None):
232         result = {}
233         for t in self.browse(cr, uid, ids, context=context):
234             result[t.id] = t.number_tasks and t.number_tasks_done*100./t.number_tasks or 0.
235         return result
236
237
238     _name = "todolist.topic"
239
240     _states = [("Active", "Active"), ("Inactive", "Inactive")]
241
242     _columns = {
243         "name": fields.char(string="Title", size=64, required=True),
244         "description": fields.text(string="Description"),
245         "activated": fields.selection(_states, string="State", select=True),
246         "todolist_ids": fields.many2many("todolist.container", "todolist_container_topic_rel", "topic_id", "Container_id", string="TO DO Lists"),
247         "nb_lists": fields.function(_get_nb_lists, type="integer", string="Number of lists"),
248         "number_tasks": fields.function(_get_number_tasks, type="integer", string="Number of tasks"),
249         "number_tasks_done": fields.function(_get_number_tasks_done, type="integer", string="Number of tasks done"),
250         "progress_tasks": fields.function(_progress_tasks, type="float", string="Number of lists"),
251     }
252
253     _defaults = {
254         "activated": "Active",
255     }
256
257
258     _sql_constraints = [
259         (
260             "name_different_from_description_constraint",
261             "CHECK(name <> description)",
262             "Fields name and description should be different",
263         ),
264     ]