90 |
90 |
91 Returns: |
91 Returns: |
92 An instance of GradeSurveyTakeForm. |
92 An instance of GradeSurveyTakeForm. |
93 """ |
93 """ |
94 |
94 |
95 grade_choices = (('pass', 'Pass'), ('fail', 'Fail')) |
|
96 survey_form = GradeSurveyTakeForm(survey_content=survey.survey_content, |
95 survey_form = GradeSurveyTakeForm(survey_content=survey.survey_content, |
97 survey_record=record, |
96 survey_record=record, |
98 survey_logic=params['logic'], |
97 survey_logic=params['logic'], |
99 grade_choices=grade_choices, |
|
100 data=post_dict) |
98 data=post_dict) |
101 |
99 |
102 return survey_form |
100 return survey_form |
103 |
101 |
104 def _constructFilterForProjectSelection(self, survey, params): |
102 def _constructFilterForProjectSelection(self, survey, params): |
130 fields = {'mentor': mentor_entities, |
128 fields = {'mentor': mentor_entities, |
131 'status': 'accepted'} |
129 'status': 'accepted'} |
132 |
130 |
133 return fields |
131 return fields |
134 |
132 |
|
133 |
135 class GradeSurveyTakeForm(surveys.SurveyTakeForm): |
134 class GradeSurveyTakeForm(surveys.SurveyTakeForm): |
136 """Extends SurveyTakeForm by adding a grade field. |
135 """Extends SurveyTakeForm by adding a grade field. |
137 |
136 |
138 The grade field logic is dependent on the kwarg 'grade_choices' (behavior |
137 The grade field logic is dependent on the kwarg 'grade_choices' (behavior |
139 should be the same as the base class's if this argument is missing). |
138 should be the same as the base class's if this argument is missing). |
140 """ |
139 """ |
141 |
140 |
142 def __init__(self, *args, **kwargs): |
141 DEF_GRADE_CHOICES = (('pass', 'Pass'), ('fail', 'Fail')) |
143 """Store grade choices and initialize the form. |
|
144 |
|
145 params: |
|
146 grade_choices: pair of tuples representing the grading choices |
|
147 """ |
|
148 |
|
149 self.grade_choices = kwargs.pop('grade_choices', None) |
|
150 |
|
151 super(GradeSurveyTakeForm, self).__init__(*args, **kwargs) |
|
152 |
142 |
153 def setCleaners(self, post_dict=None): |
143 def setCleaners(self, post_dict=None): |
154 """Ensures that the grade field is added to the clean data. |
144 """Ensures that the grade field is added to the clean data. |
155 |
145 |
156 For args see surveys.SurveyTakeForm.setCleaners(). |
146 For args see surveys.SurveyTakeForm.setCleaners(). |
190 grade = self.survey_record.grade |
180 grade = self.survey_record.grade |
191 |
181 |
192 # remap bool to string values as the ChoiceField validates on 'choices'. |
182 # remap bool to string values as the ChoiceField validates on 'choices'. |
193 vals_grade = {True: 'pass', False: 'fail'} |
183 vals_grade = {True: 'pass', False: 'fail'} |
194 |
184 |
195 if self.grade_choices: |
185 self.data['grade'] = vals_grade.get(grade, None) or grade |
196 self.data['grade'] = vals_grade.get(grade, None) or grade |
|
197 |
186 |
198 return super(GradeSurveyTakeForm, self).getFields(post_dict) |
187 return super(GradeSurveyTakeForm, self).getFields(post_dict) |
199 |
188 |
200 def insertFields(self): |
189 def insertFields(self): |
201 """Add ordered fields to self.fields, add grade field with grade choices. |
190 """Add ordered fields to self.fields, add grade field with grade choices. |
202 """ |
191 """ |
203 |
192 |
204 # add common survey fields |
193 # add common survey fields |
205 fields = super(GradeSurveyTakeForm, self).insertFields() |
194 fields = super(GradeSurveyTakeForm, self).insertFields() |
206 |
195 |
207 if self.grade_choices: |
196 # add empty option to choices |
208 # add empty option to choices |
197 grade_choices = (('', "Choose a Grade"),) + tuple(self.DEF_GRADE_CHOICES) |
209 grade_choices = (('', "Choose a Grade"),) + tuple(self.grade_choices) |
198 |
210 |
199 gradeField = forms.fields.ChoiceField(choices=grade_choices, |
211 gradeField = forms.fields.ChoiceField(choices=grade_choices, |
200 required=True, |
212 required=True, |
201 widget=forms.Select(), |
213 widget=forms.Select(), |
202 initial=self.data.get('grade')) |
214 initial=self.data.get('grade')) |
203 # add the grade field at the form's bottom |
215 # add the grade field at the form's bottom |
204 fields.insert(len(fields) + 1, 'grade', gradeField) |
216 fields.insert(len(fields) + 1, 'grade', gradeField) |
|
217 |
205 |
218 return fields |
206 return fields |
219 |
207 |
220 |
208 |
221 view = View() |
209 view = View() |