[Mpuls-commits] r4018 - in base/trunk: . mpulsweb/model
scm-commit@wald.intevation.org
scm-commit at wald.intevation.org
Mon Oct 25 14:52:42 CEST 2010
Author: bh
Date: 2010-10-25 14:52:41 +0200 (Mon, 25 Oct 2010)
New Revision: 4018
Modified:
base/trunk/ChangeLog
base/trunk/mpulsweb/model/case.py
base/trunk/mpulsweb/model/logbook.py
Log:
* mpulsweb/model/case.py (MpulsCaseBundle.export): Export the
logbook as well if it has been selected and the case is not being
anonymized during export.
* mpulsweb/model/logbook.py (text_subelement, iso_time_string)
(export_logbook): New functions to export the logbook.
Modified: base/trunk/ChangeLog
===================================================================
--- base/trunk/ChangeLog 2010-10-22 17:13:47 UTC (rev 4017)
+++ base/trunk/ChangeLog 2010-10-25 12:52:41 UTC (rev 4018)
@@ -1,3 +1,16 @@
+2010-10-25 Bernhard Herzog <bh at intevation.de>
+
+ Export the logbook, too, if requested. The user interface has not
+ yet been extended to actually let the user do that, because XML
+ files containing the logbook cannot yet be imported again.
+
+ * mpulsweb/model/logbook.py (text_subelement, iso_time_string)
+ (export_logbook): New functions to export the logbook.
+
+ * mpulsweb/model/case.py (MpulsCaseBundle.export): Export the
+ logbook as well if it has been selected and the case is not being
+ anonymized during export.
+
2010-10-22 Bernhard Herzog <bh at intevation.de>
Fetch UUIDs of logbook entries, in preparation for logbook XML
Modified: base/trunk/mpulsweb/model/case.py
===================================================================
--- base/trunk/mpulsweb/model/case.py 2010-10-22 17:13:47 UTC (rev 4017)
+++ base/trunk/mpulsweb/model/case.py 2010-10-25 12:52:41 UTC (rev 4018)
@@ -59,6 +59,7 @@
ConsistenceCheckException, PHASEFIELD
from mpulsweb.model.user import UserObject
from mpulsweb.model.agencysettings import Agency
+from mpulsweb.model.logbook import export_logbook
log = logging.getLogger(__name__)
@@ -430,8 +431,13 @@
def export(self, type='XML', anonym=True, selection=None):
factory = InstanceFactory(g.formedTree, PostgresDBInterface())
num = 0
+ external = None
+ if not anonym and "logbook" in selection:
+ # Only export logbook when actually selected for export and
+ # when the case is not exported anonymized.
+ external = export_logbook
xmltree = factory.toXML(self.case_list, selection=selection,
- anonymize=anonym)
+ anonymize=anonym, external=external)
num = len(self.case_list)
if type == "XML":
Modified: base/trunk/mpulsweb/model/logbook.py
===================================================================
--- base/trunk/mpulsweb/model/logbook.py 2010-10-22 17:13:47 UTC (rev 4017)
+++ base/trunk/mpulsweb/model/logbook.py 2010-10-25 12:52:41 UTC (rev 4018)
@@ -30,6 +30,7 @@
import re
import logging
from datetime import timedelta, datetime
+import xml.etree.ElementTree as ET
from pylons import app_globals as g
import mpulsweb.lib.helpers as h
@@ -402,4 +403,47 @@
def getCaseId(self):
return self.case_id
+
+def text_subelement(parent, tag, text):
+ """Create an element under parent with text content.
+ The text will always be converted to unicode, so it should either be
+ unicode or be an object where the default conversion to unicode is
+ appropriate for XML serialisation.
+ """
+ if text is not None:
+ element = ET.SubElement(parent, tag)
+ element.text = unicode(text)
+
+def iso_time_string(d):
+ """Convert a datetime object to an ISO string."""
+ # datetime.strftime() dislikes years before 1900, so we can't use it
+ # here because the "unknown" date is before 1900.
+ return ("%04d-%02d-%02dT%02d:%02d:%02d"
+ % (d.year, d.month, d.day, d.hour, d.minute, d.second))
+
+
+def export_logbook(case, case_id, cursor, selection, anonymize):
+ """Add the contents of the case with id case_id to the elementtree case"""
+ logbook = Logbook()
+ logbook.loadById(case_id)
+
+ logbook_element = ET.SubElement(case, "tagebuch")
+
+ for entry in logbook.getEntries():
+ entry_element = ET.SubElement(logbook_element, "tagebuch-eintrag")
+
+ text_subelement(entry_element, "uuid_id", entry.uuid)
+ text_subelement(entry_element, "tagebuch-datum",
+ iso_time_string(entry.datum))
+ text_subelement(entry_element, "tagebuch-bearbeiter",
+ entry.bearbeiter_name)
+ text_subelement(entry_element, "tagebuch-art", entry.art)
+ text_subelement(entry_element, "tagebuch-sonstiges", entry.kurz_notiz)
+ text_subelement(entry_element, "tagebuch-notiz", entry.notiz)
+ if entry.dauer is not None:
+ text_subelement(entry_element, "tagebuch-dauer",
+ int(h.timedelta_in_minutes(entry.dauer)))
+ text_subelement(entry_element, "tagebuch-typ", entry.typ)
+
+
# vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8:
More information about the Mpuls-commits
mailing list