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

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Wed Feb 10 17:24:09 CET 2010


Author: torsten
Date: 2010-02-10 17:24:06 +0100 (Wed, 10 Feb 2010)
New Revision: 1473

Added:
   wasko/branches/2.0/mpulsweb/model/appointment.py
Log:
* mpulsweb/model/appointment.py: New. Copied from
  waskaweb.model.appointments. Deleted case-related functions.


Copied: wasko/branches/2.0/mpulsweb/model/appointment.py (from rev 1472, wasko/branches/2.0/waskaweb/model/appointment.py)
===================================================================
--- wasko/branches/2.0/waskaweb/model/appointment.py	2010-02-10 16:18:03 UTC (rev 1472)
+++ wasko/branches/2.0/mpulsweb/model/appointment.py	2010-02-10 16:24:06 UTC (rev 1473)
@@ -0,0 +1,381 @@
+# -*- coding: utf-8 -*-
+#
+# Copyright 2007, 2008 Intevation GmbH, Germany, <info at intevation.de>
+#
+# 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
+# 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
+# European Social Fund resources.
+#
+# Authors:
+# Torsten Irländer <torsten.irlaender at intevation.de>
+#
+
+from datetime import datetime, timedelta
+
+import mpulsweb.lib.helpers as h
+from mpulsweb.lib.base import session
+from mpulsweb.lib.db import db
+
+GET_GLOBAL_APPOINTMENT_SQL = """\
+SELECT id, master_id, sdatum, edatum, name, beschreibung, art
+FROM ka_global_termine_tbl_view
+WHERE id = %(id)s
+"""
+GET_GLOBAL_APPOINTMENTLIST_SQL = """\
+SELECT id, master_id, sdatum, edatum, name, beschreibung, art
+FROM ka_global_termine_tbl_view
+WHERE coalesce(edatum, sdatum)::date+1 >= %(date)s::date ORDER BY sdatum
+"""
+DELETE_GLOBAL_APPOINTMENT_SQL = "SELECT delete_global_termin_ds(%(id)s)"
+CREATE_GLOBAL_APPOINTMENT_SQL = "SELECT create_global_termin_ds()"
+STORE_GLOBAL_APPOINTMENT_SQL = """\
+UPDATE ka_global_termine_tbl_view
+SET
+    sdatum = %(start_date)s::timestamp,
+    edatum = %(end_date)s::timestamp,
+    name = %(title)s,
+    beschreibung = %(description)s,
+    art = %(type)s
+WHERE id = %(id)s
+"""
+
+GET_CASE_APPOINTMENT_SQL = """\
+SELECT id, master_id, sdatum, edatum, name, beschreibung, art
+FROM ka_fall_termine_tbl_view
+WHERE id = %(id)s
+"""
+
+GET_CASE_APPOINTMENTLIST_SQL = """\
+SELECT id, master_id, sdatum, edatum, name, beschreibung, art
+FROM ka_fall_termine_tbl_view
+WHERE master_id = %(id)s
+      AND coalesce(edatum, sdatum)::date+1 >= %(date)s::date
+      AND art = 0
+ORDER BY sdatum
+"""
+
+GET_CASE_REMINDERLIST_SQL = """\
+SELECT id, master_id, sdatum, edatum, name, beschreibung, art
+FROM ka_fall_termine_tbl_view
+WHERE master_id = %(id)s AND art = 1
+ORDER BY sdatum
+"""
+
+GET_ALLCASES_APPOINTMENTLIST_SQL = """\
+SELECT a.id, a.master_id, a.sdatum, a.edatum, a.name, a.beschreibung, a.art
+FROM ka_fall_termine_tbl_view a JOIN master_tbl_view m ON m.id = a.master_id
+WHERE coalesce(edatum, sdatum)::date+1 >= %(date)s::date
+      AND art = 0 AND m.bearbeiter_id = %(editor)s
+ORDER BY sdatum
+"""
+
+GET_ALLCASES_REMINDERLIST_SQL = """\
+SELECT a.id, a.master_id, a.sdatum, a.edatum, a.name, a.beschreibung, a.art
+FROM ka_fall_termine_tbl_view a JOIN master_tbl_view m ON m.id = a.master_id
+WHERE art = 1 AND m.bearbeiter_id = %(editor)s
+ORDER BY sdatum
+"""
+
+DELETE_CASE_APPOINTMENT_SQL = "SELECT delete_fall_termin_ds(%(id)s)"
+CREATE_CASE_APPOINTMENT_SQL = "SELECT create_fall_termin_ds(%(id)s)"
+STORE_CASE_APPOINTMENT_SQL = """\
+UPDATE ka_fall_termine_tbl_view
+SET
+    sdatum = %(start_date)s::timestamp,
+    edatum = %(end_date)s::timestamp,
+    name = %(title)s,
+    beschreibung = %(description)s,
+    art = %(type)s
+WHERE id = %(id)s
+"""
+
+class AppointmentFactory:
+
+    def loadById(self, id):
+        appointment = None
+        try:
+            conn = db.getConnection()
+            cur = conn.cursor()
+            cur.execute(self.get_sql, {'id': id})
+            row = cur.fetchone()
+            appointment = self.createByDBRow(row)
+        finally:
+            db.recycleConnection(conn, cur)
+        return appointment
+
+    def createByData(self, id=None, title=None, sdate=None, edate=None,
+                     desc=None, case_id=None, type=0):
+        appointment             = self.constructor()
+        appointment.id          = id
+        appointment.start_date  = sdate
+        appointment.end_date    = edate
+        appointment.title       = title
+        appointment.description = desc
+        appointment.case_id     = case_id
+        appointment.type        = type
+        return appointment
+
+    def createByDBRow(self, row):
+        id        = row[0]
+        master_id = row[1]
+        sdate     = row[2]
+        edate     = row[3]
+        name      = h.ensure_unicode(row[4])
+        desc      = h.ensure_unicode(row[5])
+        type      = row[6]
+        return self.createByData(id, name, sdate, edate, desc, master_id, type)
+
+    def createNew(self, case_id=None):
+        try:
+            conn = db.getConnection()
+            cur = conn.cursor()
+            if case_id:
+                cur.execute(self.create_sql, {'id': case_id})
+            else:
+                cur.execute(self.create_sql)
+            row = cur.fetchone()
+            id = row[0]
+            return self.createByData(id)
+        finally:
+            db.recycleConnection(conn, cur)
+        return None
+
+
+class CaseAppointmentFactory(AppointmentFactory):
+
+    def __init__(self):
+        self.get_sql = GET_CASE_APPOINTMENT_SQL
+        self.create_sql = CREATE_CASE_APPOINTMENT_SQL
+        self.constructor = CaseAppointment
+
+
+class CaseReminderFactory(AppointmentFactory):
+
+    def __init__(self):
+        self.get_sql = GET_CASE_APPOINTMENT_SQL
+        self.create_sql = CREATE_CASE_APPOINTMENT_SQL
+        self.constructor = CaseReminder
+
+
+class GlobalAppointmentFactory(AppointmentFactory):
+
+    def __init__(self):
+        self.get_sql = GET_GLOBAL_APPOINTMENT_SQL
+        self.create_sql = CREATE_GLOBAL_APPOINTMENT_SQL
+        self.constructor = GlobalAppointment
+
+
+class AppointmentOverview:
+
+    def _load(self, factory, sql, values):
+        self.appointment_list = []
+        conn, cur = None, None
+        try:
+            conn = db.getConnection()
+            cur = conn.cursor()
+            cur.execute(sql, values)
+            rows = cur.fetchall()
+            for row in rows:
+                self.appointment_list.append(factory.createByDBRow(row))
+        finally:
+            db.recycleConnection(conn, cur)
+
+    def getDatasets(self):
+        return self.appointment_list
+
+    def setDatasets(self, list):
+        self.appointment_list = list
+
+    def join(self, overview):
+        self.appointment_list.extend(overview.getDatasets())
+
+    def sort(self, cmp=None):
+        self.appointment_list = sorted(self.appointment_list)
+
+
+class CaseAppointmentOverview(AppointmentOverview):
+
+    def __init__(self, caseid=None):
+        afactory = CaseAppointmentFactory()
+        today = datetime.now().replace(hour=0, minute=0, second=0,
+                                       microsecond=0)
+        if caseid:
+            values = {'id': caseid, 'date': today}
+            sql = GET_CASE_APPOINTMENTLIST_SQL
+        else:
+            user = session['USER_AUTHORIZED']
+            values = {'date': today, 'editor': user.id}
+            sql = GET_ALLCASES_APPOINTMENTLIST_SQL
+        self._load(afactory, sql, values)
+
+
+class GlobalAppointmentOverview(AppointmentOverview):
+
+    def __init__(self):
+        afactory = GlobalAppointmentFactory()
+        today = datetime.now().replace(hour=0, minute=0, second=0,
+                                       microsecond=0)
+        values = {'date': today}
+        sql = GET_GLOBAL_APPOINTMENTLIST_SQL
+        self._load(afactory, sql, values)
+
+
+class CaseReminderOverview(AppointmentOverview):
+
+    def __init__(self, caseid=None):
+        afactory = CaseReminderFactory()
+        today = datetime.now().replace(hour=0, minute=0, second=0,
+                                       microsecond=0)
+        if caseid:
+            values = {'id': caseid, 'date': today}
+            sql = GET_CASE_REMINDERLIST_SQL
+        else:
+            user = session['USER_AUTHORIZED']
+            values = {'date': today, 'editor': user.id}
+            sql = GET_ALLCASES_REMINDERLIST_SQL
+        self._load(afactory, sql, values)
+
+class Appointment:
+
+    def __init__(self):
+        self.id = None
+        self.title = None
+        self.start_date = None
+        self.end_date = None
+        self.description = None
+        self.case_id = None
+        # available types
+        # 0: normal appointment
+        # 1: Reminder
+        self.type = 0
+
+    def __cmp__(self, other):
+        return cmp(self.start_date, other.start_date)
+
+    def _convertStr2Date(self, str, format=None):
+        str = str.strip()
+        date, time = str.split(' ')
+        try:
+            d, m, Y = date.split('.')
+        except:
+            return None
+        try:
+            H, M = time.split(':')
+        except:
+            H, M = (0, 0)
+        return datetime(int(Y), int(m), int(d), int(H), int(M))
+
+    def getStartDate(self):
+        return self.start_date
+
+    def getEndDate(self):
+        return self.end_date
+
+    def getDuration(self, empty=''):
+        if self.end_date:
+            return h.human_timedelta(self.end_date - self.start_date)
+        return empty
+
+    def setData(self, data):
+        """Sets values of the appointment to data.
+        Data is a dict returned from formencode
+        """
+        self.title = data.get('title', self.title)
+        self.type = data.get('type', self.type)
+
+        # Set date
+        sdate = "%s %s" % (data.get('start_date'), data.get('start_time'))
+        self.start_date = self._convertStr2Date(sdate)
+
+        ## If not end_date is given, than end_date is equal to start_date
+        #if not data.get('end_date') or not data.get('end_time'):
+        #    self.end_date   = self.start_date
+        #else:
+        #    edate = "%s %s" % (data.get('end_date'), data.get('end_time'))
+        #    self.end_date   = self._convertStr2Date(edate)
+
+        edate = "%s %s" % (data.get('end_date'), data.get('end_time'))
+        self.end_date = self._convertStr2Date(edate)
+
+        self.description = data.get('description', self.description)
+        self.case_id = data.get('case_id', self.case_id)
+
+    def store(self):
+        conn, cur = None, None
+        fields = {'id': self.id, 'case_id': self.case_id,
+                  'type': self.type,
+                  'title': self.title, 'description': self.description,
+                  'start_date': self.start_date, 'end_date': self.end_date,
+                  }
+        try:
+            conn = db.getConnection()
+            cur = conn.cursor()
+            cur.execute(self.store_sql, fields)
+            conn.commit()
+            return True
+        finally:
+            db.recycleConnection(conn, cur)
+        return False
+
+    def isGlobal(self):
+        return self.case_id is None
+
+    def isReminder(self):
+        return self.type == 1
+
+    def delete(self):
+        conn, cur = None, None
+        try:
+            conn = db.getConnection()
+            cur = conn.cursor()
+            cur.execute(self.delete_sql, {'id': self.id})
+            conn.commit()
+            return True
+        finally:
+            db.recycleConnection(conn, cur)
+        return False
+
+
+class GlobalAppointment(Appointment):
+
+    def __init__(self):
+        Appointment.__init__(self)
+        self.type = 0
+        self.store_sql = STORE_GLOBAL_APPOINTMENT_SQL
+        self.delete_sql = DELETE_GLOBAL_APPOINTMENT_SQL
+
+
+class CaseAppointment(Appointment):
+
+    def __init__(self):
+        Appointment.__init__(self)
+        self.type = 0
+        self.store_sql = STORE_CASE_APPOINTMENT_SQL
+        self.delete_sql = DELETE_CASE_APPOINTMENT_SQL
+
+
+class CaseReminder(CaseAppointment):
+
+    def __init__(self):
+        CaseAppointment.__init__(self)
+        self.type = 1
+
+# vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8:


Property changes on: wasko/branches/2.0/mpulsweb/model/appointment.py
___________________________________________________________________
Name: svn:mergeinfo
   + 



More information about the Mpuls-commits mailing list