[Mpuls-commits] r115 - in waska/trunk: . waskaweb/controllers waskaweb/lib waskaweb/model waskaweb/templates/casemanagement

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Tue Sep 2 14:29:06 CEST 2008


Author: teichmann
Date: 2008-09-02 14:29:05 +0200 (Tue, 02 Sep 2008)
New Revision: 115

Modified:
   waska/trunk/ChangeLog.txt
   waska/trunk/waskaweb/controllers/agency_overview.py
   waska/trunk/waskaweb/controllers/case_overview.py
   waska/trunk/waskaweb/lib/excel.py
   waska/trunk/waskaweb/lib/exportselection.py
   waska/trunk/waskaweb/model/case.py
   waska/trunk/waskaweb/templates/casemanagement/downloadXLS.mako
   waska/trunk/waskaweb/templates/casemanagement/selectParts.mako
Log:
Partial XLS export


Modified: waska/trunk/ChangeLog.txt
===================================================================
--- waska/trunk/ChangeLog.txt	2008-09-02 11:11:41 UTC (rev 114)
+++ waska/trunk/ChangeLog.txt	2008-09-02 12:29:05 UTC (rev 115)
@@ -1,3 +1,23 @@
+2008-09-02  Sascha L. Teichmann <sascha.teichmann at intevation.de>
+
+	Partial XLS export
+
+	* waskaweb/lib/excel.py: uses the selection lib now.
+
+	* waskaweb/lib/exportselection.py: Added a convinience method to lookup views.
+
+	* waskaweb/model/case.py: uses selection for XLS export now. 
+
+	* waskaweb/controllers/case_overview.py: Rewired to integrate parts selection.
+
+	* waskaweb/controllers/agency_overview.py: XXX: Some controller is missing!
+	  Check this if eval server is testes the next time.
+
+	* waskaweb/templates/casemanagement/selectParts.mako: Make it more general to
+	  be useful for XLS and CSV too.
+
+	*  waskaweb/templates/casemanagement/downloadXLS.mako: Added link to part selection.
+
 2008-09-02	Torsten Irlaender  <torsten.irlaender at intevation.de>
 
 	Adjusted evaluation to work with new phases model. TODO: Consolidate

Modified: waska/trunk/waskaweb/controllers/agency_overview.py
===================================================================
--- waska/trunk/waskaweb/controllers/agency_overview.py	2008-09-02 11:11:41 UTC (rev 114)
+++ waska/trunk/waskaweb/controllers/agency_overview.py	2008-09-02 12:29:05 UTC (rev 115)
@@ -311,12 +311,37 @@
 
     @checkRole(('pb_ka'))
     def downloadXLS(self):
-        c.success_for  = EXPORT_SUCCESS_XLS
-        c.success_text = EXPORT_SUCCESS_XLS_EXPLAINATION
-        c.download_link = h.url_for(action="downloadXLSAction")
-        c.overview_link = h.url_for(action="overview")
+        selection_checker = self.__getSelectionChecker()
+        c.is_all_selected = selection_checker.isAllSelected()
+        c.selection_link = h.url_for(action="selectXLSParts")
+        c.download_link  = h.url_for(action="downloadXLSAction")
+        c.overview_link  = h.url_for(action="overview")
         return render('casemanagement/downloadXLS.mako')
 
+    @checkRole('pb_ka')
+    def selectXLSParts(self):
+        selection_checker  = self.__getSelectionChecker()
+        c.selection_question = SELECT_PARTS_FOR_EXPORT
+        c.store_action       = h.url_for(action="storeSelectedXLSParts")
+        c.back_url           = h.url_for(controller='/case_overview', action='exportXLS', confirmed=1)
+        c.back_title         = "XLS Export"
+        page = render('casemanagement/selectParts.mako')
+        return formencode.htmlfill.render(unicode(page, 'utf-8'),defaults=selection_checker.selections)
+
+    @checkRole('pb_ka')
+    def storeSelectedXLSParts(self):
+        validator = CasePartSelectionForm()
+        selection_checker = self.__getSelectionChecker()
+        try:
+            params = validator.to_python(request.params)
+            for key, value in params.iteritems():
+                selection_checker.setChecked(key, value)
+            session['selected_export_parts'] = selection_checker
+            session.save()
+        except formencode.Invalid, error:
+            return selectXLSParts()
+        return self.exportXLS(1)
+
     @checkRole(('pb_ka'))
     def downloadCSVAction(self):
         agency_bundle = session.get('agencybundle')
