[Mpuls-commits] r6130 - base/trunk/mpulsweb/model

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Fri Oct 26 21:06:31 CEST 2012


Author: bh
Date: 2012-10-26 21:06:31 +0200 (Fri, 26 Oct 2012)
New Revision: 6130

Modified:
   base/trunk/mpulsweb/model/case.py
Log:
Allow setting values in MpulsCase.copy and return new instance tree.
This makes it easier to create a modified copy. The caller may still
need to refer to the new case, e.g. to redirect to it, so the copy
method now returns the new instance tree.

This is needed for mpuls/issue2231.


Modified: base/trunk/mpulsweb/model/case.py
===================================================================
--- base/trunk/mpulsweb/model/case.py	2012-10-18 11:47:54 UTC (rev 6129)
+++ base/trunk/mpulsweb/model/case.py	2012-10-26 19:06:31 UTC (rev 6130)
@@ -1048,12 +1048,31 @@
                           % (self.id, userid))
         return self.editor
 
-    def copy(self):
-        """Copy the case and all its contents into a new case."""
+    def copy(self, new_values=None):
+        """Copy the case and all its contents into a new case.
+
+        If the new_values parameter is given it should be a dictionary
+        with values to be set in the copy. The keys of this dictionary
+        should be names of only to top-level, i.e. non-repeat-group,
+        fields and the values should be strings. This method constructs
+        a new dictionary where the keys have been converted to instance
+        specific keys with get_field_identifier so that it can be passed
+        to the new instance tree's setData method.
+
+        The method returns the instance tree of the copy.
+        """
         factory = self._get_instance_factory()
         self._load_formed_instance_tree()
-        factory.copy_instance_tree(self.formed_instance)
+        new_it = factory.copy_instance_tree(self.formed_instance)
 
+        if new_values:
+            new_id = new_it.getRootNode().getIdentifier()
+            new_it.setData(dict((get_field_identifier(new_id, key), value)
+                                for key, value in new_values.items()))
+            new_it.commit()
+
+        return new_it
+
     def getMeta(self):
         """Returns the meta object for the case identified with its id"""
         return Meta(self.id)



More information about the Mpuls-commits mailing list