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

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Tue Apr 20 11:32:42 CEST 2010


Author: torsten
Date: 2010-04-20 11:32:41 +0200 (Tue, 20 Apr 2010)
New Revision: 2430

Modified:
   wasko/branches/2.0/mpulsweb/model/phase.py
Log:
* mpulsweb/model/phase.py: Seperated building the phaselist and
loading data from a given instance tree.


Modified: wasko/branches/2.0/mpulsweb/model/phase.py
===================================================================
--- wasko/branches/2.0/mpulsweb/model/phase.py	2010-04-20 09:31:39 UTC (rev 2429)
+++ wasko/branches/2.0/mpulsweb/model/phase.py	2010-04-20 09:32:41 UTC (rev 2430)
@@ -80,25 +80,33 @@
                 self.pre[p] = []
                 self.pre[p].extend(k)
 
-    def load(self, id):
-        factory = InstanceFactory(g.formedTree, PostgresDBInterface())
-        it = factory.loadInstanceTreeByIdentifier(id)
-
+    def build(self):
+        '''Build phaselist from metadata'''
         for p in g.mpuls_config.get('phases', 'phases'):
             desc = g.mpuls_config.get('phases', 'description')[0].get(p)
             pair = g.mpuls_config.get('phases', 'pairs')[0].get(p)
-            start = StartPhasePart(pair[0], p, it, 
+            start = StartPhasePart(pair[0], p, 
                                    self.suc.get(pair[0], []), 
                                    self.pre.get(pair[0], []))
-            end = EndPhasePart(pair[1], p, it,
+            end = EndPhasePart(pair[1], p,
                                    self.suc.get(pair[1], []), 
                                    self.pre.get(pair[1], []))
             self.phases.append(Phase(start, end, desc))
-
         self.phases.set_suc(self.suc)
         self.phases.set_pre(self.pre)
         return self.phases
 
+    def load(self, id):
+        '''Build phaselist from metadata and load current state from instance tree'''
+        self.build()
+        factory = InstanceFactory(g.formedTree, PostgresDBInterface())
+        it = factory.loadInstanceTreeByIdentifier(id)
+        for p in self.phases:
+            start = p.start
+            end = p.end
+            start.load(it)
+            end.load(it)
+        return self.phases
 
 class PhaseList(list):
 
@@ -124,7 +132,7 @@
                 return p2
 
         # Return dummy phase for unknown phase
-        p = EndPhasePart('-1', None, None, ['1'], [])
+        p = EndPhasePart('-1', None, ['1'], [])
         p.phase = '-1'
         return p
 
@@ -188,12 +196,13 @@
 
 class PhasePart:
 
-    def __init__(self, id, phase, it, suc, pre):
+    def __init__(self, id, phase, suc, pre):
         self.id = id
-        self.it = it
+        self.it = None 
         self.phase = phase
         self.active = False
         self.date = None
+        self.datefield = None
         self.suc = suc
         self.pre = pre
         self.fields = []
@@ -203,9 +212,15 @@
         if self.id == '0':
             self.pre.append('-1')
 
-        if not it: return
+        # Get phase relevat dates
+        for p in g.formedTree.walk():
+            flags = get_flags(p.getFlags())
+            if self.id in flags['time']:
+                self.datefield = p.getName()
+
+    def load(self, it):
         formed = it.getFormEd()
-
+        self.it = it
         # Check if the current phasepart is active
         case_id = it.getRootNode().getIdentifier()
         realid = "%s:%s" % (PHASEFIELD, case_id)
@@ -221,18 +236,19 @@
             name = meta.getName()
             identifier = p.getIdentifier()
             # Check if this fields is a phase relvant required field 
-            if id in flags['required']:
+            if self.id in flags['required']:
                 # Only add warnigs and rules with "required" in der name
                 rules = [rule for rule in formed.name2markingrules.get(name, ()) 
                          if rule.isWarning() and REQUIRED.match(rule.getName())]
                 self.fields.append(Field(identifier, page, rules, it))
-            # Check if this fields is a phase relvant datefield
-            if id in flags['time']:
-                realid = "%s:%s" % (name, case_id)
-                field = it.getItem(realid)
-                warnings = [] 
-                self.date = field.getValue()
 
+        # Load phase relvant datefield
+        if self.datefield is not None:
+            realid = "%s:%s" % (self.datefield, case_id)
+            field = it.getItem(realid)
+            warnings = [] 
+            self.date = field.getValue()
+
     def is_neighbor(self, phase):
         return (phase in self.suc) or (phase in self.pre)
 
@@ -259,7 +275,7 @@
         # 1. Simulate switching the phase
         # Setting values will trigger evaluation error rules. This way we can
         # check logic rules which are modeled as errors.
-        if check_setting_phase:
+        if check_setting_phase and self.it:
             case_id = self.it.getRootNode().getIdentifier()
             realid = "%s:%s" % (PHASEFIELD, case_id)
             try:



More information about the Mpuls-commits mailing list