app/soc/views/helper/surveys.py
changeset 2659 8df08a3b17db
parent 2658 34b414a80d42
child 2670 884f808d8469
equal deleted inserted replaced
2658:34b414a80d42 2659:8df08a3b17db
   949     form.fields['last_modified_by'].initial = instance.modified_by.name
   949     form.fields['last_modified_by'].initial = instance.modified_by.name
   950     form.fields['doc_key_name'].initial = instance.key().id_or_name()
   950     form.fields['doc_key_name'].initial = instance.key().id_or_name()
   951     return form
   951     return form
   952 
   952 
   953 
   953 
   954 def _get_csv_header(sur):
   954 def getCSVHeader(survey_entity):
   955   """CSV header helper, needs support for comment lines in CSV.
   955   """CSV header helper, needs support for comment lines in CSV.
   956 
   956 
   957   Args:
   957   Args:
   958       sur: Survey entity
   958       survey_entity: Survey entity
   959   """
   959   """
   960 
   960 
   961   tpl = '# %s: %s\n'
   961   tpl = '# %s: %s\n'
   962 
   962 
   963   # add static properties
   963   # add static properties
   964   fields = ['# Melange Survey export for \n#  %s\n#\n' % sur.title]
   964   fields = ['# Melange Survey export for \n#  %s\n#\n' % survey_entity.title]
   965   fields += [tpl % (k,v) for k,v in sur.toDict().items()]
   965   fields += [tpl % (k,v) for k,v in survey_entity.toDict().items()]
   966   fields += [tpl % (f, str(getattr(sur, f))) for f in PLAIN.split()]
   966   fields += [tpl % (f, str(getattr(survey_entity, f))) for f in PLAIN.split()]
   967   fields += [tpl % (f, str(getattr(sur, f).link_id)) for f in FIELDS.split()]
   967   fields += [tpl % (f, str(getattr(survey_entity, f).link_id))
       
   968              for f in FIELDS.split()]
   968   fields.sort()
   969   fields.sort()
   969 
   970 
   970   # add dynamic properties
   971   # add dynamic properties
   971   fields += ['#\n#---\n#\n']
   972   fields += ['#\n#---\n#\n']
   972   dynamic = sur.survey_content.dynamic_properties()
   973   dynamic = survey_entity.survey_content.dynamic_properties()
   973   dynamic = [(prop, getattr(sur.survey_content, prop)) for prop in dynamic]
   974   dynamic = [(prop, getattr(survey_entity.survey_content, prop))
       
   975              for prop in dynamic]
   974   fields += [tpl % (k,v) for k,v in sorted(dynamic)]
   976   fields += [tpl % (k,v) for k,v in sorted(dynamic)]
   975 
   977 
   976   # add schema
   978   # add schema
   977   fields += ['#\n#---\n#\n']
   979   fields += ['#\n#---\n#\n']
   978   schema =  sur.survey_content.schema
   980   schema =  survey_entity.survey_content.schema
   979   indent = '},\n#' + ' ' * 9
   981   indent = '},\n#' + ' ' * 9
   980   fields += [tpl % ('Schema', schema.replace('},', indent)) + '#\n']
   982   fields += [tpl % ('Schema', schema.replace('},', indent)) + '#\n']
   981 
   983 
   982   return ''.join(fields).replace('\n', '\r\n')
   984   return ''.join(fields).replace('\n', '\r\n')
   983 
   985 
   984 
   986 
   985 def _get_records(recs, props):
   987 def getRecords(recs, props):
   986   """Fetch properties from SurveyRecords for CSV export.
   988   """Fetch properties from SurveyRecords for CSV export.
   987   """
   989   """
   988 
   990 
   989   records = []
   991   records = []
   990   props = props[1:]
   992   props = props[1:]
  1007     """
  1009     """
  1008     survey_logic = survey_view.getParams()['logic']
  1010     survey_logic = survey_view.getParams()['logic']
  1009     record_logic = survey_logic.getRecordLogic()
  1011     record_logic = survey_logic.getRecordLogic()
  1010 
  1012 
  1011     # get header and properties
  1013     # get header and properties
  1012     header = _get_csv_header(survey)
  1014     header = getCSVHeader(survey)
  1013     leading = ['user', 'created', 'modified']
  1015     leading = ['user', 'created', 'modified']
  1014     properties = leading + survey.survey_content.orderedProperties()
  1016     properties = leading + survey.survey_content.orderedProperties()
  1015 
  1017 
  1016     # retrieve the query of the data to export
  1018     # retrieve the query of the data to export
  1017     fields = {'survey': survey}
  1019     fields = {'survey': survey}
  1023       # bail out early if survey_records.run() is empty
  1025       # bail out early if survey_records.run() is empty
  1024       return header, survey.link_id
  1026       return header, survey.link_id
  1025 
  1027 
  1026     # generate results list
  1028     # generate results list
  1027     recs = record_query.run()
  1029     recs = record_query.run()
  1028     recs = _get_records(recs, properties)
  1030     recs = getRecords(recs, properties)
  1029 
  1031 
  1030     # write results to CSV
  1032     # write results to CSV
  1031     output = StringIO.StringIO()
  1033     output = StringIO.StringIO()
  1032     writer = csv.writer(output)
  1034     writer = csv.writer(output)
  1033     writer.writerow(properties)
  1035     writer.writerow(properties)