Ajout contrainte pour taches : manday > 0
[OpenERP/todolist.git] / todolist.py
index 9c0ee61..6812316 100644 (file)
@@ -8,13 +8,13 @@ from openerp.osv import osv, fields
 class Container(osv.Model):
     """TODO List : Tasks container"""
 
-
     def _get_nb_tasks(self, cr, uid, ids, field, arg, context=None):
         result = {}
         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))
+#       OR : 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={}
@@ -22,12 +22,26 @@ class Container(osv.Model):
             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
 
+
+    def copy(self, cr, uid, id, default, context=None):
+        container = self.browse(cr, uid, id, context=context)
+        new_name =  "Copy of %s" % container.name
+        # =like is the original LIKE operator from SQL
+        others_count = self.search(cr,  uid, [('name', '=like', new_name+'%')],
+                                   count=True, context=context)
+        if others_count > 0:
+            new_name = "%s (%s)" % (new_name, others_count+1)
+        default['name'] = new_name
+        return osv.Model.copy(self, cr, uid, id, default, context=context)
+
+
     _name = "todolist.container"
 
     _status = [("draft", "Draft"), ("pending", "Pending"), ("done", "Done")]
@@ -42,7 +56,7 @@ class Container(osv.Model):
         "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_tasks, type="integer", string="Number of tasks"),
-        "number_tasks_done": fields.function(_get_nb_tasks_done, type="integer", string="Number of tasks"),
+        "number_tasks_done": fields.function(_get_nb_tasks_done, type="integer", string="Number of tasks done"),
         "progress_tasks": fields.function(_tasks_progress, type="float", string="Progression"),
     }
 
@@ -92,7 +106,7 @@ class Task(osv.Model):
 
     _name = "todolist.task"
 
-    _priorities = [("useful", "Useful"), ("necessary", "Necessary"), ("essential", "Essential")]
+    _priorities = [("Useful", "Useful"), ("Necessary", "Necessary"), ("Essential", "Essential")]
 
     _states = [("draft", "Draft"), ("proposal", "Proposal"), ("approved", "Approved"), ("started", "Started"), ("done", "Done")]
 
@@ -108,9 +122,31 @@ class Task(osv.Model):
     }
 
     _defaults = {
-        "state": "draft"
+        "state": "draft",
+        "priority": "useful"
     }
 
+    _order = "planned"
+
+    _sql_constraints = [
+        (
+            "name_different_from_description_constraint",
+            "CHECK(name <> description)",
+            "Fields name and description should be different",
+        ),
+        (
+            "planned_before_milestone_constraint",
+            "CHECK(planned < milestone)",
+            "The planned date should be previous milestone date",
+        ),
+        (
+            "manday_sup_0_constraint",
+            "CHECK(manday < 0)",
+            "The manday should be positive",
+        ),
+    ]
+
+
     def action_draft(self, cr, uid, ids, context=None):
         self.write(cr, uid, ids, {"state": "draft"}, context=context)
         return self
@@ -151,25 +187,23 @@ class Topic(osv.Model):
 
 
     def _get_number_tasks(self, cr, uid, ids, field, arg, context=None):
-        result = 0
+        result = {}
         for topic in self.browse(cr, uid, ids, context=context):
-            for container in self.browse(cr, uid, ids, context=context):
-                result += container.number_tasks
+            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 = 0
+        result = {}
         for topic in self.browse(cr, uid, ids, context=context):
-            for container in self.browse(cr, uid, ids, context=context):
-                result += container.number_tasks_done
+            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 topic in self.browse(cr, uid, ids, context=context):
-            result[topic.id] = topic.number_tasks and topic.number_tasks_done*100./topic.number_tasks or 0.
+        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
 
 
@@ -183,11 +217,20 @@ class Topic(osv.Model):
         "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"),
+        "number_tasks": fields.function(_get_number_tasks, type="integer", string="Number of tasks"),
+        "number_tasks_done": fields.function(_get_number_tasks_done, type="integer", string="Number of tasks done"),
         "progress_tasks": fields.function(_progress_tasks, type="float", string="Number of lists"),
     }
 
     _defaults = {
         "activated": "Active",
     }
+
+
+    _sql_constraints = [
+        (
+            "name_different_from_description_constraint",
+            "CHECK(name <> description)",
+            "Fields name and description should be different",
+        ),
+    ]