[Mpuls-commits] r5704 - base/trunk/mpulsweb/lib

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Tue Dec 13 13:30:24 CET 2011


Author: bh
Date: 2011-12-13 13:30:21 +0100 (Tue, 13 Dec 2011)
New Revision: 5704

Modified:
   base/trunk/mpulsweb/lib/export.py
Log:
Simplify the xls/csv export code substantially.

In particular, this is about the changes to the names of the columns
with the IDs, which was introduced with revision 5697, so that columns
in different tables that contain the same kind of ID have the same name
(the column with the id of the master is always called "master_id", for
instance).

The new version of that code removes the additional recursive fill_*
methods again. Only the original fill_from_node is needed, although with
one additinal parameter and a little more complex implementation. And it
simplifies how the structure of the tables is determined in the
create_tables method. Overall, the new code is only a little more
complex than it was before r5697.

Improves the solution to mpuls/issue2620 substantially.


Modified: base/trunk/mpulsweb/lib/export.py
===================================================================
--- base/trunk/mpulsweb/lib/export.py	2011-12-13 10:12:11 UTC (rev 5703)
+++ base/trunk/mpulsweb/lib/export.py	2011-12-13 12:30:21 UTC (rev 5704)
@@ -99,7 +99,7 @@
 
     def create_tables(self, selection):
         table_columns = dict()
-        repeat_groups = set()
+        repeat_groups = dict()
         titles = dict()
         table_order = []
         if not self.one_table_per_page:
@@ -120,41 +120,25 @@
                 else:
                     table_name = "master"
                     table_title = "Master"
-                prev_table_name = 'master'
-                repeat = False
+                parent_name = "master"
                 for p in nc.pathToRoot():
                     if isinstance(p, RepeatNode):
-                        if not repeat:
-                            repeat = True
-                            prev_table_name = 'master'
-                        else:
-                            prev_table_name = table_name
                         table_name = p.getName()
                         table_title = p.getDescription()
-                        repeat_groups.add(table_name)
+                        repeat_groups[table_name] = parent_name
+                        parent_name = table_name
 
                 if table_name not in table_columns:
                     table_order.append(table_name)
-                if not repeat:
-                    table_columns.setdefault(table_name,
-                                             []).append(nc.getName())
-
-                elif not isinstance(nc, RepeatNode):
-                    table_columns.setdefault(table_name,[])
-                    if not table_name +"_id" in table_columns[table_name]:
-                        table_columns[table_name].append(table_name +"_id")
-                    if not prev_table_name +"_id" in table_columns[table_name]:
-                        table_columns[table_name].append(prev_table_name +"_id")
-                    table_columns[table_name].append(nc.getName())
-                else:
-                    table_columns.setdefault(table_name,
-                                             []).append(nc.getName()+"_id")
-                    table_columns[table_name].append(prev_table_name+ "_id")
+                table_columns.setdefault(table_name, []).append(nc.getName())
                 titles[table_name] = table_title
 
         for name, columns in table_columns.iteritems():
             extra_columns = []
-            if not name in repeat_groups:
+            if name in repeat_groups:
+                extra_columns.append(name + "_id")
+                extra_columns.append(repeat_groups[name] + "_id")
+            else:
                 extra_columns.append("master_id")
             self.tables[name] = self.table_factory(titles[name],
                                                    extra_columns + columns)
@@ -169,62 +153,30 @@
         for case_id, case in enumerate(tree.getiterator('master')):
             self.fill_from_node(case, case_id)
 
-    def fill_from_node(self, node, node_id, parent_id=None):
-        rows = defaultdict(lambda: dict(master_id=node_id))
+    def fill_from_node(self, node, node_id, parent_name=None, parent_id=None):
+        assert parent_name is None or parent_id is not None
 
-        if parent_id is not None:
-            rows[node.tag]["master_id"] = parent_id
+        rows = defaultdict(dict)
 
-        for child in node.getchildren():
-            name = child.tag
-            nc = self.meta_tree.findByName(name)
-            if isinstance(nc, RepeatNode):
-                self.fill_rg_from_node(child, self.repeat_group_ids.next(),
-                                    node_id)
-            else:
-                table_name = self.field_name_to_table_name.get(name)
-                if table_name is not None:
-                    rows[table_name][name] = child.text
+        if parent_name is not None:
+            rows[node.tag][parent_name + "_id"] = parent_id
 
-        for table_name, row in rows.iteritems():
-            self.tables[table_name].append(row)
-
-    def fill_rg_from_node(self, node, node_id, case_id):
-        rows = defaultdict(lambda: dict(master_id=case_id))
-        name = node.tag
-        table_name = self.field_name_to_table_name.get(name,name)
-        rows[table_name][name + "_id"] = node_id
-        
-        self.fill_rg_children(node, node_id, rows)
-
-    def fill_inner_rg_from_node(self, node, node_id, prev_id, prev_nc):
-        rows = defaultdict(lambda: dict())
-        name = node.tag
-        table_name = self.field_name_to_table_name.get(name,name)
-        rows[table_name][name+"_id"] = node_id 
-        rows[table_name][prev_nc.getName()+ "_id"] = prev_id
-        
-        self.fill_rg_children(node, node_id, rows)
-        
-    def fill_rg_children(self, node, node_id, rows):     
-        node_name = node.tag
-        node_nc = self.meta_tree.findByName(node_name)
         for child in node.getchildren():
             name = child.tag
             nc = self.meta_tree.findByName(name)
             if isinstance(nc, RepeatNode):
-                self.fill_inner_rg_from_node(child,
-                                             self.repeat_group_ids.next(),
-                                             node_id, node_nc)
+                self.fill_from_node(child, self.repeat_group_ids.next(),
+                                    node.tag, node_id)
             else:
                 table_name = self.field_name_to_table_name.get(name)
                 if table_name is not None:
                     rows[table_name][name] = child.text
 
         for table_name, row in rows.iteritems():
+            rows[table_name][node.tag + "_id"] = node_id
             self.tables[table_name].append(row)
 
-        
+
 #
 # XLS Export
 #



More information about the Mpuls-commits mailing list