app/soc/views/models/grading_project_survey.py
changeset 2798 ec1857f0d0c7
parent 2765 b1c591bb2e87
--- a/app/soc/views/models/grading_project_survey.py	Tue Aug 25 15:28:50 2009 +0200
+++ b/app/soc/views/models/grading_project_survey.py	Tue Aug 25 15:58:21 2009 +0200
@@ -33,6 +33,10 @@
 from soc.views.helper import decorators
 from soc.views.helper import surveys
 from soc.views.models import project_survey
+from soc.views.helper import widgets as custom_widgets
+
+
+DEF_GRADE_CHOICES = (('pass', 'Pass'), ('fail', 'Fail'))
 
 
 class View(project_survey.View):
@@ -71,6 +75,7 @@
     new_params['name'] = "Grading Project Survey"
 
     new_params['survey_take_form'] = GradeSurveyTakeForm
+    new_params['survey_record_form'] = GradeSurveyRecordForm
 
     # used for sending reminders
     new_params['survey_type'] = 'grading'
@@ -179,8 +184,6 @@
   should be the same as the base class's if this argument is missing).
   """
 
-  DEF_GRADE_CHOICES = (('pass', 'Pass'), ('fail', 'Fail'))
-
   def setCleaners(self, post_dict=None):
     """Ensures that the grade field is added to the clean data.
 
@@ -235,7 +238,7 @@
     fields = super(GradeSurveyTakeForm, self).insertFields()
 
     # add empty option to choices
-    grade_choices = (('', "Choose a Grade"),) + tuple(self.DEF_GRADE_CHOICES)
+    grade_choices = (('', "Choose a Grade"),) + tuple(DEF_GRADE_CHOICES)
 
     gradeField = forms.fields.ChoiceField(choices=grade_choices,
                                           required=True,
@@ -247,6 +250,44 @@
     return fields
 
 
+
+class GradeSurveyRecordForm(surveys.SurveyRecordForm):
+  """RecordForm for the GradeSurveyTakeForm.
+  """
+
+  def getFields(self, *args):
+    """Add the extra grade field's value from survey_record.
+    """
+
+    # try to fetch from survey_record
+    if hasattr(self.survey_record, 'grade'):
+      grade = self.survey_record.grade
+
+    # remap bool to string values as the ChoiceField validates on 'choices'.
+    vals_grade = {True: 'pass', False: 'fail'}
+
+    self.data['grade'] = vals_grade.get(grade, None) or grade
+
+    return super(GradeSurveyRecordForm, self).getFields(*args)
+
+  def insertFields(self):
+    """Add ordered fields to self.fields, add grade field with grade choices.
+    """
+
+    # add common survey fields
+    fields = super(GradeSurveyRecordForm, self).insertFields()
+
+    # add empty option to choices
+    grade_choices = (('', "Choose a Grade"),) + tuple(DEF_GRADE_CHOICES)
+
+    gradeField = forms.fields.CharField(widget=custom_widgets.PlainTextWidget,
+                                        initial=self.data.get('grade'))
+    # add the grade field at the form's bottom
+    fields.insert(len(fields) + 1, 'grade', gradeField)
+
+    return fields
+
+
 view = View()
 
 create = decorators.view(view.create)