Surcharge duplication todolist
authorAlicia FLOREZ <alicflorez@gmail.com>
Thu, 21 Mar 2013 10:23:42 +0000 (11:23 +0100)
committerAlicia FLOREZ <alicflorez@gmail.com>
Thu, 21 Mar 2013 10:23:42 +0000 (11:23 +0100)
todolist.py

index 8acbe8b..a555d79 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"),
     }
 
@@ -181,8 +195,8 @@ 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"),
     }