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

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


Author: torsten
Date: 2010-12-28 11:27:56 +0100 (Tue, 28 Dec 2010)
New Revision: 4435

Modified:
   base/trunk/mpulsweb/lib/search.py
Log:
* mpulsweb/lib/search.py (AppMixin): New base class for application
	specific search.
* mpulsweb/lib/search.py (EvaluationSearch): New class for search in
	evaluationserver. Refactored Search and CaseSearch. Moved common
	functions into base Search class.


Modified: base/trunk/mpulsweb/lib/search.py
===================================================================
--- base/trunk/mpulsweb/lib/search.py	2010-12-28 10:24:41 UTC (rev 4434)
+++ base/trunk/mpulsweb/lib/search.py	2010-12-28 10:27:56 UTC (rev 4435)
@@ -18,6 +18,12 @@
 
 log = logging.getLogger(__name__)
 
+class AppMixin(object):
+    """
+        class to configure application spectific behavior of the search which
+        is common to the case and evaluation search. This class can be
+        overwritten in the application specific /lib/search.py
+    """ 
 
 class Search(object):
 
@@ -38,6 +44,7 @@
     """
 
     SEARCH_ORDER = """ORDER BY %s %s"""
+    GROUP_BY = """GROUP BY %s"""
 
 
     def __init__(self):
@@ -158,26 +165,6 @@
             options = self.check_search_options(search)
         return self.queryDB(self._build_sql(options))
 
-
-class CaseSearch(Search):
-
-    VALID_OPTIONS = (Search.VALID_OPTIONS
-                     + ("state", "phase", "own", "standin", "editor", "branch",
-                        "sdate", "edate", "sort_field", "sort_order"))
-    MULTI_OPTIONS = Search.MULTI_OPTIONS + ('state', 'phase')
-
-
-    def __init__(self, retrieve_fields, match_fields):
-        Search.__init__(self)
-        self.retrieve_fields = retrieve_fields
-        self.match_fields = match_fields
-
-    def get_fields(self, options):
-        retrieve = ", ".join("m." + field for field in self.retrieve_fields)
-        if retrieve:
-            retrieve = "%s," % retrieve
-        return self.SEARCH_FIELDS % retrieve
-
     def validate_form_params(self, params):
         # FIXME: We import SearchCaseForm here because importing it
         # during import doesn't work.  The validators module performs
@@ -459,6 +446,28 @@
         sort_order = options.get('sort_order', "desc")
         return self.SEARCH_ORDER % (sort_field, sort_order)
 
+
+class CaseSearch(Search):
+
+    VALID_OPTIONS = (Search.VALID_OPTIONS
+                     + ("state", "phase", "own", "standin", "editor", "branch",
+                        "sdate", "edate", "sort_field", "sort_order"))
+    MULTI_OPTIONS = Search.MULTI_OPTIONS + ('state', 'phase')
+
+
+    def __init__(self, retrieve_fields, match_fields):
+        Search.__init__(self)
+        self.retrieve_fields = retrieve_fields
+        self.match_fields = match_fields
+
+    def get_fields(self, options):
+        retrieve = ", ".join("m." + field for field in self.retrieve_fields)
+        if retrieve:
+            retrieve = "%s," % retrieve
+        print retrieve
+        return self.SEARCH_FIELDS % retrieve
+
+
     def _build_sql(self, options):
         sql_fields = {}
         sql_fields['fields'] = self.get_fields(options)
@@ -468,4 +477,51 @@
         SQL = self.SEARCH_QUERY % sql_fields
         return SQL
 
+class EvaluationSearch(Search):
+
+    SEARCH_QUERY = """
+    SELECT %(fields)s
+    FROM master_tbl_view m
+    JOIN ka_status_tbl_view st ON m.id = st.master_id
+    JOIN ka_benutzer_tbl b ON m.bearbeiter_id = b.id
+    WHERE %(where)s
+    %(group)s
+    %(order)s
+    """
+
+    SEARCH_FIELDS = """fkz, count(m.id) as count"""
+
+    VALID_OPTIONS = (Search.VALID_OPTIONS
+                     + ("state", "phase", "own", "standin", "editor", "branch",
+                        "sdate", "edate", "sort_field", "sort_order"))
+    MULTI_OPTIONS = Search.MULTI_OPTIONS + ('state', 'phase')
+
+
+    def __init__(self, retrieve_fields, match_fields):
+        Search.__init__(self)
+        self.retrieve_fields = retrieve_fields
+        self.match_fields = match_fields
+
+    def get_fields(self, options):
+        return self.SEARCH_FIELDS
+
+    def get_order(self, options):
+        sort_field = options.get('sort_field', "m.fkz")
+        sort_order = options.get('sort_order', "desc")
+        return self.SEARCH_ORDER % (sort_field, sort_order)
+
+    def get_group(self, options):
+        group_field = options.get('group_field', "m.fkz")
+        return self.GROUP_BY % (group_field)
+
+    def _build_sql(self, options):
+        sql_fields = {}
+        sql_fields['fields'] = self.get_fields(options)
+        sql_fields['where'] = self.get_where(options)
+        sql_fields['order'] = self.get_order(options)
+        sql_fields['group'] = self.get_group(options)
+
+        SQL = self.SEARCH_QUERY % sql_fields
+        return SQL
+
 # vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:



More information about the Mpuls-commits mailing list