[Mpuls-commits] r942 - in wasko/branches/2.0: . waskaweb/lib

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Wed Jan 27 12:07:56 CET 2010


Author: bh
Date: 2010-01-27 12:07:53 +0100 (Wed, 27 Jan 2010)
New Revision: 942

Modified:
   wasko/branches/2.0/ChangeLog
   wasko/branches/2.0/waskaweb/lib/search.py
Log:
* waskaweb/lib/search.py: Fix formatting. Add some comments.


Modified: wasko/branches/2.0/ChangeLog
===================================================================
--- wasko/branches/2.0/ChangeLog	2010-01-27 10:05:52 UTC (rev 941)
+++ wasko/branches/2.0/ChangeLog	2010-01-27 11:07:53 UTC (rev 942)
@@ -1,3 +1,7 @@
+2010-01-27  Bernhard Herzog  <bh at intevation.de>
+
+	* waskaweb/lib/search.py: Fix formatting. Add some comments.
+
 2010-01-27  Torsten Irländer <torsten.irlaender at intevation.de>
 
 	* waskaweb/lib/helpers.py: Reordered functions and added some comments 

Modified: wasko/branches/2.0/waskaweb/lib/search.py
===================================================================
--- wasko/branches/2.0/waskaweb/lib/search.py	2010-01-27 10:05:52 UTC (rev 941)
+++ wasko/branches/2.0/waskaweb/lib/search.py	2010-01-27 11:07:53 UTC (rev 942)
@@ -1,27 +1,27 @@
 # -*- coding: utf-8 -*-
 #
 # Copyright 2007, 2008 Intevation GmbH, Germany, <info at intevation.de>
-# 
-# This file is part of mpuls WASKA (CoMPUter-based case fiLeS - 
+#
+# This file is part of mpuls WASKA (CoMPUter-based case fiLeS -
 # Web-Anwendungs-Server fuer Kompetenzagenturen).
-# 
+#
 # mpuls WASKA is free software: you can redistribute it and/or modify it under
 # the terms of the GNU Affero General Public License as published by the
 # Free Software Foundation, either version 3 of the License, or (at your
 # option) any later version.
-# 
+#
 # mpuls WASKA is distributed in the hope that it will be useful, but WITHOUT
 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
 # License for more details.
-# 
+#
 # You should have received a copy of the GNU Affero General Public
 # License along with mpuls WASKA. If not, see <http://www.gnu.org/licenses/>.
-# 
-# mpuls WASKA has been developed on behalf of the 
+#
+# mpuls WASKA has been developed on behalf of the
 # Projekttraeger im Deutschen Zentrum fuer Luft- und Raumfahrt e.V. (PT-DLR)
 # within the programme Kompetenzagenturen (Durchfuehrungsphase) funded by
-# the Bundesministerium fuer Familie, Senioren, Frauen und Jugend and 
+# the Bundesministerium fuer Familie, Senioren, Frauen und Jugend and
 # European Social Fund resources.
 #
 # Authors:
@@ -32,14 +32,18 @@
 import re
 from datetime import datetime
 
-from waskaweb.lib.db import db
 import psycopg2.extras
 
-from waskaweb.lib.base import session, g, h, config 
+from waskaweb.lib.db import db
+from waskaweb.lib.base import session, g, h, config
 
 SAVE_SEARCH = re.compile(r'[^\w:;\-\. ]', re.UNICODE)
 
