[Mpuls-commits] r1840 - wasko/branches/2.0/mpulsweb/model

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Fri Feb 26 12:05:45 CET 2010


Author: torsten
Date: 2010-02-26 12:05:45 +0100 (Fri, 26 Feb 2010)
New Revision: 1840

Modified:
   wasko/branches/2.0/mpulsweb/model/case.py
Log:
* mpulsweb/model/case.py (get_default_value): Method which returns the
default value for a given item of the instancetree.
* mpulsweb/model/case.py (MpulsCase.make_consistent): Check all fields
up to the current phase and set value to default value if no value is
set.
* mpulsweb/model/case.py (MpulsCase.make_anonymizable): Ensure that
the current phase is finished. Then make the case consistent.


Modified: wasko/branches/2.0/mpulsweb/model/case.py
===================================================================
--- wasko/branches/2.0/mpulsweb/model/case.py	2010-02-26 10:57:28 UTC (rev 1839)
+++ wasko/branches/2.0/mpulsweb/model/case.py	2010-02-26 11:05:45 UTC (rev 1840)
@@ -31,16 +31,17 @@
 
 import psycopg2.extras
 
-from formed.meta.data import RepeatNode
+from formed.meta.data import RepeatNode, DateLeaf, IntLeaf, TextLeaf
 from formed.instance.backends.postgres import DBFactory as InstanceFactory
 from formed.instance.tree import SetDataException
 
 from mpulsweb.lib.base import g, session
+from mpulsweb.lib.anonymize import determineAnonymizedCaseEndPhase 
 from mpulsweb.lib.helpers import ensure_unicode
 from mpulsweb.lib.db import PostgresDBInterface, db
 
 from mpulsweb.model.phase import PhaseFactory, StartPhasePart, \
-    ConsistenceCheckException
+    ConsistenceCheckException, PHASEFIELD
 
 
 log = logging.getLogger(__name__)
@@ -68,6 +69,18 @@
     def __str__(self):
         return repr(self.value)
 
+def get_default_value(item):
+    """Will return the default value for a instance tree item"""
+    meta = item.getMeta()
+    if isinstance(meta, DateLeaf):
+        value  = '01.01.0001'
+    elif isinstance(meta, IntLeaf):
+       value  = '-999999'
+    elif isinstance(meta, TextLeaf):
+       value  = 'unbekannt'
+    else:
+       value  = '-3'
+    return value
 
 def get_field_identifier(id, name, it=None, idx=0):
     """Will return the identifier of a field. If the field in in a rg the
@@ -204,7 +217,6 @@
         cls.alias_dict = dict((field.alias, field) for field in fields
                                                    if field.alias is not None)
 
-
     @classmethod
     def get_preset_fields(cls):
         """Return a list with the names of formed fields that can be preset.
@@ -273,17 +285,44 @@
         to 'unknown'.  Missing relevant datefields (start, end) are set to the
         current date. 
         """
-        pass
+        curphase = self.phaseslist.get_current_phase_id()
+        data = {}
+        for phase in self.phaseslist:
+            start, end = phase.getStart(), phase.getEnd()
+            if start.id > curphase: break;
+            for field in start.fields:
+                item = self._get_formed_item(field.name)
+                value = item.getValue()
+                log.debug('Current value of %s: %s' % (field.name, value))
+                if value in [-1, "-1", None, ""]:
+                    def_value = get_default_value(item)
+                    log.debug("Setting %s to %s" % (field.name, def_value))
+                    data["%s:%s" % (field.name, self.id)] = def_value 
+            for field in end.fields:
+                item = self._get_formed_item(field.name)
+                value = item.getValue()
+                log.debug('Current value of %s: %s' % (field.name, value))
+                if item.getValue() in [-1, "-1", None, ""]:
+                    def_value = get_default_value(item)
+                    log.debug("Setting %s to %s" % (field.name, def_value))
+                    data["%s:%s" % (field.name, self.id)] = def_value 
 
+        # Set values
+        self.formed_instance.setData(data)
+
     def make_anonymizable(self):
         """Will ensure that the case is in a anonymizable state. This means
         that current phase is finished and the case is in a consistent state.
         """
+        self._load_formed_instance_tree()
+        data = {}
+
         # Check if the current phase is already finished. If not then set to
         # finished
+        end_phase = determineAnonymizedCaseEndPhase(self)
+        data['%s:%s' % (PHASEFIELD, self.id)] = end_phase
+        self.formed_instance.setData(data)
 
-        #TODO: Implement phase setting
-
         # Ensurse consistence of the case
         self.make_consistent()
 
@@ -343,13 +382,16 @@
             raise KeyError("Cannot find formed item for %r" % name)
 
     def _load_formed_instance_tree(self):
-        factory = InstanceFactory(g.formedTree, PostgresDBInterface())
-        self.formed_instance = factory.loadInstanceTreeByIdentifier(self.id)
+        if self.formed_instance is None:
+            factory = InstanceFactory(g.formedTree, PostgresDBInterface())
+            self.formed_instance = factory.loadInstanceTreeByIdentifier(self.id)
 
     def _get_formed_item(self, name):
+        self._load_formed_instance_tree()
         fid = get_field_identifier(self.id, name, self.formed_instance)
         if fid:
             return self.formed_instance.getItem(fid)
+        log.error('(_get_formed_item): Can not find %s' % name)
         return None
 
 



More information about the Mpuls-commits mailing list