Maintenant ca marche
[OpenERP/todolist.git] / todolist.py
index c25f457..8acbe8b 100644 (file)
@@ -9,12 +9,24 @@ class Container(osv.Model):
     """TODO List : Tasks container"""
 
 
-    def _get_nb_task(self, cr, uid, ids, field, arg, context=None):
+    def _get_nb_tasks(self, cr, uid, ids, field, arg, context=None):
         result = {}
-        for tasks in self.browse(cr, uid, ids, context=context):
-            result[tasks.id] = len(tasks.tasks)
+        for container in self.browse(cr, uid, ids, context=context):
+            result[container.id] = len(container.tasks)
         return result
+#        return dict((c.id, len(c.tasks)) for c in self.browse(cr, uid, ids, context=context))
 
+    def _get_nb_tasks_done(self, cr, uid, ids, field, arg, context=None):
+        result={}
+        for c in self.browse(cr, uid, ids, context=context):
+            result[c.id] = len([t for t in c.tasks if t.state == "done"])
+        return result
+
+    def _tasks_progress(self, cr, uid, ids, field, arg, context=None):
+        result = {}
+        for c in self.browse(cr, uid, ids, context=context):
+            result[c.id] = c.number_tasks and c.number_tasks_done*100./c.number_tasks or 0.
+        return result
 
     _name = "todolist.container"
 
@@ -29,7 +41,9 @@ class Container(osv.Model):
         "state": fields.selection(_status, string="State", select=True),
         "tasks": fields.one2many("todolist.task", "container_id", string="Tasks"),
         "topics_id": fields.many2many("todolist.topic", "todolist_container_topic_rel", "container_id", "topic_id", string="Topics", domain=[("activated", "=","Active")]),
-        "number_tasks": fields.function(_get_nb_task, type="integer", string="Number of tasks"),
+        "number_tasks": fields.function(_get_nb_tasks, type="integer", string="Number of tasks"),
+        "number_tasks_done": fields.function(_get_nb_tasks_done, type="integer", string="Number of tasks"),
+        "progress_tasks": fields.function(_tasks_progress, type="float", string="Progression"),
     }
 
     _defaults = {
@@ -49,6 +63,8 @@ class Container(osv.Model):
         ),
     ]
 
+    _order = "name"
+
     def action_start(self, cr, uid, ids, context=None):
         self.write(cr, uid, ids, {"state": "pending"}, context=context)
         return self
@@ -64,6 +80,11 @@ class Container(osv.Model):
         return self
 
 
+    def search(self, cr, user, args=[], offset=0, limit=None, order=None, context=None, count=False):
+        args.append(("create_uid", "=", user))
+        if len(args) != 1:
+            args.insert(0, "&")
+        return osv.Model.search(self, cr, user, args, offset, limit, order, context, count)
 
 
 class Task(osv.Model):
@@ -110,12 +131,46 @@ class Task(osv.Model):
         self.write(cr, uid, ids, {"state": "done"}, context=context)
         return self
 
+    def search(self, cr, user, args=[], offset=0, limit=None, order=None, context=None, count=False):
+        args.append(("create_uid", "=", user))
+        if len(args) != 1:
+            args.insert(0, "&")
+        return osv.Model.search(self, cr, user, args, offset, limit, order, context, count)
+
+
 
-# ------------------- class Theme ------------------- #
 
 class Topic(osv.Model):
     """TODO List : Container"s Topic"""
 
+    def _get_nb_lists(self, cr, uid, ids, field, arg, context=None):
+        result = {}
+        for topic in self.browse(cr, uid, ids, context=context):
+            result[topic.id] = len(topic.todolist_ids)
+        return result
+
+
+    def _get_number_tasks(self, cr, uid, ids, field, arg, context=None):
+        result = {}
+        for topic in self.browse(cr, uid, ids, context=context):
+            result[topic.id] = sum([t.number_tasks for t in topic.todolist_ids])
+        return result
+
+
+    def _get_number_tasks_done(self, cr, uid, ids, field, arg, context=None):
+        result = {}
+        for topic in self.browse(cr, uid, ids, context=context):
+            result[topic.id] = sum([t.number_tasks_done for t in topic.todolist_ids])
+        return result
+
+
+    def _progress_tasks(self, cr, uid, ids, field, arg, context=None):
+        result = {}
+        for t in self.browse(cr, uid, ids, context=context):
+            result[t.id] = t.number_tasks and t.number_tasks_done*100./t.number_tasks or 0.
+        return result
+
+
     _name = "todolist.topic"
 
     _states = [("Active", "Active"), ("Inactive", "Inactive")]
@@ -125,6 +180,10 @@ class Topic(osv.Model):
         "description": fields.text(string="Description"),
         "activated": fields.selection(_states, string="State", select=True),
         "todolist_ids": fields.many2many("todolist.container", "todolist_container_topic_rel", "topic_id", "Container_id", string="TO DO Lists"),
+        "nb_lists": fields.function(_get_nb_lists, type="integer", string="Number of lists"),
+        "number_tasks": fields.function(_get_number_tasks, type="integer", string="Number of lists"),
+        "number_tasks_done": fields.function(_get_number_tasks_done, type="integer", string="Number of lists"),
+        "progress_tasks": fields.function(_progress_tasks, type="float", string="Number of lists"),
     }
 
     _defaults = {