[Mpuls-commits] r5475 - base/trunk/mpulsweb/controllers

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Fri Oct 14 20:24:24 CEST 2011


Author: bh
Date: 2011-10-14 20:24:22 +0200 (Fri, 14 Oct 2011)
New Revision: 5475

Modified:
   base/trunk/mpulsweb/controllers/evaluation_overview.py
Log:
Make evaluation_overview more like case_overview again.

The evaluation_overview controller started as a copy of the
case_overview controller. The case_overview controller was refactored in
the meantime, but this refactoring was not applied to the
evaluation_overview, so that by now it's harder to maintain. 

This refactoring moves both controllers closer again, so that the
differences are much smaller again now. Part of the changes to the
evaluation_overview controller is that it can now handle date objects as
the values of the sdate and edate parameters just like the case_overview
controller.


Modified: base/trunk/mpulsweb/controllers/evaluation_overview.py
===================================================================
--- base/trunk/mpulsweb/controllers/evaluation_overview.py	2011-10-14 14:41:59 UTC (rev 5474)
+++ base/trunk/mpulsweb/controllers/evaluation_overview.py	2011-10-14 18:24:22 UTC (rev 5475)
@@ -22,7 +22,7 @@
     and order. If user does not provide one of there params, try to return the
     safed params in session. If session is also empty return default params.
     '''
-    session_options = session['AGENCY_OVERVIEW_SEARCHOPTIONS']
+    session_options = session.get('AGENCY_OVERVIEW_SEARCHOPTIONS', {})
     sort = params.get('sort_field', session_options.get('sort_field'))
     order = params.get('sort_order', session_options.get('sort_order'))
     session_options['sort_field'] = sort
@@ -84,73 +84,84 @@
             session.save()
 
         search_options = search_options_from_session()
+        search_options.update(self.parseSortOptions(request.params))
         session['AGENCY_OVERVIEW_SEARCHOPTIONS'] = search_options
         session.save()
-        search = self.parseSearchOptions(search_options,
-                                        extended=session.get("EXTENDED_SEARCH"))
 
-        # Sorting the cases
-        search.update(self.parseSortOptions(request.params))
+        return self.search(search_options)
 
-        if not c.form_errors:
-            c.form_errors = {}
+    def _getCaseList(self, search_options):
+        cases = MpulsAgencyOverview()
+        if search_options:
+            options = self.parseSearchOptions(search_options,
+                                        extended=session.get("EXTENDED_SEARCH"))
+            cases.search(options)
+        return cases
 
-        # Do the search if there are no errors
-        c.cases = MpulsAgencyOverview()
-        if not c.form_errors:
-            c.cases.search(search)
+    def _renderOverview(self, cases, form_results, form_defaults, form_errors):
+        user = session['USER_AUTHORIZED']
 
         # Values used in the template
-        #
         c.extended_search = session.get('EXTENDED_SEARCH', False)
         ulo = UserListObject()
         filter = [u.id for u in ulo.getAdminList()]
         c.userlist = ulo.getUserList(filter)
-
-        user = session['USER_AUTHORIZED']
         c.phases = PhaseFactory().build()
 
         #count num of cases
-        c.count_agency = len([x for x in c.cases.getDatasets()])
-        c.count_all = sum([int(x.count) for x in c.cases.getDatasets()])
+        c.count_agency = len([x for x in cases.getDatasets()])
+        c.count_all = sum([int(x.count) for x in cases.getDatasets()])
 
-        # Load case-selection and merge with default search options
-        # Ok please close your eyes now!!! This is.... well ugly
-        search_options_copy = search_options.copy()
-
         # Hide evaluation option if someone did a search for a particular editor
         c.hide_evaluation = False
-        if "editor" in search:
+        if form_results.get('editor') != -1:
             c.hide_evaluation = True
 
-        overview = render('/evaluation/overview.mako')
-
-        form_defaults = search_options_copy or request.params.mixed()
         session["id_field_candidate"] = "m.fkz::integer"
         form_defaults["all_ids"] = " ".join(unicode(case.id)
-                                            for case in c.cases.getDatasets()
+                                            for case in cases.getDatasets()
                                             if case.id)
-        session["sql_where_candidate"] = c.cases.getWhereSQL()
+        session["sql_where_candidate"] = cases.getWhereSQL()
         session.save()
+
+        c.cases = cases
+        overview = render('/evaluation/overview.mako')
         return formencode.htmlfill.render(overview, defaults=form_defaults,
-                                          errors=c.form_errors)
+                                          errors=form_errors)
 
 
     @checkRole(('admin', 'cm', 'pt_dlr'))
-    def search(self):
-        c.form_result = {}
-        c.form_errors = {}
-        try:
-            c.form_result = g.eval_search.validate_form_params(request.params)
-        except formencode.Invalid, error:
-            log.exception("search: formencode error %s" % error)
-            c.form_result = error.value
-            c.form_errors = error.error_dict or {}
+    def search(self, search_options=None):
+        validator = g.eval_search.get_form_validator()
 
-        session['AGENCY_OVERVIEW_SEARCHOPTIONS'] = c.form_result
-        session.save()
-        return self.overview()
+        form_result = {}
+        form_errors = {}
+        form_defaults = None
 
+        if search_options is None:
+            search_options = {}
+            form_result.update(search_options_from_session())
+        else:
+            form_result.update(search_options)
+
+        if not search_options:
+            try:
+                form_result = validator.to_python(request.params)
+                search_options.update(form_result)
+                session['AGENCY_OVERVIEW_SEARCHOPTIONS'] = form_result
+                session.save()
+            except formencode.Invalid, error:
+                log.exception("search: formencode error %s" % error)
+                form_defaults = error.value
+                form_errors = error.error_dict or {}
+
+        if form_defaults is None:
+            form_defaults = validator.from_python(form_result)
+
+        cases = self._getCaseList(search_options)
+        return self._renderOverview(cases, form_result, form_defaults,
+                                    form_errors)
+
     def parseSortOptions(self, options=None):
         sort_field, sort_order = get_sort(options)
 



More information about the Mpuls-commits mailing list