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