app/soc/views/models/grading_project_survey.py
changeset 2798 ec1857f0d0c7
parent 2765 b1c591bb2e87
equal deleted inserted replaced
2797:236e5b192295 2798:ec1857f0d0c7
    31 from soc.logic.models.user import logic as user_logic
    31 from soc.logic.models.user import logic as user_logic
    32 from soc.views.helper import access
    32 from soc.views.helper import access
    33 from soc.views.helper import decorators
    33 from soc.views.helper import decorators
    34 from soc.views.helper import surveys
    34 from soc.views.helper import surveys
    35 from soc.views.models import project_survey
    35 from soc.views.models import project_survey
       
    36 from soc.views.helper import widgets as custom_widgets
       
    37 
       
    38 
       
    39 DEF_GRADE_CHOICES = (('pass', 'Pass'), ('fail', 'Fail'))
    36 
    40 
    37 
    41 
    38 class View(project_survey.View):
    42 class View(project_survey.View):
    39   """View methods for the GradingProjectSurvey model.
    43   """View methods for the GradingProjectSurvey model.
    40   """
    44   """
    69     new_params['rights'] = rights
    73     new_params['rights'] = rights
    70 
    74 
    71     new_params['name'] = "Grading Project Survey"
    75     new_params['name'] = "Grading Project Survey"
    72 
    76 
    73     new_params['survey_take_form'] = GradeSurveyTakeForm
    77     new_params['survey_take_form'] = GradeSurveyTakeForm
       
    78     new_params['survey_record_form'] = GradeSurveyRecordForm
    74 
    79 
    75     # used for sending reminders
    80     # used for sending reminders
    76     new_params['survey_type'] = 'grading'
    81     new_params['survey_type'] = 'grading'
    77 
    82 
    78     new_params['manage_student_project_heading'] = \
    83     new_params['manage_student_project_heading'] = \
   177 
   182 
   178   The grade field logic is dependent on the kwarg 'grade_choices' (behavior
   183   The grade field logic is dependent on the kwarg 'grade_choices' (behavior
   179   should be the same as the base class's if this argument is missing).
   184   should be the same as the base class's if this argument is missing).
   180   """
   185   """
   181 
   186 
   182   DEF_GRADE_CHOICES = (('pass', 'Pass'), ('fail', 'Fail'))
       
   183 
       
   184   def setCleaners(self, post_dict=None):
   187   def setCleaners(self, post_dict=None):
   185     """Ensures that the grade field is added to the clean data.
   188     """Ensures that the grade field is added to the clean data.
   186 
   189 
   187     For args see surveys.SurveyTakeForm.setCleaners().
   190     For args see surveys.SurveyTakeForm.setCleaners().
   188     """
   191     """
   233 
   236 
   234     # add common survey fields
   237     # add common survey fields
   235     fields = super(GradeSurveyTakeForm, self).insertFields()
   238     fields = super(GradeSurveyTakeForm, self).insertFields()
   236 
   239 
   237     # add empty option to choices
   240     # add empty option to choices
   238     grade_choices = (('', "Choose a Grade"),) + tuple(self.DEF_GRADE_CHOICES)
   241     grade_choices = (('', "Choose a Grade"),) + tuple(DEF_GRADE_CHOICES)
   239 
   242 
   240     gradeField = forms.fields.ChoiceField(choices=grade_choices,
   243     gradeField = forms.fields.ChoiceField(choices=grade_choices,
   241                                           required=True,
   244                                           required=True,
   242                                           widget=forms.Select(),
   245                                           widget=forms.Select(),
   243                                           initial=self.data.get('grade'))
   246                                           initial=self.data.get('grade'))
       
   247     # add the grade field at the form's bottom
       
   248     fields.insert(len(fields) + 1, 'grade', gradeField)
       
   249 
       
   250     return fields
       
   251 
       
   252 
       
   253 
       
   254 class GradeSurveyRecordForm(surveys.SurveyRecordForm):
       
   255   """RecordForm for the GradeSurveyTakeForm.
       
   256   """
       
   257 
       
   258   def getFields(self, *args):
       
   259     """Add the extra grade field's value from survey_record.
       
   260     """
       
   261 
       
   262     # try to fetch from survey_record
       
   263     if hasattr(self.survey_record, 'grade'):
       
   264       grade = self.survey_record.grade
       
   265 
       
   266     # remap bool to string values as the ChoiceField validates on 'choices'.
       
   267     vals_grade = {True: 'pass', False: 'fail'}
       
   268 
       
   269     self.data['grade'] = vals_grade.get(grade, None) or grade
       
   270 
       
   271     return super(GradeSurveyRecordForm, self).getFields(*args)
       
   272 
       
   273   def insertFields(self):
       
   274     """Add ordered fields to self.fields, add grade field with grade choices.
       
   275     """
       
   276 
       
   277     # add common survey fields
       
   278     fields = super(GradeSurveyRecordForm, self).insertFields()
       
   279 
       
   280     # add empty option to choices
       
   281     grade_choices = (('', "Choose a Grade"),) + tuple(DEF_GRADE_CHOICES)
       
   282 
       
   283     gradeField = forms.fields.CharField(widget=custom_widgets.PlainTextWidget,
       
   284                                         initial=self.data.get('grade'))
   244     # add the grade field at the form's bottom
   285     # add the grade field at the form's bottom
   245     fields.insert(len(fields) + 1, 'grade', gradeField)
   286     fields.insert(len(fields) + 1, 'grade', gradeField)
   246 
   287 
   247     return fields
   288     return fields
   248 
   289