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

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Thu Feb 11 19:30:25 CET 2010


Author: bh
Date: 2010-02-11 19:30:19 +0100 (Thu, 11 Feb 2010)
New Revision: 1521

Modified:
   wasko/branches/2.0/ChangeLog
   wasko/branches/2.0/waskaweb/model/case.py
Log:
* waskaweb/model/case.py (AliasDescription, Field): Replace
AliasDescription with the similar but more general Field.
(Case.aliases, Case.fields): Replace aliases with fields, which is
a list of Fields instances describing fields and not just aliases.
For now it doesn't do anything more than describing aliases,
though.
(Case.alias_dict): Generate from fields, not aliases.
(SessionCase.__init__, SessionCase.setData, Case.__getattr__)
(Case.get_preset_fields, Case.get_session_fields)
(Case.getSessionCase): Adapt to new Field objects.


Modified: wasko/branches/2.0/ChangeLog
===================================================================
--- wasko/branches/2.0/ChangeLog	2010-02-11 17:02:57 UTC (rev 1520)
+++ wasko/branches/2.0/ChangeLog	2010-02-11 18:30:19 UTC (rev 1521)
@@ -1,5 +1,18 @@
 2010-02-11  Bernhard Herzog  <bh at intevation.de>
 
+	* waskaweb/model/case.py (AliasDescription, Field): Replace
+	AliasDescription with the similar but more general Field.
+	(Case.aliases, Case.fields): Replace aliases with fields, which is
+	a list of Fields instances describing fields and not just aliases.
+	For now it doesn't do anything more than describing aliases,
+	though.
+	(Case.alias_dict): Generate from fields, not aliases.
+	(SessionCase.__init__, SessionCase.setData, Case.__getattr__)
+	(Case.get_preset_fields, Case.get_session_fields)
+	(Case.getSessionCase): Adapt to new Field objects.
+
+2010-02-11  Bernhard Herzog  <bh at intevation.de>
+
 	* waskaweb/model/case.py (SessionCase.toFormData): Make it work
 	with the actual return value of Case.get_session_fields()
 

Modified: wasko/branches/2.0/waskaweb/model/case.py
===================================================================
--- wasko/branches/2.0/waskaweb/model/case.py	2010-02-11 17:02:57 UTC (rev 1520)
+++ wasko/branches/2.0/waskaweb/model/case.py	2010-02-11 18:30:19 UTC (rev 1521)
@@ -559,8 +559,8 @@
         self.state = state
         self.privacy_statement = privacy_statement
 
-        for desc in Case.get_session_fields():
-            setattr(self, desc.name, kw.get(desc.name, desc.default))
+        for field in Case.get_session_fields():
+            setattr(self, field.alias, kw.get(field.alias, field.default))
 
     def getState(self):
         return self.state
@@ -575,10 +575,10 @@
         return self.privacy_statement
 
     def setData(self, params):
-        for desc in Case.get_session_fields():
-            setattr(self, desc.name, desc.convert(save_getone(params,
-                                                              desc.formedname,
-                                                              desc.default)))
+        for field in Case.get_session_fields():
+            setattr(self, field.alias, desc.convert(save_getone(params,
+                                                                field.name,
+                                                                field.default)))
 
     def toFormData(self):
         data = {}
@@ -603,36 +603,60 @@
         return True
 
 
-class AliasDescription:
+class Field(object):
 
-    """Describes one alias for a public case and SessionCase attribute.
+    """Class providing extra information about an attribute of the case.
 
-    The public instance variables are
+    The constructor parameters and 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.
-       in_session -- A boolean indicating whether this field should be
-                    available on the SessionCase object too.  Default False.
+       name -- The name of the attribute.  This should be the name used
+               in the master table in the database which usually means
+               that it's the name in the formed meta tree (but not in a
+               repeat group).
+               The value of the field can be fetched with the case
+               object's get_value method
+
+       alias -- Additional name under which the attribute can be
+               accessed directly as an attribute of the case object.
+               Default is None, which means that the field is not
+               available under an alias.
+
+       alias_force_string -- If true (default), the value will be forced
+               to be a unicode string using the ensure_unicode function
+               when it is accessed through the alias name.
+               This setting only has an affect if alias is not None.
+
+       session -- If true (default false) the field is available with
+               the alias name from the SessionCase object too.
+               This setting only has an affect if alias is not None.
+
     """
 
-    def __init__(self, name, formedname, default=None, converter=None,
-                 in_session=False):
+
+    def __init__(self, name, session=False, alias=None,
+                 alias_force_string=True):
         self.name = name
-        self.formedname = formedname
-        self.default = default
-        self.converter = converter
-        self.in_session = in_session
+        self.session = session
+        self.alias = alias
+        self.alias_force_string = alias_force_string
 
+    def get_default(self):
+        """Return the default value when accessed via the alias name"""
+        if self.alias_force_string:
+            return ""
+        else:
+            return None
+
+    default = property(get_default)
+
     def convert(self, value):
-        """Convert value using self.convert.
-        If self.convert is None, return value as is.
+        """Convert the value when accessed via the alias name.
+        If alias_force_string is true, the value is forced to a unicode
+        string with the ensure_unicode method.  Otherwise it is returned
+        as-is.
         """
