[Mpuls-commits] r130 - in waska/trunk: . waskaweb/model

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Wed Sep 3 17:06:36 CEST 2008


Author: teichmann
Date: 2008-09-03 17:06:36 +0200 (Wed, 03 Sep 2008)
New Revision: 130

Modified:
   waska/trunk/ChangeLog.txt
   waska/trunk/waskaweb/model/case.py
   waska/trunk/waskaweb/model/phases_factory.py
Log:
Load phase times from database.


Modified: waska/trunk/ChangeLog.txt
===================================================================
--- waska/trunk/ChangeLog.txt	2008-09-03 14:18:30 UTC (rev 129)
+++ waska/trunk/ChangeLog.txt	2008-09-03 15:06:36 UTC (rev 130)
@@ -1,5 +1,14 @@
 2008-09-03  Sascha L. Teichmann <sascha.teichmann at intevation.de>
 
+	Load phase times from database.
+
+	* waskaweb/model/phases_factory.py: Load phase times from database. 
+	  Needs a flag 'time:phase1,phase2,..'.
+
+	* waskaweb/model/case.py: Load required fields and times from database.
+
+2008-09-03  Sascha L. Teichmann <sascha.teichmann at intevation.de>
+
 	Build phases table from FormEd tree.
 
 	* waskaweb/model/phases_factory.py: New. contains old needed.py stuff and

Modified: waska/trunk/waskaweb/model/case.py
===================================================================
--- waska/trunk/waskaweb/model/case.py	2008-09-03 14:18:30 UTC (rev 129)
+++ waska/trunk/waskaweb/model/case.py	2008-09-03 15:06:36 UTC (rev 130)
@@ -1023,7 +1023,7 @@
             cur = con.cursor()
             rf = RequiredFields()
             rf.extractRequiredFields(g.formedTree)
-            return rf.checkIfFilled(self.id, cur) and rf or None
+            return rf.loadFromDatabase(self.id, cur) and rf or None
         finally:
             db.recycleConnection(con, cur)
 

Modified: waska/trunk/waskaweb/model/phases_factory.py
===================================================================
--- waska/trunk/waskaweb/model/phases_factory.py	2008-09-03 14:18:30 UTC (rev 129)
+++ waska/trunk/waskaweb/model/phases_factory.py	2008-09-03 15:06:36 UTC (rev 130)
@@ -60,9 +60,9 @@
 
     def extractRequiredFields(self, document):
 
-        phases = {}
+        phases, times = {}, {}
 
-        all = []
+        all, all_times = [], []
         
         for nc in document.walk():
             flags = nc.getFlags()
@@ -76,15 +76,24 @@
                     if kind == "required":
                         for p in [p.strip() for p in phase.split(',')]:
                             if p: phases.setdefault(p, []).append([nc, None])
+                    elif kind == "time":
+                        for p in [p.strip() for p in phase.split(',')]:
+                            if p: times.setdefault(p, []).append([nc, None])
                 else:
                     # TODO: Fix formed tree
                     if p[0] == "required":
                         all.append([nc, None])
+                    if p[0] == "time":
+                        all_times.append([nc, None])
 
         for a in all:
             for v in phases.itervalues():
                 v.append(a)
 
+        for t in all_times:
+            for v in times.itervalues():
+                v.append(t)
+
         for v in phases.itervalues():
             v.sort(cmp=lambda a, b: cmp(description(a[0]), description(b[0])))
 
@@ -93,12 +102,47 @@
         #        k, ', '.join([nc[0].getName() for nc in phases[k]]))
 
         self.phases = phases
+        self.times  = times
             
         return phases
 
-    def checkIfFilled(self, ds_id, cur):
+    def loadTimes(self, ds_id, cur):
         fields = {}
         idx = 0
+        for f in self.times.itervalues():
+            for l in f:
+                name = l[0].getName()
+                if not fields.has_key(name):
+                    fields[name] = idx
+                    idx += 1
+
+        all = fields.items()
+
+        if not all: return True
+
+        all.sort(cmp = lambda a, b: cmp(a[1], b[1]))
+
+        select = SELECT % ', '.join([a[0] for a in all])
+
+        cur.execute(select, { 'id': ds_id })
+        row = cur.fetchone()
+        if not row: return False
+
+        for a in all:
+            fields[a[0]] = row[a[1]]
+
+        for a in self.times.itervalues():
+            for b in a:
+                b[1] = fields.get(b[0].getName())
+
+        return True
+
+    def loadFromDatabase(self, ds_id, cur):
+        return self.loadFilled(ds_id, cur) and self.loadTimes(ds_id, cur) and True or False
+
+    def loadFilled(self, ds_id, cur):
+        fields = {}
+        idx = 0
         for f in self.phases.itervalues():
             for l in f:
                 name = l[0].getName()
