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

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Fri Feb 5 18:03:34 CET 2010


Author: bh
Date: 2010-02-05 18:03:34 +0100 (Fri, 05 Feb 2010)
New Revision: 1287

Modified:
   wasko/branches/2.0/ChangeLog
   wasko/branches/2.0/waskaweb/model/case.py
Log:
* waskaweb/model/case.py (AliasDescription): New class to help
describe the case attriute aliases.
(Case.aliases): Convert into a list of AliasDescription instances.
(Case.alias_dict): New.  Dict mapping attribute names to
AliasDescription instances.  Automatically built from aliases.
(Case.__getattr__, Case.get_preset_fields): Adapt to new alias
descriptions.


Modified: wasko/branches/2.0/ChangeLog
===================================================================
--- wasko/branches/2.0/ChangeLog	2010-02-05 16:59:49 UTC (rev 1286)
+++ wasko/branches/2.0/ChangeLog	2010-02-05 17:03:34 UTC (rev 1287)
@@ -1,5 +1,15 @@
 2010-02-05  Bernhard Herzog  <bh at intevation.de>
 
+	* waskaweb/model/case.py (AliasDescription): New class to help
+	describe the case attriute aliases.
+	(Case.aliases): Convert into a list of AliasDescription instances.
+	(Case.alias_dict): New.  Dict mapping attribute names to
+	AliasDescription instances.  Automatically built from aliases.
+	(Case.__getattr__, Case.get_preset_fields): Adapt to new alias
+	descriptions.
+
+2010-02-05  Bernhard Herzog  <bh at intevation.de>
+
 	* mpulsweb/lib/security.py (checkLogin): Move the import of
 	SessionUser, SessionSuperAdmin toward the beginning of the
 	function outside of any try/except blocks so that import errors

Modified: wasko/branches/2.0/waskaweb/model/case.py
===================================================================
--- wasko/branches/2.0/waskaweb/model/case.py	2010-02-05 16:59:49 UTC (rev 1286)
+++ wasko/branches/2.0/waskaweb/model/case.py	2010-02-05 17:03:34 UTC (rev 1287)
@@ -619,27 +619,55 @@
         return True
 
 
+class AliasDescription:
+
+    """Describes one alias for a public case and SessionCase attribute.
+
+    The public instance variables are
+
+       name -- The name of the case attribute
+       formedname -- The name used in the formed description
+       default -- The default value (default None)
+       converter -- A callable to convert a string representation to the
+                    correct python value.  Default None, which means no
+                    conversion will be done.
+    """
+
+    def __init__(self, name, formedname, default=None, converter=None):
+        self.name = name
+        self.formedname = formedname
+        self.default = default
+        self.converter = converter
+
+    def convert(self, value):
+        """Convert value using self.convert.
+        If self.convert is None, return value as is.
+        """
+        if self.converter is not None:
+            value = self.converter(value)
+        return value
+
+
 class Case:
 
     # 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))
+    # This is a list of AliasDescription instances.
+    aliases = [AliasDescription("knr", "fn",  "",  ensure_unicode),
+               AliasDescription("last_name", "name",  "",  ensure_unicode),
+               AliasDescription("first_name", "vorname",  "",  ensure_unicode),
+               AliasDescription("street", "addresse_strasse",  "",
+                                ensure_unicode),
+               AliasDescription("streetnr", "addresse_strassenr",  "",
+                                ensure_unicode),
+               AliasDescription("plz", "addresse_plz",  "",  ensure_unicode),
+               AliasDescription("city", "addresse_ort",  "",  ensure_unicode),
+               AliasDescription("first_meeting", "erstgespraech"),
+               AliasDescription("cm_end", "datum_cm_ende"),
+               AliasDescription("editor", "bearbeiter_id")]
 
+    alias_dict = dict((desc.name, desc) for desc in aliases)
+
     def __init__(self, id=None, state=None, preset=None):
         self.id = id
         self.state = state
@@ -656,16 +684,13 @@
 
     def __getattr__(self, name):
         """Provide attribute like access to some fields for compatibility"""
-        alias = self.aliases.get(name)
-        if alias:
-            formedname, default, convert = alias
+        desc = self.alias_dict.get(name)
+        if desc:
             try:
-                value = self.get_value(formedname)
+                value = self.get_value(desc.formedname)
             except KeyError:
-                value = default
-            if convert:
-                value = convert(value)
-            return value
+                value = desc.default
+            return desc.convert(value)
         raise AttributeError(name)
 
     @classmethod
@@ -674,7 +699,7 @@
         The list is intended to be used when populating the preset
         mapping passed to the constructor.
         """
-        return [value[0] for value in cls.aliases.itervalues()]
+        return [item.name for item in cls.aliases]
 
     def get_value(self, name, **kw):
         """Return the value of the formed field given by name.



More information about the Mpuls-commits mailing list