-VALID_OPTIONS = ['editor', 'migration', 'own', 'standin', 'state', 'sort_field', 'sort_order', 'interval_start_date', 'interval_end_date', 'interval_start_field', 'interval_end_field', 'gender', 'phase', 'branch', 'fkz', 'inconsistency', 'only_cases_after', 'bad', 'field']
+VALID_OPTIONS = ['editor', 'migration', 'own', 'standin', 'state',
+                 'sort_field', 'sort_order', 'interval_start_date',
+                 'interval_end_date', 'interval_start_field',
+                 'interval_end_field', 'gender', 'phase', 'branch', 'fkz',
+                 'inconsistency', 'only_cases_after', 'bad', 'field']
 
 SEARCH_CASE_SQL = \
 """ SELECT
@@ -84,7 +88,9 @@
 # FIXME: This should be better a config var
 INCONSISTENCY_CHECK_AFTER = '2008-08-31'
 
+
 class Search:
+
     def __init__(self):
         pass
 
@@ -93,10 +99,10 @@
         # implement some searchengine here
         search = []
         allowed_states = [-1]
-        bad_types      = []
-        phases         = []
-        fields         = {}
-        options        = {}
+        bad_types = []
+        phases = []
+        fields = {}
+        options = {}
 
         if s:
             s = SAVE_SEARCH.sub(u'', s)
@@ -112,13 +118,15 @@
                                 phases.append(int(value))
                             else:
                                 options[key] = value
-                    except ValueError, err :
-                        print >> sys.stderr, "Found invalid search syntax in options: %s" % err
+                    except ValueError, err:
+                        print >> sys.stderr, \
+                              "Found invalid search syntax in options: %s" % err
                 else:
                     search.append(option)
         return search, options, allowed_states, bad_types, phases, fields
 
-    def _build_sql(self, search, options, allowed_states, bad_types, phases, fieldsdic):
+    def _build_sql(self, search, options, allowed_states, bad_types, phases,
+                   fieldsdic):
         pass
 
     def queryDB(self, sql, fields):
@@ -135,39 +143,52 @@
                 return {}
         finally:
             db.recycleConnection(con, cur)
-        return rows 
+        return rows
 
     def perform(self, search_str):
         '''Returns the result set of a search based on the search string'''
-        search, options, states, bad_types, phases, fields = self._parse_search_str(search_str)
-        sql, fields = self._build_sql(search, options, states, bad_types, phases, fields)
+        search, options, states, bad_types, phases, fields = \
+                self._parse_search_str(search_str)
+        sql, fields = self._build_sql(search, options, states, bad_types,
+                                      phases, fields)
         result = self.queryDB(sql, fields)
         return result
 
+
 class CaseSearch(Search):
+
     def __init__(self):
         Search.__init__(self)
 
-    def _build_sql(self, search, options, allowed_states, bad_types, phaseslist, fieldsdic):
+    def _build_sql(self, search, options, allowed_states, bad_types, phaseslist,
+                   fieldsdic):
         fields = {'needle': ""}
-        # Show all cases regardless if the privacy statement has been declined
-        #ee =  h.hasRole(['cm_ka']) and "m.einverstaendniserklaerung <> 0" or "TRUE"
         ee = "TRUE"
 
         which_user = 0
         allowed_viewer = "FALSE"
-        if options.has_key('own'): which_user += 2
-        if options.has_key('standin'): which_user += 1
-        if which_user == 1: allowed_viewer = "m.bearbeiter_id <> %s" % options.get('standin')
-        if which_user == 2: allowed_viewer = "m.bearbeiter_id = %s" % options.get('own')
-        if which_user == 3: allowed_viewer = "TRUE"
-        gender = options.get('gender') and "m.geschlecht = %s" % options.get('gender') or "TRUE"
-        allowed_viewer =  h.hasRole(['admin_ka', 'pb_ka']) and "TRUE" or allowed_viewer
-        editor = options.has_key('editor') and "m.bearbeiter_id = %s" % options.get('editor') or "TRUE"
+        if options.has_key('own'):
+            which_user += 2
+        if options.has_key('standin'):
+            which_user += 1
+        if which_user == 1:
+            allowed_viewer = "m.bearbeiter_id <> %s" % options.get('standin')
+        if which_user == 2:
+            allowed_viewer = "m.bearbeiter_id = %s" % options.get('own')
+        if which_user == 3:
+            allowed_viewer = "TRUE"
+        gender = (options.get('gender')
+                  and "m.geschlecht = %s" % options.get('gender') or "TRUE")
+        allowed_viewer = (h.hasRole(['admin_ka', 'pb_ka'])
+                          and "TRUE" or allowed_viewer)
+        editor = (options.has_key('editor')
+                  and "m.bearbeiter_id = %s" % options.get('editor') or "TRUE")
         sort_field = options.get('sort_field', "name")
-        sort_order       = options.get('sort_order', "desc")
-        branch = options.get('branch') and "b.filiale = '%s'" % options.get('branch') or "TRUE"
-        fkz = options.get('fkz') and "m.fkz = '%s'" % options.get('fkz') or "TRUE"
+        sort_order = options.get('sort_order', "desc")
+        branch = (options.get('branch')
+                  and "b.filiale = '%s'" % options.get('branch') or "TRUE")
+        fkz = (options.get('fkz')
+               and "m.fkz = '%s'" % options.get('fkz') or "TRUE")
 
         #Identify bad cases
         bad_query = []
@@ -186,42 +207,74 @@
         # This is a convinience option to ignore older cases in the
         # inconsistency search
         if options.get('only_cases_after'):
-            only_cases_after = "coalesce(erstgespraech, now()) > '%s'" % options.get('only_cases_after')
+            only_cases_after = ("coalesce(erstgespraech, now()) > '%s'"
+                                % options.get('only_cases_after'))
         else:
             only_cases_after = 'TRUE'
 
         #Build timeinterval
         time_interval = "TRUE"
-        if options.get('interval_start_date') and options.get('interval_end_date'):
-            interval_start_field = options.get('interval_start_field', "erstgespraech")
-            interval_end_field = options.get('interval_end_field', "datum_cm_ende")
-            interval_start_date = options.get('interval_start_date',"1970-01-01")
+        if (options.get('interval_start_date')
+            and options.get('interval_end_date')):
+            interval_start_field = options.get('interval_start_field',
+                                               "erstgespraech")
+            interval_end_field = options.get('interval_end_field',
+                                             "datum_cm_ende")
+            interval_start_date = options.get('interval_start_date',
+                                              "1970-01-01")
             d = datetime.today()
-            interval_end_date = options.get('interval_end_date', d.strftime('%Y-%m-%d'))
-            fields = {'start_field': interval_start_field, 'start_date': interval_start_date, 'end_field': interval_end_field, 'end_date': interval_end_date} 
+            interval_end_date = options.get('interval_end_date',
+                                            d.strftime('%Y-%m-%d'))
+            fields = {'start_field': interval_start_field,
+                      'start_date': interval_start_date,
+                      'end_field': interval_end_field,
+                      'end_date': interval_end_date}
             if interval_start_date != '1970-01-01':
-                time_interval = "(coalesce(%(start_field)s, '1970-01-01'::date) <= '%(end_date)s'::date \
-                AND (coalesce(%(end_field)s, now()) >= '%(start_date)s'::date))" % fields
+                time_interval =("(coalesce(%(start_field)s, '1970-01-01'::date)"
+                                " <= '%(end_date)s'::date"
+                                " AND (coalesce(%(end_field)s, now())"
+                                       " >= '%(start_date)s'::date))" % fields)
 
         #Phases
         phases = 'TRUE' #default
         if phaseslist:
-            phases = 'm.id in (SELECT id from master_tbl_view WHERE phase in (%s))' % ','.join([str(p) for p in phaseslist])
+            phases = ('m.id in'
+                      ' (SELECT id from master_tbl_view WHERE phase in (%s))'
+                      % ','.join([str(p) for p in phaseslist]))
 
         #Migration
         migration = "TRUE"
         if options.get('migration'):
             if options.get('migration') == '1':
-                migration = "(nat_muspra not in (18,-1, -3) or nat_staat not in (1,-1,-3) or vater_land not in (29, -1, -3) or mutter_land not in (29,-1, -3))"
+                migration = ("(nat_muspra not in (18, -1, -3)"
+                             " or nat_staat not in (1, -1, -3)"
+                             " or vater_land not in (29, -1, -3)"
+                             " or mutter_land not in (29, -1, -3))")
             elif options.get('migration') == '0':
-                migration = "(nat_muspra = 18 and nat_staat = 1 and vater_land = 29 and mutter_land = 29)"
+                migration = ("(nat_muspra = 18 and nat_staat = 1"
+                             " and vater_land = 29 and mutter_land = 29)")
             elif options.get('migration') == '-1':
-                migration = "(not (nat_muspra not in (18,-1, -3) or nat_staat not in (1,-1,-3) or vater_land not in (29, -1, -3) or mutter_land not in (29,-1, -3)) and not (nat_muspra = 18 and nat_staat = 1 and vater_land = 29 and mutter_land = 29))"
+                migration = ("(not (nat_muspra not in (18,-1, -3)"
+                                  " or nat_staat not in (1, -1,-3)"
+                                  " or vater_land not in (29, -1, -3)"
+                                  " or mutter_land not in (29, -1, -3))"
+                                  " and not (nat_muspra = 18 and nat_staat = 1"
+                                           " and vater_land = 29"
+                                           " and mutter_land = 29))")
             elif options.get('migration') == '2':
-                migration = "(nat_staat = 2 and (vater_staat != 2 or mutter_staat != 2))"
+                # FIXME: This case is WASKO specific.  It used to search
+                # cases that were incorrectly imported so that the
+                # missing information can be corrected manually.
+                # wasko/issue344
+                migration = ("(nat_staat = 2"
+                             " and (vater_staat != 2 or mutter_staat != 2))")
 
-        fields['needle'] = " ".join(search) 
-        SQL = SEARCH_CASE_SQL % (",".join([str(x) for x in allowed_states]), ee, allowed_viewer, editor, gender, time_interval, phases, branch, fkz, bad, only_cases_after, migration, sort_field, sort_order) 
+        fields['needle'] = " ".join(search)
+        SQL = SEARCH_CASE_SQL % (",".join([str(x) for x in allowed_states]),
+                                 ee, allowed_viewer, editor, gender,
+                                 time_interval, phases, branch, fkz, bad,
+                                 only_cases_after, migration, sort_field,
+                                 sort_order)
         return (SQL, fields)
 
 # vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:



More information about the Mpuls-commits mailing list