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

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Tue Dec 28 11:22:11 CET 2010


Author: torsten
Date: 2010-12-28 11:22:10 +0100 (Tue, 28 Dec 2010)
New Revision: 4433

Added:
   base/trunk/mpulsweb/controllers/evaluation_overview.py
Log:
* mpulsweb/controllers/evaluation_overview.py: New controller for
	agancy overview in evaluation server. Search paramenters are saved in
	its own session variable. Controller is very common to the
	CaseOverview-controller.


Added: base/trunk/mpulsweb/controllers/evaluation_overview.py
===================================================================
--- base/trunk/mpulsweb/controllers/evaluation_overview.py	2010-12-28 10:19:54 UTC (rev 4432)
+++ base/trunk/mpulsweb/controllers/evaluation_overview.py	2010-12-28 10:22:10 UTC (rev 4433)
@@ -0,0 +1,174 @@
+# -*- coding: utf-8 -*-
+import re
+import logging
+
+import formencode
+
+from pylons import app_globals as g
+
+from mpulsweb.lib.base import c, render, request, session, BaseController
+from mpulsweb.lib.security import checkRole
+from mpulsweb.model.user import UserListObject
+from mpulsweb.model.agency import MpulsAgencyOverview
+from mpulsweb.model.phase import PhaseFactory 
+
+
+log = logging.getLogger(__name__)
+
+
+def get_sort(params):
+    '''Return sort field and sort order. First try to return a userdefined sort
+    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']
+    sort = params.get('sort_field',
+                      session_options.get('sort_field', 'm.fkz'))
+    order = params.get('sort_order',
+                       session_options.get('sort_order', 'desc'))
+    session_options['sort_field'] = sort
+    session_options['sort_order'] = order
+    session.save()
+    return sort, order
+
+def default_phases():
+    phases = []
+    for pp in g.mpuls_config.get('phases', 'phases'):
+        pairs = g.mpuls_config.get('phases', 'pairs')
+        for p in pairs[0].get(str(pp), [-1]):
+            phases.append(p)
+    phases.append(-1)
+    return phases
+
+def parseSearchOptions(options, extended):
+    return g.eval_search.convert_form_parameters(options,
+                                                 session['USER_AUTHORIZED'],
+                                                 extended=extended)
+
+def search_options_from_session():
+    options = session.get('AGENCY_OVERVIEW_SEARCHOPTIONS')
+    if options:
+        return options
+
+    user = session['USER_AUTHORIZED']
+    if user.isAdmin():
+        return g.mpuls_config.get('search', 'admin_defaults')[0]
+    else:
+        return g.mpuls_config.get('search', 'cm_defaults')[0]
+
+
+class EvaluationOverviewController(BaseController):
+
+    @checkRole(('admin', 'cm', 'pt_dlr'))
+    def index(self):
+        return self.overview()
+
+    @checkRole(('admin', 'cm', 'pt_dlr'))
+    def overview_extended(self):
+        extended = session.get('EXTENDED_SEARCH')
+        if extended is not None:
+            extended = not extended
+        else:
+            extended = True
+        session['EXTENDED_SEARCH'] = extended
+        session.save()
+        return self.overview()
+
+    @checkRole(('admin', 'cm', 'pt_dlr'))
+    def overview(self):
+        # If reset parameter is set ti != 0 the delete all saved params and
+        # cases on overview page
+        reset = self._checkBool(request.params.get('reset', '0'))
+        if reset:
+            session['AGENCY_OVERVIEW_SEARCHOPTIONS'] = {}
+            session['AGENCY_OVERVIEW_SELECTION'] = {}
+            session['EXTENDED_SEARCH'] = False
+            session.save()
+
+        search_options = search_options_from_session()
+        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))
+
+        if not c.form_errors:
+            c.form_errors = {}
+
+        # Do the search if there are no errors
+        c.cases = MpulsAgencyOverview()
+        if not c.form_errors:
+            print search
+            c.cases.search(search)
+
+        # 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_all = len([x for x in c.cases.getDatasets()])
+        c.count_own = len([x for x in c.cases.getDatasets()])
+        c.count_anon = len([x for x in c.cases.getDatasets()])
+        c.count_markanon = len([x for x in c.cases.getDatasets()])
+        c.count_del = len([x for x in c.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()
+        search_options_copy.update(session.get('AGENCY_OVERVIEW_SELECTION', {}))
+
+        # Hide evaluation option if someone did a search for a particular editor
+        c.hide_evaluation = False
+        if "editor" in search:
+            c.hide_evaluation = True
+
+        overview = render('/evaluation/overview.mako')
+
+        form_defaults = search_options_copy or request.params.mixed()
+        form_defaults["all_ids"] = " ".join(unicode(case.id)
+                                            for case in c.cases.getDatasets())
+        return formencode.htmlfill.render(overview, defaults=form_defaults,
+                                          errors=c.form_errors)
+
+
+    @checkRole(('admin', 'cm', 'pt_dlr'))
+    def search(self):
+        c.form_result = {}
+        c.form_errors = {}
+        try:
+            c.form_result = g.case_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 {}
+
+        session['AGENCY_OVERVIEW_SEARCHOPTIONS'] = c.form_result
+        session.save()
+        return self.overview()
+
+    def parseSortOptions(self, options=None):
+        sort_field, sort_order = get_sort(options)
+
+        # Pass the sort options to the template
+        c.sort_field = sort_field
+        c.sort_order = sort_order
+
+        # default behavior is to take the sortfield for sorting
+        return dict(sort_field=sort_field,
+                    sort_order=sort_order)
+
+    def parseSearchOptions(self, options, extended):
+        return parseSearchOptions(options, extended)
+
+    def bundleAction(self):
+        return "not implemented"
+
+# vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:



More information about the Mpuls-commits mailing list