Added getSurveyResponseFromPost method to Surveys Helper.
authorLennard de Rijk <ljvderijk@gmail.com>
Thu, 02 Jul 2009 12:45:06 +0200
changeset 2483 be8a113ee021
parent 2482 0e544df4d925
child 2484 f41a9e1eeeea
Added getSurveyResponseFromPost method to Surveys Helper. This method will be used to read the POST dictionary from a request. And will transform it into the fields that need to be set in a SurveyRecord for the questions specified by the given Survey. A TODO has been added to handle the naming/handling of the comment fields.
app/soc/views/helper/surveys.py
--- a/app/soc/views/helper/surveys.py	Thu Jul 02 12:43:06 2009 +0200
+++ b/app/soc/views/helper/surveys.py	Thu Jul 02 12:45:06 2009 +0200
@@ -597,6 +597,40 @@
     return markup
 
 
+def getSurveyResponseFromPost(survey, post_dict):
+  """Returns the data for a SurveyRecord that answer the questions
+     posed in the given Survey's schema.
+
+  Args:
+      survey: a Survey entity
+      post_dict: dictionary with data from the POST request
+  """
+
+  # TODO(ljvderijk) deal with the comment fields
+
+  # get the schema for this survey
+  schema = eval(survey.survey_content.schema)
+
+  # fill a dictionary with the data to be stored in the SurveyRecord
+  response_dict = {}
+
+  for name, value in post_dict.items():
+    # make sure name is a string
+    name = name.encode()
+    if name not in schema:
+      # property not in survey schema ignore
+      continue
+    else:
+      pick_multi = schema[name]['type'] == 'pick_multi'
+
+      if pick_multi and hasattr(post_dict, 'getlist'): # it's a multidict
+        response_dict[name] = ','.join(post_dict.getlist(name))
+      else:
+        response_dict[name] = value
+
+  return response_dict
+
+
 def getRoleSpecificFields(survey, user, this_project, survey_form,
                           survey_record):
   """For evaluations, mentors get required Project and Grade fields, and