[Mpuls-commits] r1163 - in wasko/branches/2.0: . waskaweb/model

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Tue Feb 2 17:13:55 CET 2010


Author: bh
Date: 2010-02-02 17:13:51 +0100 (Tue, 02 Feb 2010)
New Revision: 1163

Modified:
   wasko/branches/2.0/ChangeLog
   wasko/branches/2.0/waskaweb/model/case.py
Log:
* waskaweb/model/case.py (Case.__init__): Add some parameters so
that the case can be instantiated without having to assign to
instance variables.  This includes a parameter "preset" that can
contain a subset of the case's fields.
(Case.aliases): New class attribute describing which fields can be
usefully preset and how they accessed by public instance
variables.  This mainly intended for compatibility with code that
accesses these fields in the Case or CaseDigest instances.
(Case.__getattr__): New.  Provide attribute like access to the
values described in aliases.
(Case.get_value, Case.get_description): New.  Get a
value/description of the formed field.  The value is taken from
the preset dictionary the case was instantiated with if the formed
instance has not yet been loaded.


Modified: wasko/branches/2.0/ChangeLog
===================================================================
--- wasko/branches/2.0/ChangeLog	2010-02-02 15:59:59 UTC (rev 1162)
+++ wasko/branches/2.0/ChangeLog	2010-02-02 16:13:51 UTC (rev 1163)
@@ -1,3 +1,20 @@
+2010-02-02  Bernhard Herzog  <bh at intevation.de>
+
+	* waskaweb/model/case.py (Case.__init__): Add some parameters so
+	that the case can be instantiated without having to assign to
+	instance variables.  This includes a parameter "preset" that can
+	contain a subset of the case's fields.
+	(Case.aliases): New class attribute describing which fields can be
+	usefully preset and how they accessed by public instance
+	variables.  This mainly intended for compatibility with code that
+	accesses these fields in the Case or CaseDigest instances.
+	(Case.__getattr__): New.  Provide attribute like access to the
+	values described in aliases.
+	(Case.get_value, Case.get_description): New.  Get a
+	value/description of the formed field.  The value is taken from
+	the preset dictionary the case was instantiated with if the formed
+	instance has not yet been loaded.
+
 2010-02-02  Torsten Irländer <torsten.irlaender at intevation.de>
 
 	* waskaweb/templates/appointments/appointment_form.mako: Deleted. not

Modified: wasko/branches/2.0/waskaweb/model/case.py
===================================================================
--- wasko/branches/2.0/waskaweb/model/case.py	2010-02-02 15:59:59 UTC (rev 1162)
+++ wasko/branches/2.0/waskaweb/model/case.py	2010-02-02 16:13:51 UTC (rev 1163)
@@ -768,23 +768,106 @@
 
 class Case:
 
-    def __init__(self):
-        self.id            = None
-        self.first_name    = None
-        self.last_name     = None
-        self.knr           = None
-        self.editor        = None
-        self.standin       = None
-        self.state         = None
-        self.first_meeting = None
-        self.cm_end        = None
+    # Definitions for some commonly used public instance attributes.
+    #
+    # This is a dictionary mapping the name of the instance attribute to
+    # a tuple of the form (FORMED_NAME, DEFAULT, CONVERTER), where
+    # FORMED_NAME is the name used in the formed tree, DEFAULT is the
+    # default value to use if no value has been set and CONVERTER is a
+    # callable that to convert the value.  CONVERTER may be None, in
+    # which case no coversion will be done.
+    aliases = dict(knr=("fn", "", ensure_unicode),
+                   last_name=("name", "", ensure_unicode),
+                   first_name=("vorname", "", ensure_unicode),
+                   street=("addresse_strasse", "", ensure_unicode),
+                   streetnr=("addresse_strassenr", "", ensure_unicode),
+                   plz=("addresse_plz", "", ensure_unicode),
+                   city=("addresse_ort", "", ensure_unicode),
+                   first_meeting=("erstgespraech", None, None),
+                   cm_end=("datum_cm_ende", None, None),
+                   editor=("bearbeiter_id", None, None))
 
-        self.digest                   = None
-        self.documents                = []
-        self.appointments             = []
-        self.privacy_statement        = None
-        self.discretion_statement     = None
+    def __init__(self, id=None, state=None, preset=None):
+        self.id = id
+        self.state = state
+        self.standin = None
+        self.digest = None
+        self.documents = []
+        self.appointments = []
+        self.privacy_statement = None
+        self.discretion_statement = None
+        if preset is None:
+            preset = dict()
+        self.preset = preset
+        self.formed_instance = None
 
+    def __getattr__(self, name):
+        """Provide attribute like access to some fields for compatibility"""
+        alias = self.aliases.get(name)
+        if alias:
+            formedname, default, convert = alias
+            try:
+                value = self.get_value(formedname)
+            except KeyError:
+                value = default
+            if convert:
+                value = convert(value)
+            return 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.
+
+        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.
+        """
+        # always use formed instance tree if available
+        if self.formed_instance is not None:
+            item = self._get_formed_item(name)
+            if item:
+                return item.getValue()
+            else:
+                raise KeyError("Cannot find formed item for %r" % name)
+
+        # if the instance tree has not yet been loaded, look in preset
+        if name in self.preset:
+            return self.preset[name]
+
+        # otherwise actually load the formed instance tree and repeat
+        # We should only get here if self.formed_instance is not set yet.
+        assert self.formed_instance is None
+        self._load_formed_instance_tree()
+        return self.get_value(name, **kw)
+
+    def get_description(self, name):
+        """Return the description of the formed field given by name"""
+        if self.formed_instance is None:
+            self._load_formed_instance_tree()
+        item = self._get_formed_item(name)
+        if item:
+            return item.getMeta().getDescription()
+        else:
+            raise KeyError("Cannot find formed item for %r" % name)
+
+    def _load_formed_instance_tree(self):
+        factory = InstanceFactory(g.formedTree, PostgresDBInterface())
+        self.formed_instance = factory.loadInstanceTreeByIdentifier(self.id)
+
+    def _get_formed_item(self, name):
+        fid = get_field_identifier(self.id, name, self.formed_instance)
+        if fid:
+            return self.formed_instance.getItem(fid)
+        return None
+
     def delete(self):
         """Deletes the case from data base. Returns True if deletion succseeds
         """



More information about the Mpuls-commits mailing list