[Mpuls-commits] r3356 - in wasko/trunk: . waskoweb/controllers

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Tue Aug 10 14:44:55 CEST 2010


Author: roland
Date: 2010-08-10 14:44:54 +0200 (Tue, 10 Aug 2010)
New Revision: 3356

Modified:
   wasko/trunk/ChangeLog
   wasko/trunk/waskoweb/controllers/formularpage.py
Log:
issue933: create a reminder for when the CM phase exceeds the maximum allowed

Modified: wasko/trunk/ChangeLog
===================================================================
--- wasko/trunk/ChangeLog	2010-08-10 12:41:43 UTC (rev 3355)
+++ wasko/trunk/ChangeLog	2010-08-10 12:44:54 UTC (rev 3356)
@@ -1,5 +1,10 @@
 2010-08-10 Roland Geider <roland.geider at intevation.de>
+	
+	* waskoweb/controllers/formularpage.py: issue933: create a reminder
+	  for when the CM phase exceeds the maximum allowed
 
+2010-08-10 Roland Geider <roland.geider at intevation.de>
+
 	* waskoweb/public/formed/formedtree.xml: issue934: set rules for
 	  maximal duration of phases
 

Modified: wasko/trunk/waskoweb/controllers/formularpage.py
===================================================================
--- wasko/trunk/waskoweb/controllers/formularpage.py	2010-08-10 12:41:43 UTC (rev 3355)
+++ wasko/trunk/waskoweb/controllers/formularpage.py	2010-08-10 12:44:54 UTC (rev 3356)
@@ -64,27 +64,120 @@
     DIALOG_FULLAUTOMATIC_CHANGE_TITLE, DIALOG_FULLAUTOMATIC_CHANGE_TEXT, \
     DIALOG_HALFAUTOMATIC_CHANGE_TITLE, DIALOG_HALFAUTOMATIC_CHANGE_TEXT
 
+from mpulsweb.model.appointment import CaseAppointmentFactory
+
 from waskoweb.model.agencysettings import Agency
+from mpulsweb.lib.db import db
+import psycopg2 
 
+
 class FormularpageController(FormularpageController):
     @checkRole(('cm_ka'))
     def save(self):
+        
+        # Extract the case ID for later use
+        case_id = request.params['page'].split(':')[1]
+        
+        # Save the request parameters for later use
+        form_result = convert2dic(request.params)
+        
+        
         """
-        Overwrite the save method to allow for the automatic generation of the
-        evaluation ID
+        Calculate the total length of the CM, including extensions and create
+        a new appointment if the length is longer than a certain limit
+        
+        NOTE: this solution is not yet optimal
         """
+        if (u'erstgespraech:%s' % case_id in form_result or
+                u'cm_datum_verlaengerung_1:%s' % case_id in form_result or 
+                u'cm_datum_verlaengerung_2:%s' % case_id in form_result):
+            conn = db.getConnection()
+            cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
+            cur.execute('''SELECT erstgespraech,
+                                datum_cm_ende,
+                                cm_datum_verlaengerung_1,
+                                cm_datum_verlaengerung_2,
+                                fn,
+                                name,
+                                vorname
+                            FROM master_tbl_view
+                            WHERE id = %s''' % (case_id))
+            case_data = cur.fetchall()[0]
+            max_date = max([date for date in case_data[:3] if date != None])
+            date_extension_1 = case_data[2]
+            date_extension_2 = case_data[3]
+            first_date = case_data[0]
+            time_delta = max_date - first_date
+            
+            # Maximum of 18 months with 2 extensions
+            if (date_extension_1 != None and date_extension_2 != None
+                and time_delta.days > 18*31):
+                max_time_delta = datetime.timedelta(18*31)
+            # Maximum of 15 months with 1 extensions
+            elif (date_extension_1 != None and date_extension_2 == None
+                and time_delta.days > 15*31):
+                max_time_delta = datetime.timedelta(15*31)
+            # 12 months
+            elif (date_extension_1 == None and date_extension_2 == None
+                and time_delta.days > 12*31):
+                max_time_delta = datetime.timedelta(12*31)
+            
+            appointment_factory = CaseAppointmentFactory()
+            
+            # Use the first date from the DB as it is properly validated
+            # Try first to load the existing appointment and overwrite its data
+            # if one is found
+            #
+            # Searching by name and hoping that it didn't change is... ugly
+            search_appointment_sql = u"""SELECT id
+                                    FROM ka_fall_termine_tbl_view
+                                    WHERE name LIKE 'Zu langer Case-Management der Fallakte %s' 
+                                        AND art = 1""" % case_id
+            
+            cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
+            cur.execute(search_appointment_sql)
+            appointments = cur.fetchall()
+            
+            appointment = appointment_factory.createNew(case_id)
+            # Found appointment, update
+            if appointments:
+                appointment_id = appointments[0].pop() #ideally, we are cleverer
+                appointment = appointment_factory.loadById(appointment_id)
+            # No appointment found, new one
+            else:
+                appointment = appointment_factory.createNew(case_id)
+            
+            appointment_data = {'description': u'''Die Fallkate %s von %s %s 
+            überschreitet die maximale Dauer der Phase Case-Management.
+            Bitte korrigieren Sie Ihre Angaben, da die Fallakte andernfalls
+            nicht auswertbar ist.''' % (case_data[4], case_data[5], case_data[6]),
+                            'title': u'Zu langer Case-Management der Fallakte %s' % case_id,
+                            'case_id': case_id,
+                            'start_time': datetime.time(00, 00),
+                            'start_date': first_date + max_time_delta,
+                            'end_time': None,
+                            'end_date': None,
+                            'type': u'1', #reminder
+                            'id': u''
+                            }
+            
+            appointment.setData(appointment_data)
+            appointment.store()
+        
+        """
+        Generate the evaluation ID
+        """
         factory = InstanceFactory(g.formedTree, PostgresDBInterface())
         instance_tree = factory.loadInstanceTreeByIdentifier(
             session['case'].id,
             session.get('uncommited_fields'))
         page_id = request.params['page']
         try:
-            form_result = convert2dic(request.params)
+            
             apply_hidden_booleans(form_result)
 
             # Special logic for the Evaluation-ID
             if request.params['page'].split(':')[0] == 'page-2':
-                case_id = request.params['page'].split(':')[1]
                 
                 # Reset the evaluation number is the young adult does not want
                 # to participate in the survey or does not want his personal
@@ -138,7 +231,9 @@
             session['uncommited_fields'] = instance_tree.commit()
             session.save()
             
-        # Check requried fields of the case and set phase 
+        """
+        (Half)Automatic phase change
+        """
         changemode = g.mpuls_config.get('phases', 'changemode')
         if changemode != PC_MANUAL:
             phases = PhaseFactory().load(session['case'].id)



More information about the Mpuls-commits mailing list