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

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


Author: bh
Date: 2010-02-18 19:27:18 +0100 (Thu, 18 Feb 2010)
New Revision: 1666

Modified:
   wasko/branches/2.0/ChangeLog
   wasko/branches/2.0/mpulsweb/model/case.py
   wasko/branches/2.0/waskaweb/model/case.py
Log:
* waskaweb/model/case.py (Case): Derive from MpulsCase.
(Case.set_fields, Case.__getattr__)
(Case.get_preset_fields, Case.get_preload_fields)
(Case.get_session_fields, Case.get_digest_fields)
(Case.get_search_retrieve_fields, Case.get_search_match_fields)
(Case.get_value, Case.get_description)
(Case._load_formed_instance_tree, Case._get_formed_item): Removed.
These methods are now in MpulsCase

* mpulsweb/model/case.py (MpulsCase): New.  Base class for the
case objects, containing all the methods that were removed from
Case


Modified: wasko/branches/2.0/ChangeLog
===================================================================
--- wasko/branches/2.0/ChangeLog	2010-02-18 18:11:07 UTC (rev 1665)
+++ wasko/branches/2.0/ChangeLog	2010-02-18 18:27:18 UTC (rev 1666)
@@ -1,5 +1,22 @@
 2010-02-18  Bernhard Herzog  <bh at intevation.de>
 
+	Move the Case class partly to a new base class, MpulsCase:
+
+	* waskaweb/model/case.py (Case): Derive from MpulsCase.
+	(Case.set_fields, Case.__getattr__)
+	(Case.get_preset_fields, Case.get_preload_fields)
+	(Case.get_session_fields, Case.get_digest_fields)
+	(Case.get_search_retrieve_fields, Case.get_search_match_fields)
+	(Case.get_value, Case.get_description)
+	(Case._load_formed_instance_tree, Case._get_formed_item): Removed.
+	These methods are now in MpulsCase
+
+	* mpulsweb/model/case.py (MpulsCase): New.  Base class for the
+	case objects, containing all the methods that were removed from
+	Case
+
+2010-02-18  Bernhard Herzog  <bh at intevation.de>
+
 	* mpulsweb/model/case.py (Field): Added. Moved from waskaweb
 
 	* waskaweb/model/case.py (Field): Removed. Moved to mpulsweb

Modified: wasko/branches/2.0/mpulsweb/model/case.py
===================================================================
--- wasko/branches/2.0/mpulsweb/model/case.py	2010-02-18 18:11:07 UTC (rev 1665)
+++ wasko/branches/2.0/mpulsweb/model/case.py	2010-02-18 18:27:18 UTC (rev 1666)
@@ -30,9 +30,11 @@
 import logging
 
 from formed.meta.data import RepeatNode
+from formed.instance.backends.postgres import DBFactory as InstanceFactory
 
 from mpulsweb.lib.base import g
 from mpulsweb.lib.helpers import ensure_unicode
+from mpulsweb.lib.db import PostgresDBInterface
 
 
 log = logging.getLogger(__name__)
@@ -164,3 +166,121 @@
         if self.alias_force_string:
             value = ensure_unicode(value)
         return value
+
+
+class MpulsCase:
+
+    def __init__(self, id=None, preset=None):
+        self.id = id
+        if preset is None:
+            preset = dict()
+        self.preset = preset
+        self.formed_instance = None
+
+    @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_session_fields(cls):
+        """Return a list describing the fields to store in the session case.
+        The items of the list are those items of cls.alias whose
+        in_session attribute is true.
+        """
+        return [field for field in cls.fields if field.session and field.alias]
+
+    @classmethod
+    def get_digest_fields(cls):
+        """Return a list describing all fields shown in the digest."""
+        return [field for field in cls.fields if field.digest]
+
+    @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)
+        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.
+
+        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

Modified: wasko/branches/2.0/waskaweb/model/case.py
===================================================================
--- wasko/branches/2.0/waskaweb/model/case.py	2010-02-18 18:11:07 UTC (rev 1665)
+++ wasko/branches/2.0/waskaweb/model/case.py	2010-02-18 18:27:18 UTC (rev 1666)
@@ -50,7 +50,7 @@
 from mpulsweb.model.user import UserObject
 from mpulsweb.model.agencysettings import Agency
 from mpulsweb.model.document import listDocuments
-from mpulsweb.model.case import Field, LoadCaseNotExistsError, \
+from mpulsweb.model.case import Field, LoadCaseNotExistsError, MpulsCase, \
      get_field_identifier
 
 from mpulsweb.controllers.formularpage import convert2dic
@@ -579,10 +579,10 @@
         return True
 
 
-class Case:
+class Case(MpulsCase):
 
     def __init__(self, id=None, state=None, preset=None):
-        self.id = id
+        MpulsCase.__init__(self, id=id, preset=preset)
         self.state = state
         self.standin = None
         self.digest = None
@@ -590,120 +590,7 @@
         self.appointments = []
         self.privacy_statement = None
         self.discretion_statement = None
-        if preset is None:
-            preset = dict()
-        self.preset = preset
-        self.formed_instance = None
 
-
-    @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)
-
-
-    def __getattr__(self, name):
-        """Provide attribute like access to some fields for compatibility"""
-        field = self.alias_dict.get(name)
-        if field:
-            try:
-                value = self.get_value(field.name)
-            except KeyError:
-                value = field.default
-            return field.convert(value)
-        raise AttributeError(name)
-
-    @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_session_fields(cls):
-        """Return a list describing the fields to store in the session case.
-        The items of the list are those items of cls.alias whose
-        in_session attribute is true.
-        """
-        return [field for field in cls.fields if field.session and field.alias]
-
-    @classmethod
-    def get_digest_fields(cls):
-        """Return a list describing all fields shown in the digest."""
-        return [field for field in cls.fields if field.digest]
-
-    @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 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