[Mpuls-commits] r3283 - in base/trunk: . mpulsweb/controllers mpulsweb/model mpulsweb/templates/casemanagement mpulsweb/templates/casemanagement/dialogs

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Fri Jul 30 20:38:16 CEST 2010


Author: bh
Date: 2010-07-30 20:38:14 +0200 (Fri, 30 Jul 2010)
New Revision: 3283

Added:
   base/trunk/mpulsweb/templates/casemanagement/dialogs/failed_xml_import.mako
   base/trunk/mpulsweb/templates/casemanagement/dialogs/success_xml_import.mako
   base/trunk/mpulsweb/templates/casemanagement/upload.mako
Modified:
   base/trunk/ChangeLog
   base/trunk/mpulsweb/controllers/case.py
   base/trunk/mpulsweb/model/case.py
Log:
Add XML import feature.  Requires libformed revision 207:d092a27ff9f1

* mpulsweb/controllers/case.py (CaseController.importCase)
(CaseController.importXMLAction): New methods to handle XML
import.

* mpulsweb/model/case.py (MPulsXMLImporter): New class to handle
XML import.

* mpulsweb/templates/casemanagement/upload.mako,
mpulsweb/templates/casemanagement/dialogs/success_xml_import.mako,
mpulsweb/templates/casemanagement/dialogs/failed_xml_import.mako:
New.  Templates for the XML import feature.


Modified: base/trunk/ChangeLog
===================================================================
--- base/trunk/ChangeLog	2010-07-30 12:36:59 UTC (rev 3282)
+++ base/trunk/ChangeLog	2010-07-30 18:38:14 UTC (rev 3283)
@@ -1,3 +1,19 @@
+2010-07-30  Bernhard Herzog  <bh at intevation.de>
+
+	Add XML import feature.  Requires libformed revision 207:d092a27ff9f1
+
+	* mpulsweb/templates/casemanagement/upload.mako,
+	mpulsweb/templates/casemanagement/dialogs/success_xml_import.mako,
+	mpulsweb/templates/casemanagement/dialogs/failed_xml_import.mako:
+	New.  Templates for the XML import feature.
+
+	* mpulsweb/model/case.py (MPulsXMLImporter): New class to handle
+	XML import.
+
+	* mpulsweb/controllers/case.py (CaseController.importCase)
+	(CaseController.importXMLAction): New methods to handle XML
+	import.
+
 2010-07-30  Torsten Irländer <torsten.irlaender at intevation.de>
 
 	* mpulsweb/controllers/case_overview.py (get_sort): Issue860: Return

Modified: base/trunk/mpulsweb/controllers/case.py
===================================================================
--- base/trunk/mpulsweb/controllers/case.py	2010-07-30 12:36:59 UTC (rev 3282)
+++ base/trunk/mpulsweb/controllers/case.py	2010-07-30 18:38:14 UTC (rev 3283)
@@ -12,7 +12,9 @@
 from mpulsweb.lib.dialogs import checkPrivacyStatement
 from mpulsweb.lib.validators import SetStandinForm, SetEditorForm, NewCaseForm
 from mpulsweb.lib.anonymize import determineAnonymizedCaseEndPhaseName
-from mpulsweb.model.case import ConsistenceCheckException, SessionCase
+from mpulsweb.lib.db import  PostgresDBInterface
+from mpulsweb.model.case import ConsistenceCheckException, SessionCase, \
+     MPulsXMLImporter, XMLImportException
 from mpulsweb.model.user import UserGroupList, UserListObject
 from mpulsweb.model.logbook import LogbookEntry
 from mpulsweb.controllers.formularpage import convert2dic
@@ -68,6 +70,15 @@
 COPY_CONFIRM = _('Copy case?')
 COPY_CONFIRM_TEXT = _('Do you really want to copy this case? All data will be copied in the new case.')
 
