Ensure that the Grade field data stays in clean_data.
authorLennard de Rijk <ljvderijk@gmail.com>
Mon, 06 Jul 2009 17:18:41 +0200
changeset 2564 81b36f56d61a
parent 2563 eec0d98b38a1
child 2565 c17b671e5657
Ensure that the Grade field data stays in clean_data. This prevents a bug that happens when you pick an invalid entry in the choice a grade field. If this bug was triggered the value of the survey_record.grade was used. Now however a proper ValidationError is raised.
app/soc/views/helper/surveys.py
app/soc/views/models/grading_project_survey.py
--- a/app/soc/views/helper/surveys.py	Mon Jul 06 17:03:19 2009 +0200
+++ b/app/soc/views/helper/surveys.py	Mon Jul 06 17:18:41 2009 +0200
@@ -138,6 +138,9 @@
     it's possible to set clean_[field_id] methods for validation.
 
     This method populates the 'data' dict used for generating form fields.
+
+    Args:
+      post_dict: dictionary used to populate the fields
     """
 
     # prefix for method names
--- a/app/soc/views/models/grading_project_survey.py	Mon Jul 06 17:03:19 2009 +0200
+++ b/app/soc/views/models/grading_project_survey.py	Mon Jul 06 17:18:41 2009 +0200
@@ -155,14 +155,21 @@
 
     self.grade_choices = kwargs.pop('grade_choices', None)
 
-    if self.grade_choices:
-      # add grade field to self.data, respecting the data kwarg if present
-      if kwargs.get('data') and kwargs['data'].get('grade'):
-        data = {}
-        data['grade'] = kwargs['data']['grade']
-        self.data = data
+    super(GradeSurveyTakeForm, self).__init__(*args, **kwargs)
+
+  def setCleaners(self, post_dict=None):
+    """Ensures that the grade field is added to the clean data.
+
+    For args see surveys.SurveyTakeForm.setCleaners().
+    """
 
-    super(GradeSurveyTakeForm, self).__init__(*args, **kwargs)
+    clean_data = super(GradeSurveyTakeForm, self).setCleaners(
+        post_dict=post_dict)
+
+    if post_dict:
+      clean_data['grade'] = post_dict.get('grade', None)
+
+    return clean_data
 
   def clean_grade(self):
     """Validate the grade field.
@@ -186,7 +193,7 @@
     grade = post_dict.get('grade', None)
 
     # otherwise, try to fetch from survey_record
-    if not grade and hasattr(self.survey_record, 'grade'):
+    if grade == None and hasattr(self.survey_record, 'grade'):
       grade = self.survey_record.grade
 
     # remap bool to string values as the ChoiceField validates on 'choices'.