# HG changeset patch # User Sverre Rabbelier # Date 1236893799 0 # Node ID 66aec0241d61e799892ad1e35ce7e254fcc9c390 # Parent f6ec0f48624737cff6fc47a188156facbb5a1043 Make it possible to specify the key order for csv export Patch by: Sverre Rabbelier diff -r f6ec0f486247 -r 66aec0241d61 app/soc/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'): diff -r f6ec0f486247 -r 66aec0241d61 app/soc/views/helper/params.py --- 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', diff -r f6ec0f486247 -r 66aec0241d61 app/soc/views/models/base.py --- 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]