Make it possible to specify the key order for csv export
authorSverre Rabbelier <srabbelier@gmail.com>
Thu, 12 Mar 2009 21:36:39 +0000
changeset 1809 66aec0241d61
parent 1808 f6ec0f486247
child 1810 ec0bae3632bb
Make it possible to specify the key order for csv export Patch by: Sverre Rabbelier
app/soc/models/base.py
app/soc/views/helper/params.py
app/soc/views/models/base.py
--- a/app/soc/models/base.py	Thu Mar 12 14:16:55 2009 +0000
+++ b/app/soc/models/base.py	Thu Mar 12 21:36:39 2009 +0000
@@ -47,18 +47,27 @@
   """
 
   _fields_cache = None
+  DICT_TYPES = (db.StringProperty, db.IntegerProperty)
 
-  def toDict(self):
-    """Returns a dict with all StringProperty values of this entity.
+  def toDict(self, field_names=None):
+    """Returns a dict with all specified values of this entity.
+
+    Args:
+      field_names: the fields that should be included, defaults to
+        all fields that are of a type that is in DICT_TYPES.
     """
 
     result = {}
     props = self.properties()
 
+    if not field_names:
+      field_names = [i for i in props.keys() if isinstance(i, self.DICT_TYPES)]
+
     for key, value in props.iteritems():
-      # Skip everything but StringProperties and IntegerProperties
-      if not isinstance(value, (db.StringProperty, db.IntegerProperty)):
+      # Skip everything that is not valid
+      if key not in field_names:
         continue
+
       result[key] = getattr(self, key)
 
     if hasattr(self, 'name'):
--- a/app/soc/views/helper/params.py	Thu Mar 12 14:16:55 2009 +0000
+++ b/app/soc/views/helper/params.py	Thu Mar 12 21:36:39 2009 +0000
@@ -256,6 +256,7 @@
       'list_action': 'action',
       'list_description': 'description',
       'list_info': 'info',
+      'list_key_order': 'key_order',
       'list_main': 'main',
       'list_pagination': 'pagination',
       'list_row': 'row',
--- a/app/soc/views/models/base.py	Thu Mar 12 14:16:55 2009 +0000
+++ b/app/soc/views/models/base.py	Thu Mar 12 21:36:39 2009 +0000
@@ -546,7 +546,7 @@
       content = contents[export]
       key_order = content.get('key_order')
 
-      data = [i.toDict() for i in content['data']]
+      data = [i.toDict(key_order) for i in content['data']]
 
       if not key_order:
         data = [i.values() for i in data]