[Mpuls-commits] r1726 - wasko/branches/2.0/jmdweb/model

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Wed Feb 24 11:21:19 CET 2010


Author: torsten
Date: 2010-02-24 11:21:19 +0100 (Wed, 24 Feb 2010)
New Revision: 1726

Modified:
   wasko/branches/2.0/jmdweb/model/phase.py
Log:
* jmdweb/model/phase.py: Enabled evaluating rules defined in
  configuration.


Modified: wasko/branches/2.0/jmdweb/model/phase.py
===================================================================
--- wasko/branches/2.0/jmdweb/model/phase.py	2010-02-24 10:19:27 UTC (rev 1725)
+++ wasko/branches/2.0/jmdweb/model/phase.py	2010-02-24 10:21:19 UTC (rev 1726)
@@ -1,16 +1,25 @@
 # -*- coding: utf-8 -*-
+import logging
 from pylons import session, g, c
+from pylons.i18n import _ 
 
+from formed.instance.backends.postgres import DBFactory as InstanceFactory
+from mpulsweb.lib.db import PostgresDBInterface
+
+log = logging.getLogger(__name__)
+
 class PhaseFactory:
 
     def __init__(self):
         self.phases = PhaseList() 
 
     def load(self, id):
+        factory = InstanceFactory(g.formedTree, PostgresDBInterface())
+        it = factory.loadInstanceTreeByIdentifier(id)
         for p in g.mpuls_config.get('phases', 'phases'):
             desc  = g.mpuls_config.get('phases', 'description')[0].get(p)
-            start = PhasePart(g.mpuls_config.get('phases', 'pairs')[0].get(p)[0])
-            end   = PhasePart(g.mpuls_config.get('phases', 'pairs')[0].get(p)[1])
+            start = PhasePart(g.mpuls_config.get('phases', 'pairs')[0].get(p)[0], it)
+            end   = PhasePart(g.mpuls_config.get('phases', 'pairs')[0].get(p)[1], it)
             self.phases.append(Phase(start, end, desc))
         return self.phases
 
@@ -45,84 +54,87 @@
         #return self.start.isRunning() or self.end.isRunning()
 
 class PhasePart:
-    def __init__(self, id):
+    def __init__(self, id, it):
         self.id = id
-        self.condition = Condition()
+        self.fields = []
+        pfields = g.mpuls_config.get('phases', 'fields')[0]
+        for phasepart, fields in pfields.iteritems():          
+            if phasepart == id:
+                for field in fields:
+                    name = field.get('name')
+                    page = field.get('page')
+                    rules = field.get('rules', [])
+                    if name:
+                        self.fields.append(Field(name, page, rules, it))
 
     def getLinks(self):
-        return ""
+        out = []
+        if len(self.fields) > 0:
+            for field in self.fields:
+                out.append(field.get_link())
+        else:
+            out.append(_('No required fields found'))
+        return ", ".join(out)
 
     def getTime(self):
         return ""
 
-    def isOk(self):
-        return self.condition.isOk()
+    def is_ok(self):
+        for field in self.fields:
+            if not field.is_ok(): return False
+        return True 
 
+class Field:
+    def __init__(self, name, page, rules, it):
+        self.name = name
+        self.page = page
+        self.it = it
+        self.conditions = []
+
+        case_id = self.it.getRootNode().getIdentifier()
+        real_id = "%s:%s" % (self.name, case_id)
+        field = self.it.getItem(real_id)
+        if field:
+            meta = field.getMeta() 
+            self.label = meta.getDescription() 
+            for condition in rules: 
+                expr = condition.get('expr')
+                msg = condition.get('msg')
+                self.conditions.append(Condition(field, expr, msg))
+
+    def get_link(self):
+        out = []
+        case_id = session['case'].id
+        style = "ok" and self.is_ok() or "error"  
+        out.append('<a class="%s" href="/formularpage/select/%s:%s#%s">' 
+            % (style, self.page, case_id, self.name))
+        out.append('%s' % self.label)
+        out.append('</a>')
+        return "".join(out)
+
+    def is_ok(self):
+        for c in self.conditions:
+            if not c.is_ok():
+                return False
+        return True 
+
 class Condition:
 
-    def __init__(self):
-        self.ok = False
-        self.requiredFields = []
+    def __init__(self, field, expr, msg):
+        self.field = field 
+        self.expr = expr
+        self.msg = msg
 
-    def isOk(self):
-        return self.ok
+    def is_ok(self):
+        '''Returns True if expr is valid for the current value in field
+        identified with name'''
+        return self.evaluate()
 
-#class Phase:
-#
-#    def __init__(self, phase, running = False, complete = False, time = None, links = ""):
-#        self.phase    = phase
-#        self.running  = running
-#        self.complete = complete
-#        self.time     = time
-#        self.links    = links
-#        self.preds    = []
-#
-#    def isNeighbor(self, other):
-#        return phase_neighbors(self.phase, other.phase)
-#
-#    def isRunning(self):
-#        return self.running
-#
-#    def addPredecessor(self, pred):
-#        self.preds.append(pred)
-#
-#    def getLabel(self):
-#        return phase_symbol(self.phase)
-#
-#    def getDescription(self):
-#        return phase_description(self.phase)
-#
-#    def isComplete(self):
-#        return self.complete
-#
-#    def isRecursiveComplete(self):
-#        if not self.complete:
-#            return False
-#
-#        for pred in self.preds:
-#            if not pred.isRecursiveComplete():
-#                return False
-#
-#        return True
-#
-#    def hasPredecessor(self, other):
-#        #for p in self.preds:
-#        #    if other.phase == p.phase or p.hasPredecessor(other): return True
-#        #return False
-#        predecessors = phase_predecessors_path(self.phase)
-#        return other.phase in predecessors
-#
-#    def hasSuccessor(self, other):
-#        successors = phase_successors_path(self.phase)
-#        return other.phase in successors
-#
-#    def getTime(self):
-#        if str(self.time) != '0001-01-01':
-#            return self.time
-#        return None
-#
-#    def getLinks(self):
-#        return self.links
+    def evaluate(self):
+        meta  = self.field.getMeta()
+        value = self.field.getValue()
+        if self.expr == "isset":
+            if value not in [-1, "-1", None, ""]: return True
+        return False
 
-
 # vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:



More information about the Mpuls-commits mailing list