@@ -107,6 +151,9 @@
                     idx += 1
 
         all = fields.items()
+
+        if not all: return True
+
         all.sort(cmp = lambda a, b: cmp(a[1], b[1]))
 
         select_terms = []
@@ -135,6 +182,13 @@
     def getPhase(self, phase):
         return self.phases.get(phase)
 
+
+    def getPhaseTime(self, phase):
+        try:
+            time = self.times[phase][1]
+        except KeyError:
+            return None
+
     def isPhaseComplete(self, phase):
         try:
             for l in self.phases[phase]:
@@ -161,69 +215,38 @@
 
         return ',\n'.join(out)
 
+PAIRS = (
+    (CLEAR_START,    CLEAR_ENDE,    u"Clearing"),
+    (CM_START,       CM_ENDE,       u"Case-Management"),
+    (BERATUNG_START, BERATUNG_ENDE, u"Beratung"),
+    (NB_START,       NB_ENDE,       u"Nachbetreuung"))
+
 def phases_pairs(required_fields, current_phase):
 
-    clear_start = Phase(
-        CLEAR_START,
-        CLEAR_START == current_phase,
-        required_fields.isPhaseComplete(phase_symbol(CLEAR_START)),
-        None,
-        required_fields.getLinkListForPhase(phase_symbol(CLEAR_START)))
+    pairs = []
 
-    clear_ende = Phase(
-        CLEAR_ENDE,
-        CLEAR_ENDE == current_phase,
-        required_fields.isPhaseComplete(phase_symbol(CLEAR_ENDE)),
-        None,
-        required_fields.getLinkListForPhase(phase_symbol(CLEAR_ENDE)))
+    for p1, p2, description in PAIRS:
 
-    cm_start = Phase(
-        CM_START,
-        CM_START == current_phase,
-        required_fields.isPhaseComplete(phase_symbol(CM_START)),
-        None,
-        required_fields.getLinkListForPhase(phase_symbol(CM_START)))
+        symbol = phase_symbol(p1)
 
-    cm_ende = Phase(
-        CM_ENDE,
-        CM_ENDE == current_phase,
-        required_fields.isPhaseComplete(phase_symbol(CM_ENDE)),
-        None,
-        required_fields.getLinkListForPhase(phase_symbol(CM_ENDE)))
+        start = Phase(
+            p1,
+            p2 == current_phase,
+            required_fields.isPhaseComplete(symbol),
+            required_fields.getPhaseTime(symbol),
+            required_fields.getLinkListForPhase(symbol))
 
-    nb_start = Phase(
-        NB_START,
-        NB_START == current_phase,
-        required_fields.isPhaseComplete(phase_symbol(NB_START)),
-        None,
-        required_fields.getLinkListForPhase(phase_symbol(NB_START)))
+        symbol = phase_symbol(p2)
 
-    nb_ende = Phase(
-        NB_ENDE,
-        NB_ENDE == current_phase,
-        required_fields.isPhaseComplete(phase_symbol(NB_ENDE)),
-        None,
-        required_fields.getLinkListForPhase(phase_symbol(NB_ENDE)))
+        ende = Phase(
+            p2,
+            p2 == current_phase,
+            required_fields.isPhaseComplete(symbol),
+            required_fields.getPhaseTime(symbol),
+            required_fields.getLinkListForPhase(symbol))
 
-    berat_start = Phase(
-        BERATUNG_START,
-        BERATUNG_START == current_phase,
-        required_fields.isPhaseComplete(phase_symbol(BERATUNG_START)),
-        None,
-        required_fields.getLinkListForPhase(phase_symbol(BERATUNG_START)))
+        pairs.append(PhasePair(start, ende, description))
 
-    berat_ende = Phase(
-        BERATUNG_ENDE,
-        BERATUNG_ENDE == current_phase,
-        required_fields.isPhaseComplete(phase_symbol(BERATUNG_ENDE)),
-        None,
-        required_fields.getLinkListForPhase(phase_symbol(BERATUNG_ENDE)))
+    return pairs
 
-    clear = PhasePair(clear_start, clear_ende, u"Clearing")
-    cm    = PhasePair(cm_start,    cm_ende,    u"Case-Management")
-    berat = PhasePair(berat_start, berat_ende, u"Beratung")
-    nb    = PhasePair(nb_start,    nb_ende,    u"Nachbetreuung")
-
-    return [ clear, cm, berat, nb ]
-
 # vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:



More information about the Mpuls-commits mailing list