[Mpuls-commits] r4863 - in base/trunk: . mpulsweb/controllers mpulsweb/lib mpulsweb/model

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Tue Apr 5 16:02:33 CEST 2011


Author: torsten
Date: 2011-04-05 16:02:28 +0200 (Tue, 05 Apr 2011)
New Revision: 4863

Modified:
   base/trunk/ChangeLog
   base/trunk/mpulsweb/controllers/case.py
   base/trunk/mpulsweb/controllers/case_bundle.py
   base/trunk/mpulsweb/lib/export.py
   base/trunk/mpulsweb/model/case.py
Log:
Implemented serial letters according to MEP-1.  Export now always includes all defined fields in the defined order (specified by sql query).


Modified: base/trunk/ChangeLog
===================================================================
--- base/trunk/ChangeLog	2011-04-05 09:43:21 UTC (rev 4862)
+++ base/trunk/ChangeLog	2011-04-05 14:02:28 UTC (rev 4863)
@@ -1,3 +1,12 @@
+2011-04-05  Torsten Irlaender <torsten at intevation.de>
+
+	* mpulsweb/model/case.py,
+	mpulsweb/controllers/case_bundle.py,
+	mpulsweb/controllers/case.py,
+	mpulsweb/lib/export.py: Implemented serial letters according to MEP-1.
+	Export now always includes all defined fields in the defined order
+	(specified by sql query). 
+
 2011-03-31  Ludwig Reiter <ludwig.reiter at intevation.de>
 
 	* mpulsweb/model/appointment.py

Modified: base/trunk/mpulsweb/controllers/case.py
===================================================================
--- base/trunk/mpulsweb/controllers/case.py	2011-04-05 09:43:21 UTC (rev 4862)
+++ base/trunk/mpulsweb/controllers/case.py	2011-04-05 14:02:28 UTC (rev 4863)
@@ -562,8 +562,8 @@
     def formletter(self, id, letterid=1):
         id = self._checkInt(id)
         case = self._loadCase(id)
-        d = case.formletter(letterid)
-        export = FormletterExport([d]).export() 
+        d,o = case.formletter(letterid)
+        export = FormletterExport([d], o).export() 
         return self._download_data(export, "application/msexcel", "xls")
 
 

Modified: base/trunk/mpulsweb/controllers/case_bundle.py
===================================================================
--- base/trunk/mpulsweb/controllers/case_bundle.py	2011-04-05 09:43:21 UTC (rev 4862)
+++ base/trunk/mpulsweb/controllers/case_bundle.py	2011-04-05 14:02:28 UTC (rev 4863)
@@ -555,8 +555,8 @@
     def formletterAction(self):
         letter_id = request.params.get('letterid')
         case_bundle = session.get('casebundle')
-        d = case_bundle.formletter(letter_id)
-        export = FormletterExport(d).export() 
+        d,o = case_bundle.formletter(letter_id)
+        export = FormletterExport(d,o).export() 
         return self._download_data(export, "application/msexcel", "xls")
 
     def _download_data(self, data, mime_type, extension, **extra):

Modified: base/trunk/mpulsweb/lib/export.py
===================================================================
--- base/trunk/mpulsweb/lib/export.py	2011-04-05 09:43:21 UTC (rev 4862)
+++ base/trunk/mpulsweb/lib/export.py	2011-04-05 14:02:28 UTC (rev 4863)
@@ -293,10 +293,15 @@
 
 class FormletterExport(Export):
 
-    def __init__(self, data):
+    def __init__(self, data, order=None):
         self.data = data
-        self.xls = FormletterXLSTable('formletter', data[0].keys())
+        if order:
+            self.order = order
+        else:
+            self.order =  data[0].keys()
 
+        self.xls = FormletterXLSTable('formletter', self.order)
+
     def export(self):
         for row in self.data: 
             self.xls.append(row)

Modified: base/trunk/mpulsweb/model/case.py
===================================================================
--- base/trunk/mpulsweb/model/case.py	2011-04-05 09:43:21 UTC (rev 4862)
+++ base/trunk/mpulsweb/model/case.py	2011-04-05 14:02:28 UTC (rev 4863)
@@ -28,6 +28,7 @@
 #
 
 import logging
+import re
 from datetime import datetime
 from sets import Set
 from xml.etree import ElementTree
@@ -439,13 +440,15 @@
     def formletter(self, letterid):
         factory = g.case_factory
         l = []
+        o = None
         for id in self.case_list:
             case = factory.loadById(id)
             try:
-                l.append(case.formletter(letterid))
+                d,o = case.formletter(letterid)
+                l.append(d)
             except Exception, e:
                 log.exception(e)
-        return l 
+        return l,o 
 
     def _exportXML(self, tree):
         root = tree.getroot()
@@ -1020,9 +1023,22 @@
     def formletter(self, id):
         """Returns a dictionary with fields required for a formletter""" 
         fields = {}
+        order = []
         letter = None
         conn, cur = None, None
 
+        def get_fields(sql):
+            fields = []
+            sql = sql.lower()
+            r = re.compile('select (.*) from') 
+            m = r.match(sql)
+            if m:
+                fields_str = m.group(1)
+                fields_raw = fields_str.split(',')
+                for f in fields_raw:
+                    fields.append(f.split('as')[-1].strip())
+            return fields
+
         # Search for formletter
         for letter in g.mpuls_config.get('case', 'formletter'):
             if letter.get('id') == str(id): break
@@ -1033,6 +1049,7 @@
                 cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
                 d = {'id': self.id}
                 for sql in letter.get('sql'):
+                    order.extend(get_fields(sql)) 
                     cur.execute(sql, d)
                     result = cur.fetchone()
                     if result:
@@ -1042,7 +1059,7 @@
                 db.recycleConnection(conn, cur)
         except:
             log.exception(_('Error on fetching data for formletter'))
-        return fields 
+        return fields, order 
 
 class SessionCase:
 



More information about the Mpuls-commits mailing list