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