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

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Tue Apr 13 14:28:26 CEST 2010


Author: torsten
Date: 2010-04-13 14:28:26 +0200 (Tue, 13 Apr 2010)
New Revision: 2322

Modified:
   wasko/branches/2.0/mpulsweb/model/phase.py
Log:
* mpulsweb/model/phase.py (walk_it): Helper function to get all items
	ins instance tree.
* mpulsweb/model/phase.py (get_flags): Helper for formular items.
	Parses flags attribute and returns a dictionary with phase ids for
	required fields and time fields.
* mpulsweb/model/phase.py (PhasePart.__init__): Do not load
	information about required fields and time fields for json config but
	from formedtree.
* mpulsweb/model/phase.py (Field.__init__): Initialise fields with
	Rule-Nodes fro formed.
* mpulsweb/model/phase.py (Condition.evaluate): Evaluate rules
	depending on context (conditionals etc.)


Modified: wasko/branches/2.0/mpulsweb/model/phase.py
===================================================================
--- wasko/branches/2.0/mpulsweb/model/phase.py	2010-04-13 12:26:48 UTC (rev 2321)
+++ wasko/branches/2.0/mpulsweb/model/phase.py	2010-04-13 12:28:26 UTC (rev 2322)
@@ -1,26 +1,50 @@
 # -*- coding: utf-8 -*-
 
 import logging
+import re
 
 from pylons import session, g, c
 from pylons.i18n import _
 
 from formed.instance.backends.postgres import DBFactory as InstanceFactory
+from formed.instance.backends.common import RuleEvalContext
+from formed.instance.tree import InstanceLeaf, PageInstanceNode, RepeatInstanceContainerNode 
 
 from mpulsweb.lib.db import PostgresDBInterface
 
 
 # name of the dbfield for phase
 PHASEFIELD = "phase"
+# name of flags
+REQUIRED = re.compile('required')
+PHASEDATE = re.compile('time')
 
-
 log = logging.getLogger(__name__)
 
-
 CASE_NOT_CONSISTENT = """At least the phase '%s' is not in a consistent state
 and seems to have some missing required fields."""
 
+def walk_it(node, type=None):
+    items = []
+    items.append(node)
+    if not isinstance(node, InstanceLeaf):
+        for c in node.getChildren():
+            items.extend(walk_it(c))
+    return items
 
+def get_flags(flags):
+    result = {'required': [], 'time': []}
+    if not flags: return result
+    ft = flags.split(';')
+    for f in ft:
+        for p in f.split(':')[1].split(','):
+            id = p.split('_')[1]
+            if REQUIRED.match(f):
+                result['required'].append(id)
+            if PHASEDATE.match(f):
+                result['time'].append(id)
+    return result
+
 class ConsistenceCheckException(Exception):
 
     def __init__(self, value):
@@ -117,7 +141,6 @@
     def isRunning(self):
         return self.start.is_active()
 
-
 class PhasePart:
 
     def __init__(self, id, phase, it):
@@ -126,32 +149,35 @@
         self.active = False
         self.date = None
         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))
 
+        if not it: return
+        formed = it.getFormEd()
+
         # Check if the current phasepart is active
-        if it:
-            case_id = it.getRootNode().getIdentifier()
-            realid = "%s:%s" % (PHASEFIELD, case_id)
-            field = it.getItem(realid)
-            self.active = field.getValue() == int(self.id)
+        case_id = it.getRootNode().getIdentifier()
+        realid = "%s:%s" % (PHASEFIELD, case_id)
+        field = it.getItem(realid)
+        self.active = field.getValue() == int(self.id)
 
-            # Get date on which this phasepart was started
-            pdates = g.mpuls_config.get('phases', 'dates')
-            for dates in pdates:
-                for phasepart, field in dates.iteritems():
-                    if phasepart == id:
-                        realid = "%s:%s" % (field, case_id)
-                        field = it.getItem(realid)
-                        self.date = field.getValue()
+        for p in walk_it(it.getRootNode()):
+            meta = p.getMeta()
+            if isinstance(p, PageInstanceNode):
+                page = meta.getName()
+            flags = get_flags(meta.getFlags())
 
+            # Check if this fields is a phase relvant required field 
+            name = meta.getName()
+            if id in flags['required']:
+                rules = [rule for rule in formed.name2markingrules.get(name, ()) 
+                         if rule.isWarning()]
+                self.fields.append(Field(name, 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()
+
     def getLinks(self):
         out = []
         if len(self.fields) > 0:
@@ -204,13 +230,12 @@
         case_id = self.it.getRootNode().getIdentifier()
         real_id = "%s:%s" % (self.name, case_id)
         field = self.it.getItem(real_id)
-        if field:
+        # TODO: Fix handling of RG
+        if field and not isinstance(field, RepeatInstanceContainerNode):
             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))
+            for rule in rules:
+                self.conditions.append(Condition(field, rule, it))
         else:
             log.warning('Can not find field %s' % real_id)
 
@@ -233,10 +258,10 @@
 
 class Condition:
 
-    def __init__(self, field, expr, msg):
+    def __init__(self, field, rule, it):
         self.field = field
-        self.expr = expr
-        self.msg = msg
+        self.rule= rule 
+        self.it = it 
 
     def is_ok(self):
         '''Returns True if expr is valid for the current value in field
@@ -244,11 +269,12 @@
         return self.evaluate()
 
     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
+        id = self.field.getIdentifier()
+        context = RuleEvalContext(self.it, id)
+        if not self.it.rule_available(self.rule, context): return True
+        for result, variables in self.rule.getExpr().evaluate_instances(context):
+            if not result and id in variables:
+                return False
+            return True
 
 # vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:



More information about the Mpuls-commits mailing list