[Mpuls-commits] r1033 - wasko/branches/2.0/waskaweb/model
scm-commit@wald.intevation.org
scm-commit at wald.intevation.org
Mon Feb 1 10:59:06 CET 2010
Author: torsten
Date: 2010-02-01 10:59:05 +0100 (Mon, 01 Feb 2010)
New Revision: 1033
Modified:
wasko/branches/2.0/waskaweb/model/casedocument.py
Log:
* waskaweb/model/casedocument.py (CaseDocument): Deleted. Class is not
referenced anymore. It was initialised as g.formedTree in older
versions which is not replaced by the new formedlib.
Modified: wasko/branches/2.0/waskaweb/model/casedocument.py
===================================================================
--- wasko/branches/2.0/waskaweb/model/casedocument.py 2010-01-29 18:09:13 UTC (rev 1032)
+++ wasko/branches/2.0/waskaweb/model/casedocument.py 2010-02-01 09:59:05 UTC (rev 1033)
@@ -92,453 +92,4 @@
return values, errors
-
-class CaseDocument(Document):
-
- def __init__(self, root=None):
- Document.__init__(self, root)
-
- self.all_rules = list(self.findAllByClass(RuleLeaf))
-
- collector = WidgetCollector()
- self.visit(collector.visitor)
-
- self.buildNameToWidget(collector)
- self.buildNameToDefault(collector)
- self.buildWidgetsToPages(collector)
- self.buildVarToRules()
- self.buildPhases(collector)
-
- def buildNameToWidget(self, collector):
- self.widgets = dict([
- (w.getName(), w)
- for w in collector.widgets if w.getName()])
-
- def buildNameToDefault(self, collector):
- self.name2default = dict([
- (w.getName(), w.findDefault())
- for w in collector.widgets if w.getName()])
-
- def buildWidgetsToPages(self, collector):
- widgets2pages = {}
- for widget in collector.widgets:
- page = widget
- while page and not isinstance(page, PageNode):
- page = page.parent
- if page:
- widgets2pages[widget.getName()] = page.getName()
-
- self.widgets2pages = widgets2pages
-
- def buildVarToRules(self):
- var2rules = {}
- for rule in self.all_rules:
- expr = rule.getExpr()
- vars = expr.getDependencies()
- for var in vars:
- var2rules.setdefault(var, []).append(rule)
- self.var2rules = var2rules
-
- def buildPhases(self, collector):
-
- all, all_times = [], []
-
- phases, times = {}, {}
-
- for nc in collector.widgets:
- flags = nc.getFlags()
- if flags is None: continue
- for pair in flags.split(';'):
- pair = pair.strip()
- if not pair: continue
- p = [p.strip() for p in pair.split(':')]
- if len(p) > 1:
- kind, phase = p[0], p[1]
- if kind == "required":
- for p in [p.strip() for p in phase.split(',')]:
- if p: phases.setdefault(p, []).append(nc)
- elif kind == "time":
- for p in [p.strip() for p in phase.split(',')]:
- if p: times.setdefault(p, []).append(nc)
- else:
- # TODO: Fix formed tree
- if p[0] == "required":
- all.append(nc)
- elif p[0] == "time":
- all_times.append(nc)
-
- 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)
-
- self.phases_vars = phases
- self.time_vars = times
-
- def getPhaseVars(self, phase):
- return self.phases_vars.get(phase)
-
- def getTimeVars(self, phase):
- return self.time_vars.get(phase)
-
- def getRulesForVariable(self, var):
- return self.var2rules.get(var)
-
- def getPageStore(self, name, ds_id, no_data=False):
-
- if no_data: # XXX: urcan mode!
- db_view = "%s_view" % name.replace('-', '_')
- nc = self.findByClassAndName(PageNode, name)
- return EmptyPageStore(db_view, nc, ds_id)
-
- return page_cache.getPageStore(self, name, ds_id)
-
- def getPageOfWidget(self, name):
- return self.widgets2pages[name]
-
- def getData(self, name, ds_id = None):
- if ds_id is None:
- case = session.get('case')
- if not case: return None
- ds_id = case.id
- page_name = self.widgets2pages.get(name)
- if not page_name: return None
- page_store = self.getPageStore(page_name, ds_id)
- return page_store.getData(name)
-
- def getPhasePairs(self, current_phase, navigation = None, link = None):
- return ()
-
- def getAllRules(self):
- return self.all_rules
-
- def globalWarnings(self, ds_id):
- warnings = {}
- for r in self.all_rules:
- mark, expr = r.getMark(), r.getExpr()
- if not mark or not expr: continue
- # notices only
- mark = NOTICE.search(mark)
- if not mark: continue
- mark = mark.group(1)
-
- isNull = False
- params = {}
- for n in expr.getDependencies():
- v = self.getData(n, ds_id)
- if v is None: isNull = True; break
- params[n] = v
-
- if not isNull:
- okay = True
- try:
- okay = expr.evaluate(params)
- except:
- okay = False
-
- if not okay:
- try:
- t = warnings[mark]
- except KeyError:
- t = (self.widgets[mark], [])
- warnings[mark] = t
- t[1].append(r.getValue())
- return warnings
-
- def linksToGlobalWarnings(self, ds_id, navigation, link):
- warnings = self.globalWarnings(ds_id)
- if not warnings: return u""
-
- out = [
- u'<h1><a name="warning_list">%s</a></h1>' \
- u'<ul>' % (len(warnings) > 1 and u"Hinweise" or u"Hinweis")]
-
- for k, v in warnings.iteritems():
- try:
- page_name = self.widgets2pages[k]
- except KeyError:
- continue
- tree_item = navigation.findTreeItemByPageName(page_name)
- if not tree_item: continue
- tree_item = tree_item.deepest_item()
- link_text = link(tree_item, u"#f_" + k)
- messages = escape(u", ".join(v[1]))
- desc = escape(v[0].getDescription())
- out.append(
- u'<li>%s: <a href=%s class="required_missing" '
- u'title="Der Wert des Feldes hat ggf. unbeabsichtigte Auswirkungen.">'
- u'%s</a></li>' % (
- messages, link_text, desc))
-
- out.append(u"</ul>")
-
- return u"".join(out)
-
-
- def warningsOnPage(self, page, ds_id):
- """ Generates a dictionary of warnings that should be displayed
- on a given page.
- """
- # widgets on page
- used_fields = set([
- w.getName() for w in page.allWidgets() if w.getName()])
-
- warnings = {}
-
- for r in self.all_rules:
- mark, expr = r.getMark(), r.getExpr()
- if not mark or not expr: continue
- # warnings only
- mark = WARNING_NOTICE.search(mark)
- if not mark: continue
- mark = mark.group(1)
- # only mark if field is on current page
- if not mark in used_fields: continue
-
- try:
- okay = expr.evaluate(dict([
- (n, self.getData(n, ds_id)) for n in expr.getDependencies()]))
- except:
- #print_exc(file=sys.stderr)
- okay = False
-
- if not okay:
- try:
- t = warnings[mark]
- except KeyError:
- t = (self.widgets[mark], [])
- warnings[mark] = t
- t[1].append(r.getValue())
-
- return warnings
-
- def evaluate(self, expr):
- if not expr: return False
- try:
- okay = expr.evaluate(dict([
- (n, self.getData(n)) for n in expr.getDependencies()]))
- except:
- okay = False
- return okay
-
- def dryConvertAgainstDefaults(self, params):
-
- not_converted = []
-
- converted, delete_vars = {}, []
-
- for k, v in params.iteritems():
- if v == "": # no value -> delete it
- if self.name2default.get(k) != None: # NULL already?
- delete_vars.append(k)
- else:
- widget = self.widgets[k]
- try:
- nv = checkAndConvert(widget, v)
- if nv != self.name2default.get(k): # only if it's a change
- converted[k] = nv
- except SematicError, inst:
- not_converted.append((key, v, inst.value))
-
- if not_converted:
- raise DataSaveException(
- "Konnte Daten nicht konvertieren", not_converted)
-
- # second step: simulate to apply the data
- simulation = dict([(n, None) for n in delete_vars])
-
- def getValue(n):
- try:
- return simulation[n]
- except KeyError:
- return self.name2default.get(n)
-
- for k, v in converted.iteritems():
- try:
- rules = self.var2rules[k]
- except KeyError: # no rules
- simulation[k] = v
- continue
-
- simulation_succeed = True
- for rule in rules:
- mark = rule.getMark()
- if mark and mark.find("warning:") >= 0 or mark.find("notice:") >= 0:
- continue
- expr = rule.getExpr()
- if not expr: continue
- vars = {}
- isNull = False
- for dep in expr.getDependencies():
- if dep != k:
- ov = getValue(dep)
- if ov is None: isNull = True; break
- vars[dep] = ov
-
- if not isNull:
- vars[k] = v
- try:
- okay = expr.evaluate(vars)
- except:
- okay = False
- if not okay:
- simulation_succeed = False
- not_converted.append((k, params[k], rule.getValue()))
-
- if simulation_succeed:
- simulation[k] = v
-
- if not_converted:
- raise DataSaveException(
- "Datenkonsistenz nicht eingehalten", not_converted)
-
- return simulation
-
-
- def setDataNoChecks(self, converted, ds_id = None):
- pages = {}
- for kv in converted.iteritems():
- try:
- pages.setdefault(self.widgets2pages[kv[0]], []).append(kv)
- except KeyError:
- print >> sys.stderr, "cannot find page for '%s'" % repr(kv[0])
- continue
-
- if ds_id is None:
- case = session.get('case')
- if not case: return None
- ds_id = case.id
-
- for page_name, params in pages.iteritems():
- if not params: continue
- ps = self.getPageStore(page_name, ds_id)
- for k, v in params:
- ps.setData(k, v)
- ps.store()
-
-
- def setData(self, params, ds_id = None, do_write = True):
-
- orig_params = params
-
- # assign params to their pages
- param_pages = {}
-
- for k, v in params.iteritems():
- try:
- page_name = self.widgets2pages[k]
- except KeyError:
- print >> sys.stderr, "cannot find page for '%s'" % repr(k)
- continue
-
- try:
- param_page = param_pages[page_name]
- except KeyError:
- param_page = {}
- param_pages[page_name] = param_page
-
- param_page[k] = v
-
- not_converted = []
-
- # first step: try to convert the incoming data
- if ds_id is None:
- case = session.get('case')
- if not case: return None
- ds_id = case.id
-
- for page_name, params in param_pages.items():
- ps = self.getPageStore(page_name, ds_id)
-
- converted, delete_vars = {}, []
- for k, v in params.iteritems():
- if v == "": # no value -> delete it
- if ps.getData(k) != None: # NULL already?
- delete_vars.append(k)
- else:
- widget = self.widgets[k]
- try:
- nv = checkAndConvert(widget, v)
- if nv != ps.getData(k): # only if it's a change
- converted[k] = nv
- except SematicError, inst:
- not_converted.append((k, v, inst.value))
-
- param_pages[page_name] = (converted, delete_vars)
-
- if not_converted:
- raise DataSaveException(
- "Konnte Daten nicht konvertieren", not_converted)
-
- # second step: simulate to apply the data
- for page_name, params in param_pages.items():
- params, delete_vars = params
- simulation = dict([(n, None) for n in delete_vars])
-
- def getValue(n):
- try:
- return simulation[n]
- except KeyError:
- return self.getData(n)
-
- for k, v in params.iteritems():
- try:
- rules = self.var2rules[k]
- except KeyError: # no rules
- simulation[k] = v
- continue
-
- simulation_succeed = True
- for rule in rules:
- mark = rule.getMark()
- if mark and mark.find("warning:") >= 0 or mark.find("notice:") >= 0:
- continue
- expr = rule.getExpr()
- if not expr: continue
- vars = {}
- isNull = False
- for dep in expr.getDependencies():
- if dep != k:
- ov = getValue(dep)
- if ov is None: isNull = True; break
- vars[dep] = ov
-
- if not isNull:
- vars[k] = v
- try:
- okay = expr.evaluate(vars)
- except:
- okay = False
- if not okay:
- simulation_succeed = False
- not_converted.append((k, orig_params[k], rule.getValue()))
-
- if simulation_succeed:
- simulation[k] = v
-
- # only write back the minimal changeset
- param_pages[page_name] = simulation
-
- if not_converted:
- raise DataSaveException(
- "Datenkonsistenz nicht eingehalten", not_converted)
-
- if do_write:
- # third step: write back the changes:
- for page_name, params in param_pages.iteritems():
- if not params: continue
- ps = self.getPageStore(page_name, ds_id)
- for k, v in params.iteritems():
- ps.setData(k, v)
- ps.store()
-
- def getDescription(self, name):
- try:
- return self.widgets[name].getDescription()
- except KeyError:
- return name
-
# vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
More information about the Mpuls-commits
mailing list