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

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Wed Mar 24 15:34:15 CET 2010


Author: torsten
Date: 2010-03-24 15:34:12 +0100 (Wed, 24 Mar 2010)
New Revision: 2110

Modified:
   wasko/branches/2.0/mpulsweb/model/case.py
Log:
* mpulsweb/model/case.py (MpulsCaseState): New. Copied from jmdweb.


Modified: wasko/branches/2.0/mpulsweb/model/case.py
===================================================================
--- wasko/branches/2.0/mpulsweb/model/case.py	2010-03-24 13:50:47 UTC (rev 2109)
+++ wasko/branches/2.0/mpulsweb/model/case.py	2010-03-24 14:34:12 UTC (rev 2110)
@@ -63,7 +63,26 @@
 SELECT delete_nm_benutzer_master_ds(%(groupid)s, %(caseid)s)"""
 DELETE_CASE_SQL = """select delete_master_ds(%(id)s)"""
 SET_EDITOR_SQL = """SELECT set_case_editor(%(userid)s, %(caseid)s)"""
+LOAD_CASESTATE_SQL = """
+SELECT
+    s.id,
+    s.status,
+    s.zugriff,
+    getLastCaseAppointment(m.id) AS letzer_termin,
+    m.phase AS phase
+FROM ka_status_tbl_view s JOIN master_tbl_view m ON m.id = s.master_id
+WHERE s.master_id = %(id)s
+"""
+SET_CASESTATE_SQL = """
+UPDATE ka_status_tbl_view
+    SET status = %(status)s
+    WHERE id = %(id)s"""
 
+SET_CASEPHASE_SQL = """
+    UPDATE master_tbl_view
+    SET phase = %(phase)s
+    WHERE id IN (SELECT master_id FROM ka_status_tbl_view WHERE id = %(id)s)"""
+
 #DIALOGS
 MARKANONYMIZE_FAILED_MIN_PHASE = u"""\
 <p>Eine Anonymisierung ist für Fälle in der Phase "%s" nicht
@@ -818,4 +837,145 @@
     def getDatasets(self):
         return self.ds_list
 
+class MpulsCaseState(object):
 
+    """This class store the state and the last accesstime of the case.
+    Possible states are:
+        * Open (1)
+        * Closes (2)
+        * outstanding deletion (3)
+        * outstanding anonymision (4)
+        * anonymised (5)
+    """
+
+    def __init__(self, id=None, master_id=None, state=None, access=None):
+        self.id         = id
+        self.master_id  = master_id
+        self.state      = state
+        self.access     = access
+        self.phase      = None
+        self._last_date = None
+
+        if self.master_id is not None and self.id is None:
+            self._load()
+
+    def getLastDateFromDB(self):
+        if self._last_date:
+            return self._last_date
+        else:
+            # Reload all the data. As fetching the last appointment
+            # for the case is the most expensive part of the operation getting
+            # some more values does not hurt.
+            self._load()
+            return self._last_date
+
+    last_date = property(getLastDateFromDB)
+
+    def setData(self, id, master_id, state, access):
+        self.master_id   = master_id
+        self.id          = id
+        self.state       = state
+        self.access      = access
+
+    def _load(self):
+        conn, cur = None, None
+        try:
+            conn = db.getConnection()
+            cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
+            cur.execute(LOAD_CASESTATE_SQL, {'id': self.master_id})
+            result = cur.fetchone()
+            self.id          = result.get('id')
+            self.state       = result.get('status')
+            self.access      = result.get('zugriff')
+            self._last_date  = result.get('letzer_termin')
+            self.phase       = result.get('phase')
+        except:
+            log.exception('Failed loading State')
+            raise
+        finally:
+            db.recycleConnection(conn, cur)
+
+    def getStateLabel(self):
+        state_labels = {'1': _('case_state_label_open'),
+                        '2': _('case_state_label_open'),
+                        '3': _('case_state_label_outstanding_delete'),
+                        '4': _('case_state_label_outstanding_anon'),
+                        '5': _('case_state_label_anon'),
+                        }
+        return state_labels.get(str(self.state))
+
+    def isCmFinished(self):
+        return self.phase in g.mpuls_config.get('phases', 'finished') 
+
+    def getState(self):
+        return self.state
+
+    def setState(self, value):
+        conn, cur = None, None
+        try:
+            try:
+                conn = db.getConnection()
+                cur = conn.cursor()
+                fields =  {'status': value, 'id': self.id}
+                cur.execute(SET_CASESTATE_SQL, fields)
+                conn.commit()
+                self.state = value
+            finally:
+                db.recycleConnection(conn, cur)
+            return True
+        except:
+            traceback.print_exc(file=sys.stderr)
+        return False
+
+    def getPhase(self):
+        return self.phase
+
+    def setPhase(self, phase):
+        conn, cur = None, None
+        try:
+            try:
+                conn = db.getConnection()
+                cur = conn.cursor()
+                fields =  {'phase': phase, 'id': self.id}
+                cur.execute(SET_CASEPHASE_SQL, fields)
+                conn.commit()
+                self.phase = phase
+            finally:
+                db.recycleConnection(conn, cur)
+            return True
+        except:
+            traceback.print_exc(file=sys.stderr)
+        return False
+
+    def getPhaseDescription(self):
+        desc = []
+        phases = PhaseFactory().load(self.master_id)
+        phase = phases.get_current_phase()
+        desc.append(g.mpuls_config.get('phases', 'description')[0].get(phase.phase))
+        if phase.is_active():
+            desc.append(_('Running'))
+        else:
+            desc.append(_('Finished'))
+        return " ".join(desc)
+
+    def getAccessTime(self):
+        return self.access
+
+    def getLastDate(self):
+        """Returns the last occuring date from appointments, aids, or
+        the last accesstime
+        """
+        return self.last_date
+
+    def getMaxSavetime(self):
+        return Agency().getMaxSavetime()
+
+    def daysToMaxSavetime(self):
+        maxage = self.getMaxSavetime()
+        if self.isCmFinished():
+            age = datetime.now() - self.getAccessTime()
+        else:
+            age = datetime.now() - self.getLastDate()
+        if int(maxage) != 0:
+            return int(maxage) - age.days
+        return None



More information about the Mpuls-commits mailing list