[Mpuls-commits] r1041 - wasko/branches/2.0/waskaweb/model
scm-commit@wald.intevation.org
scm-commit at wald.intevation.org
Mon Feb 1 11:27:33 CET 2010
Author: torsten
Date: 2010-02-01 11:27:32 +0100 (Mon, 01 Feb 2010)
New Revision: 1041
Removed:
wasko/branches/2.0/waskaweb/model/datapage.py
Log:
Deleted. Not imported anywhere now
Deleted: wasko/branches/2.0/waskaweb/model/datapage.py
===================================================================
--- wasko/branches/2.0/waskaweb/model/datapage.py 2010-02-01 10:25:15 UTC (rev 1040)
+++ wasko/branches/2.0/waskaweb/model/datapage.py 2010-02-01 10:27:32 UTC (rev 1041)
@@ -1,333 +0,0 @@
-# -*- coding: utf-8 -*-
-#
-# Copyright 2007, 2008 Intevation GmbH, Germany, <info at intevation.de>
-#
-# This file is part of mpuls WASKA (CoMPUter-based case fiLeS -
-# Web-Anwendungs-Server fuer Kompetenzagenturen).
-#
-# mpuls WASKA is free software: you can redistribute it and/or modify it under
-# the terms of the GNU Affero General Public License as published by the
-# Free Software Foundation, either version 3 of the License, or (at your
-# option) any later version.
-#
-# mpuls WASKA is distributed in the hope that it will be useful, but WITHOUT
-# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
-# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
-# License for more details.
-#
-# You should have received a copy of the GNU Affero General Public
-# License along with mpuls WASKA. If not, see <http://www.gnu.org/licenses/>.
-#
-# mpuls WASKA has been developed on behalf of the
-# Projekttraeger im Deutschen Zentrum fuer Luft- und Raumfahrt e.V. (PT-DLR)
-# within the programme Kompetenzagenturen (Durchfuehrungsphase) funded by
-# the Bundesministerium fuer Familie, Senioren, Frauen und Jugend and
-# European Social Fund resources.
-#
-# Authors:
-# Sascha L. Teichmann <teichmann at intevation.de>
-#
-
-from waskaweb.model.semantic import ErrorItem, SematicError, checkAndConvert
-from waskaweb.model.data import ConditionalWidgetCollector
-
-from waskaweb.lib.db import db
-
-import sys
-import traceback
-
-def convert(x):
- if isinstance(x, str): return unicode(x, 'utf-8')
- return x
-
-class PageStore:
- #def __init__(self, relation, widgets, id = None):
- def __init__(self, relation, nc, id):
- self.nc = nc
- self.items = None
- self.widgets = None
- self.data = None
- self.relation = relation.replace('-', '_')
- self.id = int(id)
-
- def getWidget(self):
- return self.nc
-
- def getId(self):
- return self.id
-
- def setId(self, id):
- self.id = id
-
- def getData(self, item):
- if self.data is None:
- self._fetchData()
- data = self.data
- if data is None: return None
- try:
- # TODO: Ugly! Better change the db scheme "ja, nein, keine angabe"
- # fields to int instead of boolean
- if isinstance(data[item][0], type(True)):
- return data[item][0] and 1 or 0
- return data[item][0]
- except KeyError:
- return None
-
- def setData(self, item, value):
- if self.data is None:
- self._fetchData()
- data = self.data
- if data is None: return
- try:
- v = data[item]
- # store only if different
- if v[0] != value:
- v[0] = value
- v[1] = True
- except KeyError:
- pass
-
- def store(self):
-
- self.__lazyCheck()
-
- data = self.data
- if data is None: return
- fields = {}
- keys = []
- for k, v in data.iteritems():
- if v[1]:
- keys.append("%s=%%(%s)s" % (k, k))
- fields[k]=v[0]
-
- if not fields:
- return
-
- fields['id'] = int(self.id)
-
- stmnt = "UPDATE %s SET %s WHERE id = %%(id)s" % (
- self.relation, ", ".join(keys))
-
- try:
- con, cur = None, None
- try:
- con = db.getConnection()
- cur = con.cursor()
- cur.execute(stmnt, fields)
- con.commit()
- finally:
- db.recycleConnection(con, cur)
-
- # mark as written
- for v in data.itervalues():
- v[1] = False
-
- except StandardError, err:
- traceback.print_exc(file=sys.stderr)
- print >> sys.stderr, "DB error: %s" % str(err)
-
- def __lazyCheck(self):
- if self.items is None:
- self.widgets = self.nc.allWidgets()
- self.items = [w.getName() for w in self.widgets]
-
- def _fetchData(self):
- self.__lazyCheck()
- try:
- fields = ", ".join(self.items)
- except:
- pass
-
- stmnt = "SELECT %s FROM %s WHERE id = %%(id)s;" % (
- fields, self.relation)
-
- #print >> sys.stderr, "select: %s" % stmnt
- try:
- con, cur = None, None
- try:
- con = db.getConnection()
- cur = con.cursor()
- cur.execute(stmnt, { 'id': int(self.id)})
- res = cur.fetchone()
- finally:
- db.recycleConnection(con, cur)
- if res:
- self.data = dict(
- zip(self.items, [[convert(item), False] for item in res]))
-
- except StandardError, err:
- traceback.print_exc(file=sys.stderr)
- print >> sys.stderr, "DB error: %s" % str(err)
-
- def save(self, params, old_errors, document):
-
- wc = ConditionalWidgetCollector(document.evaluate)
- self.nc.visit(wc.visitor)
-
- nwidgets = wc.widgets
- widgets_names = [w.getName() for w in wc.widgets]
- pageName = self.nc.getName()
-
- dbPage = self
-
- # widgets on current page
- widgets = dict([
- (widget.getName(), widget) for widget in nwidgets if widget.getName()])
-
- widgets_clone = widgets.copy()
-
- # empty parameters will nullify values
- delete_vars = []
-
- # vars that passed lexical and syntactic check
- to_be_set = {}
-
- for k, v in params.iteritems():
- if k.startswith("__"):
- continue
- try:
- value = v[0]
- widget = widgets.pop(k)
- #print "widget: %s" % repr(widget)
- if value == "": # no value -> delete it
- delete_vars.append(k)
- else:
- # Hopefully this one causes no trouble any more
- old_errors.pop(k, None)
- # the empty array is for the rules
- nv = checkAndConvert(widget, value)
- ov = self.getData(k)
- if nv != ov:
- to_be_set[k] = (nv, [])
- except SematicError, inst:
- ei = ErrorItem(pageName, value, widget.getDescription())
- ei.addMessage(inst.value)
- # overwrite old errors
- old_errors[k] = ei
- except KeyError, inst:
- pass
-
- # flag to indicate if we should flush the page
- dirty = False
-
- # remove variable from dataset which are set to ""
- for wname in widgets.keys() + delete_vars:
- dbPage.setData(wname, None)
- dirty = True
- # an unset value causes no problems.
- old_errors.pop(wname, None)
-
- # find all rules that depend on variables to be set.
- # build up a lookup var-name -> list of rules
- for r in document.all_rules:
- mark = r.getMark()
- # warning rules are evaluated at page rendering
- if mark and mark.find("warning:") >= 0 or mark.find("notice:") >= 0:
- #print "Ignore warning rule '%s'" % r.getName()
- continue
- expr = r.getExpr()
- if not expr: continue
- for var in expr.getDependencies():
- try:
- to_be_set[var][1].append(r)
- except KeyError:
- pass
-
- # set vars into db page and check rules.
- for k, tup in to_be_set.iteritems():
- v, rules = tup[0], tup[1]
- hasNoProblems = True
- for rule in rules:
- vars = {}
- isNull = False # only process rule if all values are not null
- for dependency in rule.getExpr().getDependencies():
- if dependency == k: # ignore because this comes from the web
- continue
- # XXX: Normally they are distributed over all kind of pages.
- value = document.getData(dependency)
- if value is None: isNull = True; break
- vars[dependency] = value
- if isNull:
- continue
- expr = rule.getExpr()
- # Now evaluate the rule to see if its violated
- try:
- vars[k] = v # test it with new value from web
- isOkay = expr.evaluate(vars)
- except:
- isOkay = False
- if not isOkay:
- hasNoProblems = False
- ie = old_errors.get(k, None)
- if ie is None:
- try:
- desc = widgets_clone[k].getDescription()
- except KeyError:
- desc = ""
- ie = ErrorItem(pageName, v, desc)
- old_errors[k] = ie
- msg = rule.getValue()
- if msg: ie.addMessage(msg)
- else: ie.addMessage("There is a problem with '%s'." % k)
- if hasNoProblems: # No new errors -> set it
- dbPage.setData(k, v)
- dirty = True
- # TODO: Solve transitive problems!
- # e.g. A was set. B contradicts A. That prevents B from
- # being set. -> B is marked was wrong input.
- # Now A is set to a value that does not contradicts B
- # any more. Therefore B is not an error input any more.
- # -> B can be applied. This scheme has to be applied
- # recursively along the dependencies.
- old_errors.pop(k, None)
-
- if dirty: # flush the db page
- dbPage.store()
-
- return old_errors
-
-class EmptyPageStore(PageStore):
- def __init__(self, relation, nc, id = None):
- PageStore.__init__(self, relation, nc, id)
-
- def _fetchData(self):
- pass
-
-class DataExtractor(object):
-
- def __init__(self):
- self.data = None
-
- def loadData(self, ds_id, keys):
- cur, con = None, None
-
- try:
- con = db.getConnection()
- cur = con.cursor()
- select = "SELECT %s FROM master_tbl_view WHERE id = %%(ds_id)s" % \
- ", ".join(keys)
- cur.execute(select, { 'ds_id': ds_id })
- row = cur.fetchone()
- if not row:
- raise Exception("No such data set: %d" % ds_id)
-
- self.data = dict(zip([d[0] for d in cur.description], row))
- finally:
- db.recycleConnection(con, cur)
-
- def storeData(self, ds_id):
-
- update = "UPDATE master_tbl_view SET %s WHERE id = %%(ds_id)s" % \
- ", ".join(["%s = %%(%s)s" % (n, n) for n in self.data.iterkeys()])
-
- try:
- con = db.getConnection()
- cur = con.cursor()
- values = self.data or {}
- values['ds_id'] = ds_id
- cur.execute(update, values)
- con.commit()
- finally:
- db.recycleConnection(con, cur)
-
-# vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:
More information about the Mpuls-commits
mailing list