Ajout du nombre de list pour les topics
[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
12     def _get_nb_task(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 #        return dict((c.id, len(c.tasks)) for c in self.browse(cr, uid, ids, context=context))
18
19
20     def _task_progress(self, cr, uid, ids, field, arg, context=None):
21         result = {}
22         for container in self.browse(cr, uid, ids, context=context):
23             done = total = 0
24             for task in container.tasks.browse(cr, uid, ids, context=context):
25                 total += 1
26                 if task.state == "done":
27                     done += 1
28             result[container.id] = total and done/total or 0.
29         return result
30
31
32     def _task_progress2(self, cr, uid, ids, field, arg, context=None):
33         result = {}
34         for container in self.browse(cr, uid, ids, context=context):
35             tasks = container.tasks
36             total = len(tasks)
37             done = len([t for t in tasks if t.state == "done"])
38             result[container.id] = total and done/total or 0.
39         return result
40
41
42     _name = "todolist.container"
43
44     _status = [("draft", "Draft"), ("pending", "Pending"), ("done", "Done")]
45
46     _columns = {
47         "name": fields.char(string="Title", size=64, required=True),
48         "description": fields.text(string="Description"),
49         "target": fields.date(string="Target", help="Target Date"),
50         "milestone": fields.date(string="Milestone", help="Due date"),
51         "manday": fields.float(string="Man-Days", digits=(6, 2)),
52         "state": fields.selection(_status, string="State", select=True),
53         "tasks": fields.one2many("todolist.task", "container_id", string="Tasks"),
54         "topics_id": fields.many2many("todolist.topic", "todolist_container_topic_rel", "container_id", "topic_id", string="Topics", domain=[("activated", "=","Active")]),
55         "number_tasks": fields.function(_get_nb_task, type="integer", string="Number of tasks"),
56         "progress_tasks": fields.function(_task_progress2, type="float", string="Progression"),
57     }
58
59     _defaults = {
60         "state": "draft",
61     }
62
63     _sql_constraints = [
64         (
65             "name_different_from_description_constraint",
66             "CHECK(name <> description)",
67             "Fields name and description should be different",
68         ),
69         (
70             "target_before_milestone_constraint",
71             "CHECK(target < milestone)",
72             "The target date should be previous milestone date",
73         ),
74     ]
75
76     _order = "name"
77
78     def action_start(self, cr, uid, ids, context=None):
79         self.write(cr, uid, ids, {"state": "pending"}, context=context)
80         return self
81
82
83     def action_stop(self, cr, uid, ids, context=None):
84         self.write(cr, uid, ids, {"state": "done"}, context=context)
85         return self
86
87
88     def action_restart(self, cr, uid, ids, context=None):
89         self.write(cr, uid, ids, {"state": "draft"}, context=context)
90         return self
91
92
93     def search(self, cr, user, args=[], offset=0, limit=None, order=None, context=None, count=False):
94         args.append(("create_uid", "=", user))
95         if len(args) != 1:
96             args.insert(0, "&")
97         return osv.Model.search(self, cr, user, args, offset, limit, order, context, count)
98
99
100 class Task(osv.Model):
101     """TODO List : A task (something to do in a to do list)"""
102
103     _name = "todolist.task"
104
105     _priorities = [("useful", "Useful"), ("necessary", "Necessary"), ("essential", "Essential")]
106
107     _states = [("draft", "Draft"), ("proposal", "Proposal"), ("approved", "Approved"), ("started", "Started"), ("done", "Done")]
108
109     _columns = {
110         "name": fields.char(string="Title", size=64, required=True),
111         "description": fields.text(string="Description"),
112         "planned": fields.date(string="Planed"),
113         "milestone": fields.date(string="Milestone"),
114         "manday": fields.integer(string="Man-Days"),
115         "priority": fields.selection(_priorities, string="Priority", select=True),
116         "state": fields.selection(_states, string="State", select=True),
117         "container_id": fields.many2one("todolist.container", string="To do list", required=True),
118     }
119
120     _defaults = {
121         "state": "draft"
122     }
123
124     def action_draft(self, cr, uid, ids, context=None):
125         self.write(cr, uid, ids, {"state": "draft"}, context=context)
126         return self
127
128     def action_propose(self, cr, uid, ids, context=None):
129         self.write(cr, uid, ids, {"state": "proposal"}, context=context)
130         return self
131
132     def action_approve(self, cr, uid, ids, context=None):
133         self.write(cr, uid, ids, {"state": "approved"}, context=context)
134         return self
135
136     def action_start(self, cr, uid, ids, context=None):
137         self.write(cr, uid, ids, {"state": "started"}, context=context)
138         return self
139
140     def action_done(self, cr, uid, ids, context=None):
141         self.write(cr, uid, ids, {"state": "done"}, context=context)
142         return self
143
144     def search(self, cr, user, args=[], offset=0, limit=None, order=None, context=None, count=False):
145         args.append(("create_uid", "=", user))
146         if len(args) != 1:
147             args.insert(0, "&")
148         return osv.Model.search(self, cr, user, args, offset, limit, order, context, count)
149
150
151
152
153 class Topic(osv.Model):
154     """TODO List : Container"s Topic"""
155
156     def _get_nb_lists(self, cr, uid, ids, field, arg, context=None):
157         result = {}
158         for topic in self.browse(cr, uid, ids, context=context):
159             result[topic.id] = len(topic.todolist_ids)
160         return result
161
162
163     _name = "todolist.topic"
164
165     _states = [("Active", "Active"), ("Inactive", "Inactive")]
166
167     _columns = {
168         "name": fields.char(string="Title", size=64, required=True),
169         "description": fields.text(string="Description"),
170         "activated": fields.selection(_states, string="State", select=True),
171         "todolist_ids": fields.many2many("todolist.container", "todolist_container_topic_rel", "topic_id", "Container_id", string="TO DO Lists"),
172         "nb_lists": fields.function(_get_nb_lists, type="integer", string="Number of lists"),
173     }
174
175     _defaults = {
176         "activated": "Active",
177     }