Modification ordre de tri des containers
[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
94
95 class Task(osv.Model):
96     """TODO List : A task (something to do in a to do list)"""
97
98     _name = "todolist.task"
99
100     _priorities = [("useful", "Useful"), ("necessary", "Necessary"), ("essential", "Essential")]
101
102     _states = [("draft", "Draft"), ("proposal", "Proposal"), ("approved", "Approved"), ("started", "Started"), ("done", "Done")]
103
104     _columns = {
105         "name": fields.char(string="Title", size=64, required=True),
106         "description": fields.text(string="Description"),
107         "planned": fields.date(string="Planed"),
108         "milestone": fields.date(string="Milestone"),
109         "manday": fields.integer(string="Man-Days"),
110         "priority": fields.selection(_priorities, string="Priority", select=True),
111         "state": fields.selection(_states, string="State", select=True),
112         "container_id": fields.many2one("todolist.container", string="To do list", required=True),
113     }
114
115     _defaults = {
116         "state": "draft"
117     }
118
119     def action_draft(self, cr, uid, ids, context=None):
120         self.write(cr, uid, ids, {"state": "draft"}, context=context)
121         return self
122
123     def action_propose(self, cr, uid, ids, context=None):
124         self.write(cr, uid, ids, {"state": "proposal"}, context=context)
125         return self
126
127     def action_approve(self, cr, uid, ids, context=None):
128         self.write(cr, uid, ids, {"state": "approved"}, context=context)
129         return self
130
131     def action_start(self, cr, uid, ids, context=None):
132         self.write(cr, uid, ids, {"state": "started"}, context=context)
133         return self
134
135     def action_done(self, cr, uid, ids, context=None):
136         self.write(cr, uid, ids, {"state": "done"}, context=context)
137         return self
138
139
140 # ------------------- class Theme ------------------- #
141
142 class Topic(osv.Model):
143     """TODO List : Container"s Topic"""
144
145     _name = "todolist.topic"
146
147     _states = [("Active", "Active"), ("Inactive", "Inactive")]
148
149     _columns = {
150         "name": fields.char(string="Title", size=64, required=True),
151         "description": fields.text(string="Description"),
152         "activated": fields.selection(_states, string="State", select=True),
153         "todolist_ids": fields.many2many("todolist.container", "todolist_container_topic_rel", "topic_id", "Container_id", string="TO DO Lists"),
154     }
155
156     _defaults = {
157         "activated": "Active",
158     }