[Mpuls-commits] r5552 - base/trunk/mpulsweb/model

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Fri Nov 4 21:15:01 CET 2011


Author: bh
Date: 2011-11-04 21:15:01 +0100 (Fri, 04 Nov 2011)
New Revision: 5552

Modified:
   base/trunk/mpulsweb/model/case.py
   base/trunk/mpulsweb/model/document.py
Log:
Implement import of documents attached to cases from XML files.
This includes the actual document import code in the document model and
the corresponding hookup in MPulsXMLImporter in the case model.

Implements the mpuls server part of mpuls/issue2534


Modified: base/trunk/mpulsweb/model/case.py
===================================================================
--- base/trunk/mpulsweb/model/case.py	2011-11-04 19:43:25 UTC (rev 5551)
+++ base/trunk/mpulsweb/model/case.py	2011-11-04 20:15:01 UTC (rev 5552)
@@ -53,7 +53,8 @@
 from mpulsweb.lib.helpers import ensure_unicode, format_date, get_TagSetter
 from mpulsweb.lib.db import PostgresDBInterface, db
 
-from mpulsweb.model.document import listDocuments, export_documents
+from mpulsweb.model.document import listDocuments, export_documents, \
+     get_documents_import_factory, import_documents
 from mpulsweb.model.statement import PrivacyStatement
 from mpulsweb.model.phase import PhaseFactory, StartPhasePart, \
     PhaseAnonymizableCheckException, ConsistenceCheckException, PHASEFIELD, PC_MANUAL
@@ -1476,6 +1477,7 @@
         if extra_factories is None:
             extra_factories = dict()
         extra_factories["tagebuch"] = get_logbook_import_factory()
+        extra_factories["anlagen"] = get_documents_import_factory()
         DBXMLImporter.__init__(self, meta_tree, extra_factories)
         self.imported_ids = []
 
@@ -1520,6 +1522,8 @@
                 # only try to import logbook if there actually was a
                 # logbook for the case
                 import_logbook(tree, cursor, components["tagebuch"])
+            if "anlagen" in components:
+                import_documents(tree, cursor, components["anlagen"])
             self.imported_ids.append(tree.getRootNode().getIdentifier())
             return tree
         return None

Modified: base/trunk/mpulsweb/model/document.py
===================================================================
--- base/trunk/mpulsweb/model/document.py	2011-11-04 19:43:25 UTC (rev 5551)
+++ base/trunk/mpulsweb/model/document.py	2011-11-04 20:15:01 UTC (rev 5552)
@@ -37,10 +37,16 @@
 
 import psycopg2  as dbapi
 
+from formed.meta.data import RepeatNode, TextLeaf
+from formed.meta.structure import StructureNode
+from formed.instance.backends.xmlimport import ComponentFactory
+from formed.instance.simplified import StructureInstanceNode, \
+     StructureInstanceTree
+
 import mpulsweb.lib.helpers as h
 from mpulsweb.lib.db import db
 from mpulsweb.lib.translation import _
-from mpulsweb.model.logbook import text_subelement
+from mpulsweb.model.logbook import text_subelement, new_node
 
 
 MAX_SIZE = 10 * 1024 * 1024
@@ -253,4 +259,45 @@
                         base64.b64encode(document.data))
 
 
+def get_documents_import_factory():
+    """Return component factory for documents for libformed xmlimport"""
+    entry_node = StructureNode(new_node(RepeatNode, "anlage"),
+                               [new_node(TextLeaf, "anlage-mime"),
+                                new_node(TextLeaf, "anlage-name"),
+                                new_node(TextLeaf, "anlage-daten"),
+                                ],
+                               [])
+    return ComponentFactory(None,
+                            StructureNode(None, [], [entry_node]),
+                            StructureInstanceNode,
+                            StructureInstanceTree)
+
+
+def import_documents(case_tree, cursor, logbook_tree):
+    """Import the documents"""
+    documents = logbook_tree.root.get_repeat_groups("anlage")
+    if documents:
+        case_id = case_tree.getRootNode().getIdentifier()
+        known = load_known_documents(cursor, case_id)
+        for document in documents.itervalues():
+            name = document.get_leaf("anlage-name")
+            if name in known:
+                del known[name]
+            import_document(cursor, case_id, document)
+            for doc in known.itervalues():
+                delete_document_with_cursor(cursor, doc.id, True)
+
+
+def import_document(cursor, case_id, document):
+    name = document.get_leaf("anlage-name")
+    mime = document.get_leaf("anlage-mime")
+    content = base64.b64decode(document.get_leaf("anlage-daten"))
+    create_document_with_cursor(cursor, name, content, mime, case_id, None)
+
+
+def load_known_documents(cursor, case_id):
+    documents = list_documents_with_cursor(cursor, case_id)
+    return dict((doc.name, doc) for doc in documents)
+
+
 # vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:



More information about the Mpuls-commits mailing list