[Mpuls-commits] r2528 - in wasko/branches/2.0/mpulsweb: controllers lib model

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Tue Apr 27 10:38:40 CEST 2010


Author: torsten
Date: 2010-04-27 10:38:38 +0200 (Tue, 27 Apr 2010)
New Revision: 2528

Modified:
   wasko/branches/2.0/mpulsweb/controllers/case_bundle.py
   wasko/branches/2.0/mpulsweb/lib/export.py
   wasko/branches/2.0/mpulsweb/model/case.py
Log:
Added first version of xls export.


Modified: wasko/branches/2.0/mpulsweb/controllers/case_bundle.py
===================================================================
--- wasko/branches/2.0/mpulsweb/controllers/case_bundle.py	2010-04-27 06:33:19 UTC (rev 2527)
+++ wasko/branches/2.0/mpulsweb/controllers/case_bundle.py	2010-04-27 08:38:38 UTC (rev 2528)
@@ -233,7 +233,7 @@
                     return self._empty_caselist()
                 session['casebundle'] = case_bundle
                 session.save()
-                return self.exportCSV(0)
+                return self.export('CSV')
 
             if action == 'exportXLS':
                 # Fiter out case which are not open(1) or inavtice (2)
@@ -243,7 +243,7 @@
                     return self._empty_caselist()
                 session['casebundle'] = case_bundle
                 session.save()
-                return self.exportXLS(0)
+                return self.export('XLS')
 
         except formencode.Invalid, error:
             log.exception(error)

Modified: wasko/branches/2.0/mpulsweb/lib/export.py
===================================================================
--- wasko/branches/2.0/mpulsweb/lib/export.py	2010-04-27 06:33:19 UTC (rev 2527)
+++ wasko/branches/2.0/mpulsweb/lib/export.py	2010-04-27 08:38:38 UTC (rev 2528)
@@ -1,28 +1,141 @@
+import sys
+from types import StringTypes, IntType, LongType
+from StringIO import StringIO
 import logging
+
 import pyExcelerator
 
+from formed.meta.data import PageNode, WidgetCollector, RepeatNode
+
+from mpulsweb.lib.base import g 
 log = logging.getLogger(__name__)
 
+class MyStringIO(StringIO):
+
+    def close(self):
+        pass
+
+    def real_close(self):
+        MyStringIO.close(self)
+
+class FakeStr(str):
+    def __init__(self, buf):
+        self.buf = buf
+
+def fake_file(x, *args):
+    return isinstance(x, FakeStr) and x.buf or file(x, *args)
+
+pyExcelerator.CompoundDoc.file = fake_file
+
+def to_str(x):
+    t = type(x)
+    if t in (IntType, LongType):
+        return x
+    if t in StringTypes:
+        x = unicode(str(x), "utf-8")
+        if len(x) > 32765: x = x[:32765]
+        return x
+    return str(x)
+
+class Sheet(object):
+
+    def __init__(self, worksheet, column_names, rg=False, rgname=None):
+
+        for idx, name in enumerate(column_names):
+            worksheet.write(0, idx, name)
+
+        self.worksheet = worksheet
+        self.names     = column_names
+        self.rg        = rg 
+        self.rgname    = rgname
+        self.row       = 1
+        self.column    = 0
+
+    def next_row(self):
+        self.row += 1
+        self.column = 0
+
+    def add_data(self, value):
+        if not value is None:
+            self.worksheet.write(self.row, self.column, to_str(value))
+        self.column += 1
+
+class Book:
+
+    def __init__(self, tree, selection):
+        self.tree = tree
+        self.selection = selection
+        self.workbook  = pyExcelerator.Workbook()
+        self.collectors = {} 
+        self.sheets = {} 
+        self.pages = g.formedTree.findAllByClass(PageNode)
+
+        for p in self.pages:
+            rg = False
+            rgname = None
+            # Is this a rg?
+            if isinstance(p.getParent(), RepeatNode):
+                rg = True
+                rgname = p.getParent().getName()
+            collector = WidgetCollector()
+            p.visit(collector.visitor)
+
+            if p.getName() not in self.selection: continue
+
+            sheet = self.workbook.add_sheet(p.getDescription())
+            names = [w.getName() for w in collector.widgets]
+
+            #save
+            self.sheets[p.getName()] = Sheet(sheet,names,rg,rgname)
+            self.collectors[p.getName()] = collector 
+
+    def fill(self):
+        for name, sheet in self.sheets.iteritems():
+            for case in self.tree.getiterator('master'):
+                # Handling of rg. Does not work for nested rg.
+                if sheet.rg:
+                    for rg in case.findall(sheet.rgname):
+                        for col, field in enumerate(sheet.names):
+                            try:
+                                value = rg.find(field).text
+                            except:
+                                value = ""
+                            sheet.add_data(value)
+                        sheet.next_row()
+                else:
+                    for col, field in enumerate(sheet.names):
+                        try:
+                            value = case.find(field).text
+                        except:
+                            value = ""
+                        sheet.add_data(value)
+                    sheet.next_row()
+
+    def write_xls(self, fname):
+        self.workbook.save(fname)
+
 class Export:
     pass
 
 class XLSExport(Export):
 
-    def __init__(self):
-        self.workbook  = pyExcelerator.Workbook()
+    def __init__(self, tree, selection):
+        self.book = Book(tree, selection)
 
-    def export(self, xmltree):
-        for item in xmltree:
-            log.debug(item)
-        return "Test XLS"
+    def export(self):
+        self.book.fill()
+        try:
+            out = MyStringIO()
+            self.book.write_xls(FakeStr(out))
+            return out.getvalue()
+        finally:
+            out.real_close()
 
 class CSVExport(Export):
 
-    def __init__(self):
+    def __init__(self, tree, selection):
         self.csv = "" 
 
-    def export(self, xmltree):
-        for item in xmltree:
-            log.debug(item)
+    def export(self):
         return "Test CSV"
         

Modified: wasko/branches/2.0/mpulsweb/model/case.py
===================================================================
--- wasko/branches/2.0/mpulsweb/model/case.py	2010-04-27 06:33:19 UTC (rev 2527)
+++ wasko/branches/2.0/mpulsweb/model/case.py	2010-04-27 08:38:38 UTC (rev 2528)
@@ -411,9 +411,9 @@
         if type == "XML":
             export = self._exportXML(xmltree) 
         elif type== "XLS":
-            export = self._exportXSL(xmltree) 
+            export = self._exportXSL(xmltree, selection) 
         elif type== "CSV":
-            export = self._exportCSV(xmltree) 
+            export = self._exportCSV(xmltree, selection) 
 
         return num, export
 
@@ -422,11 +422,11 @@
         xml = ElementTree.tostring(root)
         return xml
 
-    def _exportXSL(self, tree):
-        return XLSExport().export(tree)
+    def _exportXSL(self, tree, selection):
+        return XLSExport(tree, selection).export()
     
     def _exportCSV(self, tree):
-        return CSVExport().export(tree)
+        return CSVExport(tree, selection).export()
 
 
 class MpulsCaseStandin:



More information about the Mpuls-commits mailing list