[Mpuls-commits] r2066 - wasko/branches/2.0/mpulsweb/model

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Tue Mar 23 11:06:03 CET 2010


Author: torsten
Date: 2010-03-23 11:06:02 +0100 (Tue, 23 Mar 2010)
New Revision: 2066

Modified:
   wasko/branches/2.0/mpulsweb/model/appointment.py
Log:
* mpulsweb/model/appointment.py (MaxSaveTimeReminderOverview): New
	copied from jmdweb 


Modified: wasko/branches/2.0/mpulsweb/model/appointment.py
===================================================================
--- wasko/branches/2.0/mpulsweb/model/appointment.py	2010-03-23 09:12:10 UTC (rev 2065)
+++ wasko/branches/2.0/mpulsweb/model/appointment.py	2010-03-23 10:06:02 UTC (rev 2066)
@@ -33,8 +33,10 @@
 from datetime import datetime, timedelta
 
 import mpulsweb.lib.helpers as h
-from mpulsweb.lib.base import session
+from mpulsweb.lib.base import session, g
 from mpulsweb.lib.db import db
+from mpulsweb.model.case import MpulsCaseOverview
+from mpulsweb.model.agencysettings import Agency
 
 GET_GLOBAL_APPOINTMENT_SQL = """\
 SELECT id, master_id, sdatum, edatum, name, beschreibung, art
@@ -109,6 +111,41 @@
 WHERE id = %(id)s
 """
 
+GET_OLDACTIVE_CASES_4_ADMIN = """
+SELECT m.id, s.zugriff, getLastCaseAppointment(m.id) AS last_date
+FROM
+  (SELECT * FROM ka_status_tbl_view
+   WHERE zugriff < now() - interval '1 day' * %(maxdays)s AND status <> 5) s
+   JOIN master_tbl_view m ON m.id = s.master_id
+WHERE phase::varchar in (%(phases)s)
+"""
+
+GET_OLDACTIVE_CASES = """
+SELECT m.id, s.zugriff, getLastCaseAppointment(m.id) AS last_date
+FROM
+  (SELECT * FROM ka_status_tbl_view
+   WHERE zugriff < now() - interval '1 day' * %(maxdays)s AND status <> 5) s
+   JOIN master_tbl_view m ON m.id = s.master_id
+WHERE phase::varchar in (%(phases)s) AND m.bearbeiter_id = %(editor)s
+"""
+
+GET_OLDFINISHED_CASES_4_ADMIN = """\
+SELECT m.id, s.zugriff, getLastCaseAppointment(m.id) as last_date
+FROM master_tbl_view m JOIN ka_status_tbl_view s ON s.master_id = m.id
+WHERE s.status <> 5
+      AND extract(day from now()-s.zugriff) > %(maxdays)s
+      AND phase::varchar in (%(phases)s)
+"""
+
+GET_OLDFINISHED_CASES = """\
+SELECT m.id, s.zugriff, getLastCaseAppointment(m.id) as last_date
+FROM master_tbl_view m JOIN ka_status_tbl_view s ON s.master_id = m.id
+WHERE s.status <> 5
+      AND m.bearbeiter_id = %(editor)s
+      AND extract(day from now()-s.zugriff) > %(maxdays)s
+      AND phase::varchar in (%(phases)s)
+"""
+
 log = logging.getLogger(__name__)
 
 class AppointmentFactory:
@@ -403,4 +440,117 @@
         CaseAppointment.__init__(self)
         self.type = 1
 
