[Mpuls-commits] r325 - in wasko/trunk: . waskaweb/model

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Wed Mar 4 13:08:47 CET 2009


Author: teichmann
Date: 2009-03-04 13:08:43 +0100 (Wed, 04 Mar 2009)
New Revision: 325

Modified:
   wasko/trunk/ChangeLog.txt
   wasko/trunk/waskaweb/model/casexml.py
Log:
XML import: store log book entries to database


Modified: wasko/trunk/ChangeLog.txt
===================================================================
--- wasko/trunk/ChangeLog.txt	2009-03-04 09:35:54 UTC (rev 324)
+++ wasko/trunk/ChangeLog.txt	2009-03-04 12:08:43 UTC (rev 325)
@@ -1,5 +1,9 @@
 2009-03-04	Sascha L. Teichmann	<teichmann at intevation.de>
 
+	* waskaweb/model/casexml.py: Store logbook entries to data base.
+
+2009-03-04	Sascha L. Teichmann	<teichmann at intevation.de>
+
 	* waskaweb/model/casexml.py: Fixed unicode problem and removed 
 	  debug output.
 

Modified: wasko/trunk/waskaweb/model/casexml.py
===================================================================
--- wasko/trunk/waskaweb/model/casexml.py	2009-03-04 09:35:54 UTC (rev 324)
+++ wasko/trunk/waskaweb/model/casexml.py	2009-03-04 12:08:43 UTC (rev 325)
@@ -46,6 +46,8 @@
 import sys
 import re
 
+from datetime import timedelta
+
 UUID_RE = re.compile(r"^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$")
 
 SQL_GET_MASTER_ID = \
@@ -60,6 +62,15 @@
 SQL_UPDATE_MASTER = \
 """UPDATE master_tbl_view SET %s WHERE id = %d"""
 
+SQL_SELECT_IDS_FROM_LOGBOOK = \
+"""SELECT id, master_id FROM ka_logbuch_tbl_view WHERE uuid_id = %(uuid_id)s"""
+
+SQL_CREATE_LOGBOOK_ENTRY = \
+"""SELECT create_logbuch_ds(%(mid)s, %(uuid_id)s)"""
+
+SQL_UPDATE_LOGBOOK = \
+"""UPDATE ka_logbuch_tbl_view SET %s WHERE id = %d"""
+
 VERSION = 2
 
 DECODERS = {
@@ -71,9 +82,25 @@
     data.DateLeaf:     decode_date
 }
 
+def convert_from_db(s):
+    if type(s) == str: return unicode(s, 'utf-8')
+    return s
+
 def is_uuid(s):
     return bool(UUID_RE.match(s))
 
+def get_logbuch_ids_from_uuid(cur, uuid_id):
+    cur.execute(SQL_SELECT_IDS_FROM_LOGBOOK, { 'uuid_id': uuid_id  })
+    return cur.fetchone()
+
+def create_logbook_entry(cur, mid, uuid_id):
+    cur.execute(SQL_CREATE_LOGBOOK_ENTRY, {
+        'mid'    : mid,
+        'uuid_id': uuid_id })
+    row = cur.fetchone()
+    if row: return row[0]
+    return None
+
 class LogbookEntry(object):
 
     def __init__(self, uuid = None):
@@ -86,8 +113,7 @@
         self.dauer      = None
 
     def getUUID(self):
-        if not self.uuid:
-            self.uuid = str(uuid4())
+        #if not self.uuid: self.uuid = str(uuid4())
         return self.uuid
 
     def setUUID(self, uuid):
@@ -167,6 +193,32 @@
 
         out.append(u'</tagebuch-eintrag>')
 
+    def to_database(self, cur, ds_id):
+
+        lid = None
+        if not self.uuid is None: # has uuid
+            old = get_logbuch_ids_from_uuid(cur, self.uuid)
+            if old:
+                if old[1] != ds_id:
+                    raise StandardError('uuid leads to wrong master id')
+                lid = old[0]
+
+        if lid is None:
+            lid = create_logbook_entry(cur, ds_id, self.uuid)
+
+        changes, fields = {}, []
+
+        for attr in (
+            'datum', 'bearbeiter', 'art', 'kurz_notiz', 'notiz', 'dauer'):
+            v = getattr(self, attr)
+            if not v is None:
+                changes[attr] = v
+                fields.append('%s = %%(%s)s' % (attr, attr))
+
+        if changes:
+            update = SQL_UPDATE_LOGBOOK % (','.join(fields), lid)
+            cur.execute(update, changes)
+
 class Logbook(object):
 
     def __init__(self):
@@ -180,6 +232,10 @@
         out.append(u"</tagebuch>")
         return u"".join(out)
 
+    def to_database(self, cur, ds_id):
+        for entry in self.entries:
+            entry.to_database(cur, ds_id)
+
     def add_entry(self, entry):
         self.entries.append(entry)
 
@@ -214,7 +270,11 @@
             if not value is None: self.entry.setType(value)
 
     def dauer_end(self, name, attrs, value):
-        if self.entry: self.entry.dauer = decode_int(value)
+        if self.entry: 
+            value = decode_int(value)
+            if not value is None:
+                value = timedelta(minutes=value)
+            self.entry.dauer = value
 
     def bearbeiter_end(self, name, attrs, value):
         if self.entry: self.entry.bearbeiter = value
@@ -304,10 +364,6 @@
     update = SQL_UPDATE_MASTER % (",".join(fields), ds_id)
     cur.execute(update, changes)
 
-def convert_from_db(s):
-    if type(s) == str: return unicode(s, 'utf-8')
-    return s
-
 def import_xml(f, formed):
 
     master_handler = MasterHandler(formed)
@@ -328,7 +384,6 @@
     parser = make_parser()
     parser.setContentHandler(loader)
     parser.parse(f)
-    logbook = logbook_loader.logbook
 
     con, cur = None, None
     try:
@@ -406,6 +461,9 @@
 
             store_changes(cur, changed, ds_id)
 
+            # store the logbook too
+            logbook_loader.logbook.to_database(cur, ds_id)
+
             con.commit()
         except:
             try: con.rollback()



More information about the Mpuls-commits mailing list