[Mpuls-commits] r3172 - base/trunk/mpulsweb/lib/helper
scm-commit@wald.intevation.org
scm-commit at wald.intevation.org
Mon Jul 12 15:54:04 CEST 2010
Author: torsten
Date: 2010-07-12 15:53:58 +0200 (Mon, 12 Jul 2010)
New Revision: 3172
Added:
base/trunk/mpulsweb/lib/helper/case.py
Log:
Added helper function for working on the formedtree or case in general
Added: base/trunk/mpulsweb/lib/helper/case.py
===================================================================
--- base/trunk/mpulsweb/lib/helper/case.py 2010-07-12 08:50:40 UTC (rev 3171)
+++ base/trunk/mpulsweb/lib/helper/case.py 2010-07-12 13:53:58 UTC (rev 3172)
@@ -0,0 +1,43 @@
+# -*- coding: utf-8 -*-
+import logging
+from formed.instance.tree import InstanceLeaf
+from formed.instance.backends.postgres import DBFactory as InstanceFactory
+from formed.meta.data import DateLeaf
+
+from mpulsweb.lib.base import g
+from mpulsweb.lib.db import PostgresDBInterface, db
+from mpulsweb.lib.helpers import format_date
+
+log = logging.getLogger(__name__)
+
+def walk_it(node, type=None):
+ items = []
+ items.append(node)
+ if not isinstance(node, InstanceLeaf):
+ for c in node.getChildren():
+ items.extend(walk_it(c))
+ return items
+
+def copy_field(src_id=None, src_field=None, dst_id=None, dst_field=None):
+ data = {}
+ factory = InstanceFactory(g.formedTree, PostgresDBInterface())
+ sit = factory.loadInstanceTreeByIdentifier(src_id)
+ dit = factory.loadInstanceTreeByIdentifier(dst_id)
+ item = sit.getItem("%s:%s" % (src_field, src_id))
+ if isinstance(item, DateLeaf):
+ value = format_date(item.getValue())
+ else:
+ value = str(item.getValue())
+ data["%s:%s" % (dst_field, dst_id)] = value
+ log.debug(data)
+ dit.setData(data)
+ dit.commit()
+
+def copy_instance_tree(it, fields=None):
+ '''Does a full copy of a given instance tree. If fields is not None, the only fields will be copied whoch are define in fields. This is done by deleting the values after copying."'''
+ factory = InstanceFactory(g.formedTree, PostgresDBInterface())
+ new_it = factory.copy_instance_tree(it, fields)
+ return new_it
+
+
+# vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:
More information about the Mpuls-commits
mailing list