@@ -369,7 +394,8 @@
         try:
             if case_ids:
                 case_bundle = CaseBundle(case_ids)
-                xls = case_bundle.exportAsXLS()
+                selection_checker = self.__getSelectionChecker()
+                xls = case_bundle.exportAsXLS(selection_checker, anonymize)
                 data = paste.fileapp.DataApp(
                     xls,
                     content_type        = 'application/msexcel',
@@ -380,7 +406,6 @@
         except:
             return render('/tests/trace.mako')
 
-
     #@checkRole(('pb_ka', 'cm_ka'))
     #def exportBundle(self, confirmed):
     #    confirmed           = self._checkBool(confirmed)

Modified: waska/trunk/waskaweb/controllers/case_overview.py
===================================================================
--- waska/trunk/waskaweb/controllers/case_overview.py	2008-09-02 11:11:41 UTC (rev 114)
+++ waska/trunk/waskaweb/controllers/case_overview.py	2008-09-02 12:29:05 UTC (rev 115)
@@ -722,10 +722,22 @@
         selection_checker  = self.__getSelectionChecker()
         c.selection_question = SELECT_PARTS_FOR_EXPORT
         c.store_action       = h.url_for(action="storeSelectedXMLParts")
+        c.back_url           = h.url_for(controller='/case_overview', action='exportXML', confirmed=1)
+        c.back_title         = "XML Export"
         page = render('casemanagement/selectParts.mako')
         return formencode.htmlfill.render(unicode(page, 'utf-8'),defaults=selection_checker.selections)
 
     @checkRole(('cm_ka', 'admin_ka'))
+    def selectXLSParts(self):
+        selection_checker  = self.__getSelectionChecker()
+        c.selection_question = SELECT_PARTS_FOR_EXPORT
+        c.store_action       = h.url_for(action="storeSelectedXLSParts")
+        c.back_url           = h.url_for(controller='/case_overview', action='exportXLS', confirmed=1)
+        c.back_title         = "XLS Export"
+        page = render('casemanagement/selectParts.mako')
+        return formencode.htmlfill.render(unicode(page, 'utf-8'),defaults=selection_checker.selections)
+
+    @checkRole(('cm_ka', 'admin_ka'))
     def storeSelectedXMLParts(self):
         validator = CasePartSelectionForm()
         selection_checker = self.__getSelectionChecker()
@@ -740,9 +752,26 @@
         return self.exportXML(1)
 
     @checkRole(('cm_ka', 'admin_ka'))
+    def storeSelectedXLSParts(self):
+        validator = CasePartSelectionForm()
+        selection_checker = self.__getSelectionChecker()
+        try:
+            params = validator.to_python(request.params)
+            for key, value in params.iteritems():
+                selection_checker.setChecked(key, value)
+            session['selected_export_parts'] = selection_checker
+            session.save()
+        except formencode.Invalid, error:
+            return selectXLSParts()
+        return self.exportXLS(1)
+
+    @checkRole(('cm_ka', 'admin_ka'))
     def downloadXLS(self):
-        c.download_link = h.url_for(action="downloadXLSAction")
-        c.overview_link = h.url_for(action="overview")
+        selection_checker = self.__getSelectionChecker()
+        c.is_all_selected = selection_checker.isAllSelected()
+        c.selection_link = h.url_for(action="selectXLSParts")
+        c.download_link  = h.url_for(action="downloadXLSAction")
+        c.overview_link  = h.url_for(action="overview")
         return render('casemanagement/downloadXLS.mako')
 
     @checkRole(('cm_ka', 'admin_ka'))
@@ -794,7 +823,8 @@
         try:
             if case_ids:
                 case_bundle = CaseBundle(case_ids)
-                xls = case_bundle.exportAsXLS(anonymize)
+                selection_checker = self.__getSelectionChecker()
+                xls = case_bundle.exportAsXLS(selection_checker, anonymize)
                 data = paste.fileapp.DataApp(
                     xls,
                     content_type        = 'application/msexcel',

Modified: waska/trunk/waskaweb/lib/excel.py
===================================================================
--- waska/trunk/waskaweb/lib/excel.py	2008-09-02 11:11:41 UTC (rev 114)
+++ waska/trunk/waskaweb/lib/excel.py	2008-09-02 12:29:05 UTC (rev 115)
@@ -32,31 +32,21 @@
 
 from pyExcelerator import Workbook
 
-SELECT_MASTER = \
-"""SELECT * FROM %s WHERE id = %%(id)s"""
+from exportselection import CasePartsSelection, SelectorFactory
 
 FETCH_ALL_IDS = \
 """SELECT id FROM master_tbl_view"""
 
 RG_VIEWS = (
-    ("Kompetenzfestellung", "rg_kompetenzfestellung_tbl_view"),
-    ("Angebote BQ",         "rg_angebote_berufliche_qualifizierung_tbl_view"),
-    ("Angebote BV",         "rg_angebote_berufsvorbereitung_tbl_view"),
-    ("Angebote BB",         "rg_angebote_bildenden_bereich_tbl_view"),
-    ("Angebote LB",         "rg_angebote_lebensbewaeltigung_tbl_view"))
+    ("Kompetenzfeststellung", "kompetenzfestellung"),
+    ("Angebote BQ",           "angebote_berufliche_qualifizierung"),
+    ("Angebote BV",           "angebote_berufsvorbereitung"),
+    ("Angebote BB",           "angebote_bildenden_bereich"),
+    ("Angebote LB",           "angebote_lebensbewaeltigung"))
 
-RG_VIEWS_ANON = (
-    ("Kompetenzfestellung", "rg_kompetenzfestellung_tbl_anonym_view"),
-    ("Angebote BQ",         "rg_angebote_berufliche_qualifizierung_tbl_anonym_view"),
-    ("Angebote BV",         "rg_angebote_berufsvorbereitung_tbl_anonym_view"),
-    ("Angebote BB",         "rg_angebote_bildenden_bereich_tbl_anonym_view"),
-    ("Angebote LB",         "rg_angebote_lebensbewaeltigung_tbl_anonym_view"))
+BLACK_LIST = set(['id', 'master_id', 'uuid_id', 'bearbeiter_id'])
 
-SELECT_RG = \
-"""SELECT * FROM %s WHERE master_id = %d"""
 
-BLACK_LIST = set(['id', 'master_id', 'uuid_id', 'bearbeiter_id'])
-
 def to_str(x):
     t = type(x)
     if t in (IntType, LongType):
@@ -118,8 +108,11 @@
         columns.sort(lambda a, b: cmp(a[1], b[1]))
         self.add_dataset([(a[0], row[a[1]]) for a in columns if a[0] not in BLACK_LIST])
 
-    def fetch_data(self, mid, cur):
-        cur.execute(SELECT_RG % (self.relation, mid))
+    def fetch_data(self, mid, cur, selector):
+        stmnt = selector.getSelectStatementByView(self.relation)
+        if not stmnt:
+            return
+        cur.execute(stmnt, { 'id': mid })
         while True:
             row = cur.fetchone()
             if not row: break
@@ -129,21 +122,24 @@
     def __init__(self, worksheet, relation):
         Sheet.__init__(self, worksheet, relation)
 
-    def fetch_data(self, mid, cur):
-        cur.execute(SELECT_MASTER % self.relation, { 'id': mid })
+    def fetch_data(self, mid, cur, selector):
+        stmnt = selector.getSelectStatementByView(self.relation)
+        if not stmnt: return
+        cur.execute(stmnt, { 'id': mid })
         row = cur.fetchone()
         if not row: return
         self.add_row(row)
 
 class Book:
 
-    def __init__(self, anonymize=False):
+    def __init__(self, selector):
+        self.selector = selector
+
         self.workbook  = Workbook()
 
-        sheets = [Master(self.workbook.add_sheet("Hauptdatensatz"), 
-            anonymize and 'master_tbl_anonym_view' or 'master_tbl_view')]
+        sheets = [Master(self.workbook.add_sheet("Hauptdatensatz"), 'master_tbl')]
 
-        for rg in anonymize and RG_VIEWS or RG_VIEWS_ANON:
+        for rg in RG_VIEWS:
             sheets.append(Sheet(self.workbook.add_sheet(rg[0]), rg[1]))
 
         self.sheets = sheets
@@ -151,7 +147,7 @@
     def add_cases(self, case_ids, cur):
         for case_id in case_ids:
             for sheet in self.sheets:
-                sheet.fetch_data(case_id, cur)
+                sheet.fetch_data(case_id, cur, self.selector)
                 sheet.next_row()
 
     def generate_headers(self):
@@ -161,7 +157,7 @@
     def write_xls(self, fname):
         self.workbook.save(fname)
 
-def exportAsXLS(cur, fname, ids=None, anonymize=False):
+def exportAsXLS(cur, fname, ids=None, case_parts=None, anonymize=False):
     if ids is None:
         ids = []
         cur.execute(FETCH_ALL_IDS)
@@ -169,7 +165,15 @@
             row = cur.fetchone()
             if not row: break
             ids.append(int(row[0]))
-    book = Book(anonymize)
+
+    if case_parts is None:
+        case_parts = CasePartsSelection()
+
+    selectFactory = SelectorFactory(case_parts, anonymize)
+
+    selector = selectFactory.createSelector()
+
+    book = Book(selector)
     book.add_cases(ids, cur)
     book.generate_headers()
     book.write_xls(fname)

Modified: waska/trunk/waskaweb/lib/exportselection.py
===================================================================
--- waska/trunk/waskaweb/lib/exportselection.py	2008-09-02 11:11:41 UTC (rev 114)
+++ waska/trunk/waskaweb/lib/exportselection.py	2008-09-02 12:29:05 UTC (rev 115)
@@ -134,6 +134,13 @@
     def getSelectStatement(self, view):
         return self.views.get(view)
 
+    def getSelectStatementByView(self, view):
+        if view == 'master_tbl':
+            relname = self.anonymize and "master_tbl_anonym_view" or "master_tbl_view"
+        else:
+            relname = (self.anonymize and "rg_%s_tbl_anonym_view"  or "rg_%s_tbl_view") % view
+        return self.views.get(relname)
+
     def addRepeatGroup(self, name):
         relname = (self.anonymize and "rg_%s_tbl_anonym_view"  or "rg_%s_tbl_view") % name
         self.views['rg_%s_tbl_view' % name] = \

Modified: waska/trunk/waskaweb/model/case.py
===================================================================
--- waska/trunk/waskaweb/model/case.py	2008-09-02 11:11:41 UTC (rev 114)
+++ waska/trunk/waskaweb/model/case.py	2008-09-02 12:29:05 UTC (rev 115)
@@ -84,9 +84,9 @@
     WHERE id = %(id)s"""
 
 SET_CASEPHASE_SQL = """
-    UPDATE ka_status_tbl_view 
+    UPDATE master_tbl_view 
     SET phase = %(phase)s 
-    WHERE id = %(id)s"""
+    WHERE id IN (SELECT master_id FROM ka_status_tbl_view WHERE id = %(id)s)"""
 
 LOAD_CASE_DIGEST_SQL = """
 SELECT
@@ -300,7 +300,7 @@
         finally:
             db.recycleConnection(con, cur)
 
-    def exportAsXLS(self, anonymize=False):
+    def exportAsXLS(self, case_parts = None, anonymize=False):
         con, cur = None, None
         fname = None
         try:
@@ -309,7 +309,7 @@
             f = None
             try:
                 fname = tmpnam() # security risk: symlink attacks
-                exportAsXLS(cur, fname, self.listDatasetIds(), anonymize)
+                exportAsXLS(cur, fname, self.listDatasetIds(), case_parts, anonymize)
                 f = open(fname, "rb")
                 return f.read()
             finally:

Modified: waska/trunk/waskaweb/templates/casemanagement/downloadXLS.mako
===================================================================
--- waska/trunk/waskaweb/templates/casemanagement/downloadXLS.mako	2008-09-02 11:11:41 UTC (rev 114)
+++ waska/trunk/waskaweb/templates/casemanagement/downloadXLS.mako	2008-09-02 12:29:05 UTC (rev 115)
@@ -22,22 +22,44 @@
 <div class="import_box_dialog">
 
     <form method="GET" action="${c.download_link}" accept-charset="UTF-8">
-        <p class="export_p"><label for="anonymize"><strong class="import_number">1.</strong> <input id="anonymize" name="anonymize" type="checkbox" value="1" checked>
+
+        <p class="export_p"><strong class="import_number">1. </strong> Aktenbereiche ausw&auml;hlen:<br> 
+        <span class="note">
+        % if c.is_all_selected:
+            Zur Zeit sind alle Bereiche ausgewählt.
+        % else:
+            Zur Zeit sind nur Teilbereiche ausgewählt.
+        % endif
+        </span>
+        <a class="button export" href="${c.selection_link}">Auswählen</a></p>
+
+        <p class="export_p"><label for="anonymize"><strong class="import_number">2.</strong> <input id="anonymize" name="anonymize" type="checkbox" value="1" checked>
         Daten anonymisiert exportieren.</label></p>
-        <p class="export_p"><strong class="import_number">2. </strong> XLS-Datei herunterladen:
+        <p class="export_p"><strong class="import_number">3. </strong> XLS-Datei herunterladen:
         <input type="submit" id="button" value="Herunterladen"></p>
     </form>
 
-    <p class="export_p"><strong class="import_number">3.</strong> Zur Übersicht zurückkehren:
+    <p class="export_p"><strong class="import_number">4.</strong> Zur Übersicht zurückkehren:
     <a class="button export" href="${c.overview_link}">Übersicht</a></p>
 </div>
 %else: 
 <div class="import_box_dialog">
 
-    <p class="export_p"><strong class="import_number">1. </strong> XLS-Datei herunterladen:
+    <p class="export_p"><strong class="import_number">1. </strong> Aktenbereiche ausw&auml;hlen:<br> 
+    <span class="note">
+    % if c.is_all_selected:
+        Zur Zeit sind alle Bereiche ausgewählt.
+    % else:
+        Zur Zeit sind nur Teilbereiche ausgewählt.
+    % endif
+    </span>
+    <a class="button export" href="${c.selection_link}">Auswählen</a></p>
+    
+
+    <p class="export_p"><strong class="import_number">2. </strong> XLS-Datei herunterladen:
     <a class="button export" href="${c.download_link}">Herunterladen</a></p>
 
-    <p class="export_p"><strong class="import_number">2.</strong> Zur Übersicht zurückkehren:
+    <p class="export_p"><strong class="import_number">3.</strong> Zur Übersicht zurückkehren:
     <a class="button export" href="${c.overview_link}">Übersicht</a></p>
 </div>
 %endif

Modified: waska/trunk/waskaweb/templates/casemanagement/selectParts.mako
===================================================================
--- waska/trunk/waskaweb/templates/casemanagement/selectParts.mako	2008-09-02 11:11:41 UTC (rev 114)
+++ waska/trunk/waskaweb/templates/casemanagement/selectParts.mako	2008-09-02 12:29:05 UTC (rev 115)
@@ -11,7 +11,8 @@
     <div class="actions">
       <ul>
         <li>
-          <a href="${h.url_for(controller='/case_overview', action='exportXML', confirmed=1)}">Zurück zu: XML Export</a>
+          ##<a href="${h.url_for(controller='/case_overview', action='exportXML', confirmed=1)}">Zurück zu: XML Export</a>
+          <a href="${c.back_url}">Zurück zu: ${c.back_title}</a>
         </li>
       </ul>
     </div>



More information about the Mpuls-commits mailing list