Made the CSV exporter for Survey not rely on the BackReferenceProperty in Survey.
authorLennard de Rijk <ljvderijk@gmail.com>
Thu, 02 Jul 2009 17:08:10 +0200
changeset 2492 6eac6cd88dad
parent 2491 f5776b6116fd
child 2493 0aabd2d76606
Made the CSV exporter for Survey not rely on the BackReferenceProperty in Survey.
app/soc/views/helper/surveys.py
app/soc/views/models/survey.py
--- a/app/soc/views/helper/surveys.py	Thu Jul 02 16:53:13 2009 +0200
+++ b/app/soc/views/helper/surveys.py	Thu Jul 02 17:08:10 2009 +0200
@@ -769,29 +769,43 @@
   return records
 
 
-def to_csv(survey):
+def to_csv(survey_view):
   """CSV exporter.
+
+  Args:
+      survey_view: instance of the SurveyView
   """
 
-  # get header and properties
-  header = _get_csv_header(survey)
-  leading = ['user', 'created', 'modified']
-  properties = leading + survey.survey_content.orderedProperties()
+  def wrapper(survey):
+    """Wrapper function.
+    """
+    survey_logic = survey_view.getParams()['logic']
+    record_logic = survey_logic.getRecordLogic()
 
-  try:
-    first = survey.getRecords().run().next()
-  except StopIteration:
-    # bail out early if survey_records.run() is empty
-    return header, survey.link_id
+    # get header and properties
+    header = _get_csv_header(survey)
+    leading = ['user', 'created', 'modified']
+    properties = leading + survey.survey_content.orderedProperties()
+
+    # retrieve the query of the data to export
+    fields = {'survey': survey}
+    record_query = record_logic.getQueryForFields(fields)
 
-  # generate results list
-  recs = survey.getRecords().run()
-  recs = _get_records(recs, properties)
-
-  # write results to CSV
-  output = StringIO.StringIO()
-  writer = csv.writer(output)
-  writer.writerow(properties)
-  writer.writerows(recs)
-
-  return header + output.getvalue(), survey.link_id
+    try:
+      first = record_query.run().next()
+    except StopIteration:
+      # bail out early if survey_records.run() is empty
+      return header, survey.link_id
+  
+    # generate results list
+    recs = record_query.run()
+    recs = _get_records(recs, properties)
+  
+    # write results to CSV
+    output = StringIO.StringIO()
+    writer = csv.writer(output)
+    writer.writerow(properties)
+    writer.writerows(recs)
+  
+    return header + output.getvalue(), survey.link_id
+  return wrapper
--- a/app/soc/views/models/survey.py	Thu Jul 02 16:53:13 2009 +0200
+++ b/app/soc/views/models/survey.py	Thu Jul 02 17:08:10 2009 +0200
@@ -126,7 +126,7 @@
 
     new_params['export_content_type'] = 'text/text'
     new_params['export_extension'] = '.csv'
-    new_params['export_function'] = surveys.to_csv
+    new_params['export_function'] = surveys.to_csv(self)
     new_params['delete_redirect'] = '/'
     new_params['list_key_order'] = [
         'link_id', 'scope_path', 'name', 'short_name', 'title',