[Mpuls-commits] r5425 - base/trunk/mpulsweb/lib

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Fri Oct 7 19:26:40 CEST 2011


Author: bh
Date: 2011-10-07 19:26:39 +0200 (Fri, 07 Oct 2011)
New Revision: 5425

Modified:
   base/trunk/mpulsweb/lib/search.py
Log:
Add Search method extra_validators for easier validation of application
specific search fields.

Usually applications only add some more search fields to the case
overview. To validate them the used to have to override
validate_form_params which was cumbersome, especially since the
evaluation search was introduced which has its own validator.

Now most applications should be able to simply override the
extra_validators method and use the same implementation for both case
search and evaluation search.

The evaluation specific validator only adds the federal_state parameter,
so it could be replaced by extending extra_validators
accordingly. However, doing this now without adapting all applications,
could introduce defects.


Modified: base/trunk/mpulsweb/lib/search.py
===================================================================
--- base/trunk/mpulsweb/lib/search.py	2011-10-07 17:01:54 UTC (rev 5424)
+++ base/trunk/mpulsweb/lib/search.py	2011-10-07 17:26:39 UTC (rev 5425)
@@ -176,8 +176,20 @@
         # search module is imported early on when the application starts
         # so validators cannot be imported then.
         from mpulsweb.lib.validators import SearchCaseForm
-        return SearchCaseForm().to_python(params)
+        return SearchCaseForm(**self.extra_validators()).to_python(params)
 
+    def extra_validators(self):
+        """Return additional application specific validators.
+        The return value should be a dictionary mapping form parameter
+        names to validators. The return value is passed as a dictionary
+        with keyword arguments to the search validator.
+
+        This default implementation returns a new empty
+        dictionary. Derived classes should call this method and add
+        their own validators.
+        """
+        return dict()
+
     def convert_form_parameters(self, options, user, extended):
         """Convert form parameters to search options.
         The parameter user is the user object for the current user.
@@ -530,14 +542,15 @@
         self.match_fields = match_fields
 
     def validate_form_params(self, params):
-        # FIXME: We import SearchCaseForm here because importing it
+        # FIXME: We import EvaluationSearchCaseForm here because importing it
         # during import doesn't work.  The validators module performs
         # translations and accesses app_globals during import, so it has
         # to be imported when a HTTP-request is being handled.  The
         # search module is imported early on when the application starts
         # so validators cannot be imported then.
         from mpulsweb.lib.validators import EvaluationSearchCaseForm
-        return EvaluationSearchCaseForm().to_python(params)
+        validator = EvaluationSearchCaseForm(self.extra_validators())
+        return validator.to_python(params)
 
     def get_needle_clause(self, options):
         """Return the search condition to select cases based on search terms.



More information about the Mpuls-commits mailing list