+IMPORT_XML_SUCCESS = _("Case(s) imported!""")
+IMPORT_XML_SUCCESS_TEXT = _("""The file was imported successfully.
+Click on "OK" to open the case overview.""")
+
+IMPORT_FAILED = _("Case(s) not imported!")
+IMPORT_FAILED_TEXT = _("""An error occurred during the import.
+Click 'OK' to return to the import dialog.""")
+
+
 log = logging.getLogger(__name__)
 
 class CaseController(BaseController):
@@ -362,6 +373,34 @@
             c.url_no = h.url_for(controller='/case', action='digest', id=id)
             return render('/casemanagement/dialogs/confirm_copy.mako')
 
+    @checkRole("cm")
+    def importCase(self):
+        return render('/casemanagement/upload.mako')
+
+    @checkRole('cm_ka')
+    def importXMLAction(self):
+        try:
+            xmlfile = request.POST['xmlfile']
+            try:
+                importer = MPulsXMLImporter(g.formedTree)
+                importer.import_xml(PostgresDBInterface(), xmlfile.file)
+            finally:
+                xmlfile.file.close()
+
+            c.dialog_title = IMPORT_XML_SUCCESS
+            c.dialog_text = IMPORT_XML_SUCCESS_TEXT
+            c.url_ok = h.url_for(controller="/case_overview")
+            return render('/casemanagement/dialogs/success_xml_import.mako')
+        except Exception, e:
+            log.exception("Exception during xml import")
+            c.dialog_title  = IMPORT_FAILED
+            if isinstance(e, XMLImportException):
+                c.dialog_text = h.escape(e.message)
+            else:
+                c.dialog_text = IMPORT_FAILED_TEXT
+            c.url_ok = h.url_for(controller='/case', action='importCase')
+            return render('/casemanagement/dialogs/failed_xml_import.mako')
+
     #
     # ORGANISATION (standin and editor settings)
     #

Modified: base/trunk/mpulsweb/model/case.py
===================================================================
--- base/trunk/mpulsweb/model/case.py	2010-07-30 12:36:59 UTC (rev 3282)
+++ base/trunk/mpulsweb/model/case.py	2010-07-30 18:38:14 UTC (rev 3283)
@@ -38,6 +38,7 @@
 
 from formed.meta.data import RepeatNode, DateLeaf, IntLeaf, TextLeaf, ChoiceNode
 from formed.instance.backends.postgres import DBFactory as InstanceFactory
+from formed.instance.backends.xmlimport import XMLImporter, XMLImportException
 from formed.instance.tree import SetDataException
 
 from mpulsweb.lib.base import g, h, session
@@ -1240,3 +1241,50 @@
                       # starts 14 days before maxsavetime is reached in
                       # template. We do not want such counter until the
                       # remindcondiotion is not true 
+
+
+
+class MPulsXMLImporter(XMLImporter):
+
+    """MPuls specific extension for the libform XML importer"""
+
+    UUID_ANONYMIZED_SQL = """SELECT is_uuid_anonymized(%(uuid)s)"""
+    STATE_FROM_UUID_SQL = """
+SELECT s.status
+FROM ka_status_tbl_view s JOIN master_tbl_view m ON m.id = s.master_id
+WHERE m.uuid_id = %(uuid)s
+"""
+
+    def import_allowed(self, cursor, uuid):
+        if self.uuid_anonymized(cursor, uuid):
+            raise XMLImportException(u"Die Akte mit UUID '%s' ist bereits"
+                                     u" anonymisiert." % uuid)
+        if not self.state_allows_import(cursor, uuid):
+            raise XMLImportException (u"Die Akte mit UUID '%s' ist schwebend"
+                                      u" anonymisiert oder schwebend gelöscht"
+                                      % uuid)
+        return True
+
+    def uuid_anonymized(self, cursor, uuid):
+        """Return whether the case for the uuid has been anonymized already.
+        Cases that have been anonymized may not be imported.
+        """
+        cursor.execute(self.UUID_ANONYMIZED_SQL, dict(uuid=uuid))
+        row = cursor.fetchone()
+        if not row:
+            return False
+        return row[0]
+
+    def state_allows_import(self, cursor, uuid):
+        """Return whether the current state of the case allows an update.
+        If a case exists and it's either marked for deletion or marked
+        for anonymization it may not be updated.  Otherwise an update is
+        OK.
+        """
+        cursor.execute(self.STATE_FROM_UUID_SQL, dict(uuid=uuid))
+        row = cursor.fetchone()
+        if not row:
+            # case doesn't exist, so import is possible as far as the state
+            # is concerned
+            return True
+        return row[0] not in (4, 3)

