Added SurveyRecordForm for viewing a SurveyRecord.
authorJames Levy <jamesalexanderlevy@gmail.com>
Tue, 25 Aug 2009 15:58:21 +0200
changeset 2798 ec1857f0d0c7
parent 2797 236e5b192295
child 2799 0fe7767592d0
Added SurveyRecordForm for viewing a SurveyRecord. This is subclassed into GradeSurveyRecord form for the GradingProjectSurveys. This should replace the need for the read_only kwarg in SurveyTakeForm. A TODO to try and extract this has been added. Fixes Issue 672. Reviewed by: Lennard de Rijk
app/soc/views/helper/surveys.py
app/soc/views/models/grading_project_survey.py
app/soc/views/models/survey.py
--- a/app/soc/views/helper/surveys.py	Tue Aug 25 15:28:50 2009 +0200
+++ b/app/soc/views/helper/surveys.py	Tue Aug 25 15:58:21 2009 +0200
@@ -45,6 +45,7 @@
 from soc.logic.lists import Lists
 from soc.models.survey import COMMENT_PREFIX
 from soc.models.survey import SurveyContent
+from soc.views.helper import widgets as custom_widgets
 
 
 CHOICE_TYPES = set(('selection', 'pick_multi', 'choice', 'pick_quant'))
@@ -96,6 +97,7 @@
     self.survey_content = self.kwargs.pop('survey_content', None)
     self.survey_logic = self.kwargs.pop('survey_logic', None)
     self.survey_record = self.kwargs.pop('survey_record', None)
+    # TODO: This should be removed since readonly is covered by the RecordForm
     self.read_only = self.kwargs.pop('read_only', None)
 
     self.fields_map = dict(
@@ -455,6 +457,51 @@
     exclude = ['schema']
 
 
+class SurveyRecordForm(SurveyTakeForm):
+  """SurveyContent form for showing record of survey answers.
+  """
+
+  def addLongField(self, field, value, attrs, schema, **kwargs):
+    """Add plain text long answer fields to this form.
+
+    params:
+      field: the current field
+      value: the initial value for this field
+      attrs: additional attributes for field
+      schema: schema for survey
+    """
+
+    question = CharField(widget=custom_widgets.PlainTextWidget, initial=value)
+    self.survey_fields[field] = question
+
+  def addShortField(self, field, value, attrs, schema, **kwargs):
+    """Add plain text short answer fields to this form.
+
+    params:
+      field: the current field
+      value: the initial value for this field
+      attrs: additional attributes for field
+      schema: schema for survey
+    """
+
+    question = CharField(widget=custom_widgets.PlainTextWidget, initial=value)
+    self.survey_fields[field] = question
+
+  def addCommentField(self, field, comment, attrs, tip):
+    """Add plain text comment field to a question.
+
+    params:
+      field: the name of the field to add the comment field to
+      comment: the initial value of this field.
+      attrs: the attrs for the widget
+      tip: tooltip text for this field
+    """
+
+    widget = custom_widgets.PlainTextWidget
+    comment_field = CharField(label='Comment', widget=widget, initial=comment)
+    self.survey_fields[COMMENT_PREFIX + field] = comment_field
+
+
 class SurveyEditForm(djangoforms.ModelForm):
   """SurveyContent form for editing a survey.
 
--- 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)
--- a/app/soc/views/models/survey.py	Tue Aug 25 15:28:50 2009 +0200
+++ b/app/soc/views/models/survey.py	Tue Aug 25 15:58:21 2009 +0200
@@ -195,6 +195,7 @@
         }
 
     new_params['survey_take_form'] = surveys.SurveyTakeForm
+    new_params['survey_record_form'] = surveys.SurveyRecordForm
 
     params = dicts.merge(params, new_params, sub_merge=True)
 
@@ -787,7 +788,7 @@
     context['record'] = record_entity
 
     # store the read only survey form in the context
-    survey_form = params['survey_take_form'](
+    survey_form = params['survey_record_form'](
        survey_content=survey_entity.survey_content,
        survey_record=record_entity,
        survey_logic=self._params['logic'],