Added generic csv export to all list views
authorSverre Rabbelier <srabbelier@gmail.com>
Thu, 12 Mar 2009 13:39:03 +0000
changeset 1806 d321d5123928
parent 1805 7204ec5ead23
child 1807 1f8cde169f32
Added generic csv export to all list views Patch by: Sverre Rabbelier
app/soc/views/helper/params.py
app/soc/views/models/base.py
--- a/app/soc/views/helper/params.py	Thu Mar 12 13:38:24 2009 +0000
+++ b/app/soc/views/helper/params.py	Thu Mar 12 13:39:03 2009 +0000
@@ -233,6 +233,7 @@
 
   new_params['export_content_type'] = None
   new_params['export_extension'] = '.txt'
+  new_params['csv_fieldnames'] = []
 
   # TODO: Use only the js modules needed instead of js_uses_all
   new_params['js_uses_all'] = DEF_JS_USES_LIST
--- a/app/soc/views/models/base.py	Thu Mar 12 13:38:24 2009 +0000
+++ b/app/soc/views/models/base.py	Thu Mar 12 13:39:03 2009 +0000
@@ -23,6 +23,10 @@
   '"Pawel Solyga" <pawel.solyga@gmail.com>',
   ]
 
+
+import csv
+import StringIO
+
 from google.appengine.ext import db
 
 from django import http
@@ -512,6 +516,9 @@
   def _list(self, request, params, contents, page_name, context=None):
     """Returns the list page for the specified contents.
 
+    If the export parameter is present in request.GET a csv export of
+    the specified list is returned instead, see csv().
+
     Args:
       request: the standard Django HTTP request object
       params: a dict with params for this View
@@ -529,6 +536,24 @@
         to display the list of all entities for this View.
     """
 
+    try:
+      export = int(request.GET.get('export', -1))
+      export = export if export >= 0 else None
+    except ValueError:
+      export = None
+
+    if export is not None and export < len(contents):
+      content = contents[export]
+      key_order = content.get('key_order')
+
+      data = [i.toDict() for i in content['data']]
+
+      if not key_order:
+        data = [i.values() for i in data]
+
+      filename = "export_%d" % export
+      return self.csv(request, data, filename, params, key_order)
+
     context = dicts.merge(context,
         helper.responses.getUniversalContext(request))
     helper.responses.useJavaScript(context, params['js_uses_all'])
@@ -705,6 +730,31 @@
 
     return response
 
+  def csv(self, request, data, filename, params, key_order=None):
+    """Returns data as a csv file
+
+    If key_order is set data should be a sequence of dicts, otherwise
+    data should be a sequence of lists,s ee csv.writer and
+    csv.DictWriter for more information.
+    """
+
+    params = params.copy()
+    params['export_extension'] = '.csv'
+    fieldnames = params['csv_fieldnames']
+
+    f = StringIO.StringIO()
+
+    if key_order:
+      writer = csv.DictWriter(f, key_order, dialect='excel')
+    else:
+      writer = csv.writer(f, dialect='excel')
+
+    writer.writerows(data)
+
+    data = f.getvalue()
+
+    return self.download(request, data, filename, params)
+
   def _editPost(self, request, entity, fields):
     """Performs any required processing on the entity to post its edit page.