+# MAXSAVETIME
+
+class MaxSaveTimeReminderOverview(AppointmentOverview):
+
+    def __init__(self):
+        self.appointment_list = []
+        agency = Agency()
+        maxage = agency.getMaxSavetime()
+        cases = MpulsCaseOverview()
+        user = session['USER_AUTHORIZED']
+
+        # 0. Load ids and some additional info on cases which either are active
+        # but seems to be orphaned or are finished and not edited for
+        # longer than maxage.
+        # orphaned means that all last appointmentment, aids or the
+        # modification date is older than maxage
+        oldAndActive = self.__getOldAndActiveCases(maxage, user)
+        oldAndFinished = self.__getOldAndFinishedCases(maxage, user)
+
+        # 1. Load cases of the user so we can so checks regarding the maximum
+        # savetime on them
+        default_search_options = ['own:%s' % user.id, 'standin:%s' % user.id,
+                                  'state:1', 'state:2', 'state:3']
+        cases.search(";".join(default_search_options))
+
+        # 2. filter cases which seems to be orphaned and create reminders
+        oldAndActiveSet = set(oldAndActive)
+        old_cases = [case for case in cases.getDatasets()
+                          if case.id in oldAndActiveSet]
+        for case in old_cases:
+            info = oldAndActive[case.id]
+            last = info['last_date']
+            appointment = self.__makeAppointment(case, last, maxage,
+                                                 finished=False)
+            self.appointment_list.append(appointment)
+
+
+        # 3. filter cases which are finished and not edited and create reminders
+        oldAndFinishedSet = set(oldAndFinished)
+        old_cases = [case for case in cases.getDatasets()
+                          if case.id in oldAndFinishedSet]
+        for case in old_cases:
+            info = oldAndFinished[case.id]
+            access = info['access']
+            appointment = self.__makeAppointment(case, access, maxage,
+                                                 finished=True)
+            self.appointment_list.append(appointment)
+
+    def __getOldAndFinishedCases(self, days, user):
+        conn, cur = None, None
+        list = {}
+        try:
+            conn = db.getConnection()
+            cur = conn.cursor()
+            if user.isAdmin():
+                cur.execute(GET_OLDFINISHED_CASES_4_ADMIN, 
+                    {'maxdays': days, 
+                     'phases': ",".join(g.mpuls_config.get('phases', 'finished'))})
+            else:
+                cur.execute(GET_OLDFINISHED_CASES, 
+                    {'maxdays': days,
+                     'editor': user.id,
+                     'phases': ",".join(g.mpuls_config.get('phases', 'finished'))})
+                
+            result = cur.fetchall()
+            for r in result:
+                list[r[0]] = {'access':r[1], 'last_date': r[2]}
+            return list
+        finally:
+            db.recycleConnection(conn, cur)
+        return False
+
+    def __getOldAndActiveCases(self, days, user):
+        conn, cur = None, None
+        list = {}
+        try:
+            conn = db.getConnection()
+            cur = conn.cursor()
+            if user.isAdmin():
+                cur.execute(GET_OLDACTIVE_CASES_4_ADMIN, 
+                    {'maxdays': days,
+                     'editor': user.id,
+                     'phases': ",".join(g.mpuls_config.get('phases', 'active'))})
+            else:
+                cur.execute(GET_OLDACTIVE_CASES, 
+                    {'maxdays': days,
+                     'editor': user.id,
+                     'phases': ",".join(g.mpuls_config.get('phases', 'active'))})
+            result = cur.fetchall()
+            for r in result:
+                list[r[0]] = {'access':r[1], 'last_date': r[2]}
+            return list
+        finally:
+            db.recycleConnection(conn, cur)
+        return False
+
+    def __makeAppointment(self, case, last_date, maxage, finished):
+        factory = CaseAppointmentFactory()
+        type = 1
+        title = u"%s, %s" % (case.last_name, case.first_name)
+        age = datetime.now() - last_date
+        mydate = datetime.now() - timedelta(age.days-int(maxage))
+        if finished:
+            desc = (u"Achtung! Die maximale Speicherdauer von (%s Tagen)"
+                    u" ist seit %s Tagen für diese Fallakte überschritten!"
+                    % (maxage, age.days-int(maxage)))
+        else:
+            desc = (u"Inaktive Fallakte? Bitte prüfen Sie,"
+                    u" ob der Fall noch bearbeitet wird.")
+        appointment = factory.createByData(None, title, mydate, None, desc,
+                                           case.id, type)
+        return appointment
+
 # vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8:



More information about the Mpuls-commits mailing list