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

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Fri Nov 4 20:40:09 CET 2011


Author: bh
Date: 2011-11-04 20:40:08 +0100 (Fri, 04 Nov 2011)
New Revision: 5549

Modified:
   base/trunk/mpulsweb/model/document.py
Log:
Extract creating documents from Document.create into the new function
create_document_with_cursor. The new function is more low-level and
requires that a database cursor has already been created. The create
method now creates a database cursor and calls
create_document_with_cursor to do the actual work.

The create_document_with_cursor function will be necessary for importing
documents attached to cases from XML files.


Modified: base/trunk/mpulsweb/model/document.py
===================================================================
--- base/trunk/mpulsweb/model/document.py	2011-11-04 19:31:30 UTC (rev 5548)
+++ base/trunk/mpulsweb/model/document.py	2011-11-04 19:40:08 UTC (rev 5549)
@@ -121,6 +121,32 @@
     return documents
 
 
+def create_document_with_cursor(cursor, name, content, mime, case_id, uuid):
+    if case_id is None:
+        cursor.execute(CREATE_GLOBAL, {'name': name})
+    else:
+        cursor.execute(CREATE_CASE,
+                       {'id': case_id, 'uuid': uuid, 'name': name})
+
+    row = cursor.fetchone()
+    if row is None:
+        raise DocumentException(_(u"Could not create new document"))
+
+    document_id = row[0]
+    fields = {'bytes': dbapi.Binary(content), 'size': len(content),
+              'name': name, 'mime': mime, 'id': document_id}
+
+    if case_id is None:
+        stmnt = UPDATE_GLOBAL
+    else:
+        stmnt = UPDATE_CASE
+        fields['case'] = case_id
+
+    cursor.execute(stmnt, fields)
+
+    return document_id
+
+
 def listDocuments(case=None):
     if not case is None:
         case = int(case)
@@ -185,30 +211,9 @@
         self.size = len(bytes) #total_size
 
         with db.transaction() as cur:
-            if case is None:
-                cur.execute(CREATE_GLOBAL, {'name': name})
-            else:
-                cur.execute(CREATE_CASE,
-                            {'id': case, 'uuid': uuid, 'name': name})
+            self.id = create_document_with_cursor(cur, name, bytes, mime, case,
+                                                  uuid)
 
-            row = cur.fetchone()
-            if row is None:
-                raise DocumentException(_(u"Could not create new document"))
-
-            id = row[0]
-            fields = {'bytes': dbapi.Binary(bytes), 'size': self.size,
-                      'name': name, 'mime': mime, 'id': id}
-
-            if case is None:
-                stmnt = UPDATE_GLOBAL
-            else:
-                stmnt = UPDATE_CASE
-                fields['case'] = case
-
-            cur.execute(stmnt, fields)
-
-            self.id = id
-
     def load(self, asCase=False):
         if self.id is None:
             raise DocumentException(_(u"An ID is needed"))



More information about the Mpuls-commits mailing list