# HG changeset patch # User Lennard de Rijk # Date 1246893521 -7200 # Node ID 81b36f56d61a3aa67af4d5f61a44317986ae344d # Parent eec0d98b38a1e64c3b8b2e55e6ed0a119dff4d81 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. diff -r eec0d98b38a1 -r 81b36f56d61a app/soc/views/helper/surveys.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 diff -r eec0d98b38a1 -r 81b36f56d61a app/soc/views/models/grading_project_survey.py --- 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'.