Added: base/trunk/mpulsweb/templates/casemanagement/dialogs/failed_xml_import.mako
===================================================================
--- base/trunk/mpulsweb/templates/casemanagement/dialogs/failed_xml_import.mako	2010-07-30 12:36:59 UTC (rev 3282)
+++ base/trunk/mpulsweb/templates/casemanagement/dialogs/failed_xml_import.mako	2010-07-30 18:38:14 UTC (rev 3283)
@@ -0,0 +1,7 @@
+## -*- coding: utf-8 -*- 
+<%inherit file="/main.mako" />
+<%def name="buildNavipath()">
+  ${parent.buildNavipath()}
+  <li><a href="#">${_('Import cases')}</a></li>
+</%def>
+<%include file="../../dialogs/failed.mako" />

Added: base/trunk/mpulsweb/templates/casemanagement/dialogs/success_xml_import.mako
===================================================================
--- base/trunk/mpulsweb/templates/casemanagement/dialogs/success_xml_import.mako	2010-07-30 12:36:59 UTC (rev 3282)
+++ base/trunk/mpulsweb/templates/casemanagement/dialogs/success_xml_import.mako	2010-07-30 18:38:14 UTC (rev 3283)
@@ -0,0 +1,7 @@
+## -*- coding: utf-8 -*- 
+<%inherit file="/main.mako" />
+<%def name="buildNavipath()">
+  ${parent.buildNavipath()}
+  <li><a href="#">${_('Import cases')}</a></li>
+</%def>
+<%include file="../../dialogs/success.mako" />

Added: base/trunk/mpulsweb/templates/casemanagement/upload.mako
===================================================================
--- base/trunk/mpulsweb/templates/casemanagement/upload.mako	2010-07-30 12:36:59 UTC (rev 3282)
+++ base/trunk/mpulsweb/templates/casemanagement/upload.mako	2010-07-30 18:38:14 UTC (rev 3283)
@@ -0,0 +1,23 @@
+## -*- coding: utf-8 -*-
+<%inherit file="../main.mako" />
+<%def name="buildNavipath()">
+		${parent.buildNavipath()}
+	<li><a href="${h.url_for(controller='/case')}">${_('cm_np_root')}</a></li>
+	<li><a href="${h.url_for(controller='/case', action='import')}">${_('Import cases')}</a></li>
+</%def>
+<h1>${_('XML Import')}</h1>
+<div id="waska_form" class="import_box">
+
+<h2>XML-Import</h2>
+<p class="import_p">
+  Es können Dateien im XML-Format (.xml) angegeben werden.<br>
+  Die Größe einer Datei darf 10MB nicht überschreiten.
+</p>
+<div class="import_box_dialog">
+  ${h.form(h.url(controller='/case', action='importXMLAction'), multipart=True)}
+  <label class="import_label" for="xmlfile"><strog class="import_number">1.</strong> Datei wählen:</label> <input type="file" name="xmlfile" id="xmlfile"><br>
+  <label class="import_label"><strong class="import_number">2.</strong> Datei importieren:</label>
+  ${h.submit('upload', _('cm_from_import_submit'))}
+  ${h.end_form()}
+</div>
+</div>



More information about the Mpuls-commits mailing list