[Mpuls-commits] r4432 - base/trunk/mpulsweb/model

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Tue Dec 28 11:19:54 CET 2010


Author: torsten
Date: 2010-12-28 11:19:54 +0100 (Tue, 28 Dec 2010)
New Revision: 4432

Added:
   base/trunk/mpulsweb/model/agency.py
Log:
* mpulsweb/model/agency.py (MpulsAgency): New classes for agencys in
	evaluation server. Class is very common to the MpulsCase class, but is
	hopfully reduced now to the minimum required functionality.



Added: base/trunk/mpulsweb/model/agency.py
===================================================================
--- base/trunk/mpulsweb/model/agency.py	2010-12-28 10:11:49 UTC (rev 4431)
+++ base/trunk/mpulsweb/model/agency.py	2010-12-28 10:19:54 UTC (rev 4432)
@@ -0,0 +1,172 @@
+# -*- coding: utf-8 -*-
+#
+# Copyright 2007, 2008, 2010 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:
+# Torsten Irländer <torsten.irlaender at intevation.de>
+#
+
+import logging
+from mpulsweb.lib.base import g 
+from mpulsweb.model.case import Field 
+
+log = logging.getLogger(__name__)
+
+class MpulsAgency:
+
+    def __init__(self, id=None, preset=None, count=None):
+        self.id = id
+        if preset is None:
+            preset = dict()
+        self.preset = preset
+        self.count = count 
+
+    @classmethod
+    def set_fields(cls, raw_fields):
+        fields = [Field(**kw) for kw in raw_fields]
+        cls.fields = fields
+        cls.alias_dict = dict((field.alias, field) for field in fields
+                                                   if field.alias is not None)
+
+    @classmethod
+    def get_preset_fields(cls):
+        """Return a list with the names of formed fields that can be preset.
+        The list is intended to be used when populating the preset
+        mapping passed to the constructor.
+        """
+        return [field.name for field in cls.fields if field.alias is not None]
+
+    @classmethod
+    def get_preload_fields(cls):
+        """Return a list with the names of the fields to preload."""
+        return [field.name for field in cls.fields if field.preload]
+
+
+    @classmethod
+    def get_overview_fields(cls):
+        """Return a list fields to be displayed in overviewpage."""
+        return [field for field in cls.fields if field.overview]
+
+    @classmethod
+    def get_search_retrieve_fields(cls):
+        """Return a list with all names of fields to retrieve from the search"""
+        return [field.name for field in cls.fields if field.search_retrieve]
+
+    @classmethod
+    def get_search_match_fields(cls):
+        """Return a list with all names of fields to match in the search"""
+        return [field.name for field in cls.fields if field.search_match]
+
+    def __getattr__(self, name):
+        """Provide attribute like access to some fields for compatibility"""
+        field = self.alias_dict.get(name)
+        log.debug('accessing field %r using name %r' % (field, name))
+        if field:
+            try:
+                value = self.get_value(field.name)
+            except KeyError:
+                value = field.default
+            return field.convert(value)
+        raise AttributeError(name)
+
+    def get_value(self, name, **kw):
+        """Return the value of the formed field given by name.
+        If the formed instance of the case has not yet been loaded, the
+        value is taken from the preset dictionary the case was
+        instantiated with.  If it's not in the preset dictionary, the
+        formed tree is loaded.  Once the formed instance has been
+        loaded, the value will always be taken from there first. Only if this
+        does not work try to lookup the value in the present dictionary
+
+        This behavior is intended as an optimization so that the case
+        can be instantiated from e.g. a search result which returns the
+        commonly used fields without any further database queries.  The
+        other fields will be loaded on demand when they're actually
+        needed.  Once the formed instance has been created it takes
+        precedence over the preset fields.
+        """
+        # if the instance tree has not yet been loaded, look in preset
+        if name in self.preset:
+            return self.preset[name]
+        return None 
+
+class MpulsAgencyFactory:
+
+    """Factory for case object. This factory provides methods to either load
+    existing cases from db or create new cases"""
+
+    def __init__(self, cls):
+        self.case_cls = cls 
+
+    def _build_preset(self, result):
+        return dict((name, result[name])
+                    for name in self.case_cls.get_preset_fields()
+                    if result.has_key(name))
+
+    def get_search_retrieve_fields(self):
+        """Frontend for the same method of the factory's case class"""
+        return self.case_cls.get_search_retrieve_fields()
+
+    def get_search_match_fields(self):
+        """Frontend for the same method of the factory's case class"""
+        return self.case_cls.get_search_match_fields()
+
+    def loadFromQueryResult(self, result):
+        """Create a Case object from a DB query result row.
+
+        The result should be a dict-like object mapping the names used
+        in the formed tree to the values.  Which fields can be used is
+        determined by the list returned by the Case's get_preset_fields
+        method.  In addition to those fields, the row must contain the
+        following fields that will be used to instantiate the State
+        object: s_id, status, zugriff
+        """
+        return self.case_cls(result["fkz"], preset=self._build_preset(result), count=result["count"])
+
+    def _initNewCase(self, id):
+        """Additional initialization of the new case.
+        The base-class method does nothing.  Derived classes should
+        override this method if necessary.
+        """
+        pass
+
+class MpulsAgencyOverview:
+
+    def __init__(self):
+        self.ds_list = []
+
+    def search(self, search_str=None):
+        self.ds_list = []
+        result = g.eval_search.perform(search_str)
+        factory = MpulsAgencyFactory(g.agency) 
+        for row in result:
+            case = factory.loadFromQueryResult(row)
+            self.ds_list.append(case)
+        return self.ds_list
+
+    def numDatasets(self):
+        return len(self.ds_list)
+
+    def getDatasets(self):
+        return self.ds_list



More information about the Mpuls-commits mailing list