-        if self.converter is not None:
-            value = self.converter(value)
+        if self.alias_force_string:
+            value = ensure_unicode(value)
         return value
 
 
@@ -640,27 +664,26 @@
 
     # Definitions for some commonly used public instance attributes.
     #
-    # This is a list of AliasDescription instances.
-    aliases = [AliasDescription("knr", "fn",  "",  ensure_unicode,
-                                in_session=True),
-               AliasDescription("last_name", "name",  "",  ensure_unicode,
-                                in_session=True),
-               AliasDescription("first_name", "vorname",  "",  ensure_unicode,
-                                in_session=True),
-               AliasDescription("street", "adresse_strasse",  "",
-                                ensure_unicode, in_session=True),
-               AliasDescription("streetnr", "adresse_strassenr",  "",
-                                ensure_unicode, in_session=True),
-               AliasDescription("plz", "adresse_plz",  "",  ensure_unicode,
-                                in_session=True),
-               AliasDescription("city", "adresse_ort",  "",  ensure_unicode,
-                                in_session=True),
-               AliasDescription("first_meeting", "erstgespraech",
-                                in_session=True),
-               AliasDescription("cm_end", "datum_cm_ende", in_session=True),
-               AliasDescription("editor", "bearbeiter_id", in_session=False)]
+    fields = [
+        Field("fn", alias="knr", alias_force_string=True, session=True),
+        Field("name", alias="last_name", alias_force_string=True, session=True),
+        Field("vorname", alias="first_name", alias_force_string=True,
+              session=True),
+        Field("adresse_strasse", alias="street", alias_force_string=True,
+              session=True),
+        Field("adresse_strassenr", alias="streetnr", alias_force_string=True,
+              session=True),
+        Field("adresse_plz", alias="plz", alias_force_string=True,
+              session=True),
+        Field("adresse_ort", alias="city", alias_force_string=True,
+              session=True),
+        Field("erstgespraech", alias="first_meeting", session=True),
+        Field("datum_cm_ende", alias="cm_end", session=True),
+        Field("bearbeiter_id", alias="editor", session=False),
+        ]
 
-    alias_dict = dict((desc.name, desc) for desc in aliases)
+    alias_dict = dict((field.alias, field) for field in fields
+                                           if field.alias is not None)
 
     def __init__(self, id=None, state=None, preset=None):
         self.id = id
@@ -678,13 +701,13 @@
 
     def __getattr__(self, name):
         """Provide attribute like access to some fields for compatibility"""
-        desc = self.alias_dict.get(name)
-        if desc:
+        field = self.alias_dict.get(name)
+        if field:
             try:
-                value = self.get_value(desc.formedname)
+                value = self.get_value(field.name)
             except KeyError:
-                value = desc.default
-            return desc.convert(value)
+                value = field.default
+            return field.convert(value)
         raise AttributeError(name)
 
     @classmethod
@@ -693,7 +716,7 @@
         The list is intended to be used when populating the preset
         mapping passed to the constructor.
         """
-        return [alias.formedname for alias in cls.aliases]
+        return [field.name for field in cls.fields if field.alias is not None]
 
     @classmethod
     def get_session_fields(cls):
@@ -701,7 +724,7 @@
         The items of the list are those items of cls.alias whose
         in_session attribute is true.
         """
-        return [alias for alias in cls.aliases if alias.in_session]
+        return [field for field in cls.fields if field.session and field.alias]
 
     def get_value(self, name, **kw):
         """Return the value of the formed field given by name.
@@ -785,14 +808,14 @@
     def getSessionCase(self):
         """Return a SessionCase object initialized from the current case"""
         fields = dict()
-        for desc in self.get_session_fields():
+        for field in self.get_session_fields():
             # FIXME: Ideally we would use
-            # self.get_value(desc.formedname) to get the value.  We use
+            # self.get_value(field.name) to get the value.  We use
             # getattr instead because this compatibility layer
-            # translates None values into string which some code
+            # translates None values into strings which some code
             # (e.g. buildInfofield in
             # templates/casemanagement/main.mako) relies on.
-            fields[desc.name] = getattr(self, desc.name)
+            fields[field.alias] = getattr(self, field.alias)
         signed = self.getPrivacyStatement().isSigned()
         return SessionCase(self.id, state=self.state,
                            privacy_statement=signed,



More information about the Mpuls-commits mailing list