app/soc/views/helper/surveys.py
author Lennard de Rijk <ljvderijk@gmail.com>
Tue, 30 Jun 2009 14:55:55 +0200
changeset 2463 05af53239799
parent 2451 c58a7ea6c126
child 2465 037a7e79a635
permissions -rw-r--r--
Add more flexibility to the Survey helper for use with the new Survey types. This includes using the new SurveyLogic methods from an earlier commit. And passing on the Logic from the view where necessary.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
     2
#
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
     3
# Copyright 2009 the Melange authors.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
     4
#
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
     8
#
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    10
#
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    15
# limitations under the License.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    16
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    17
"""Custom widgets used for Survey form fields, plus the SurveyContent form.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    18
"""
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    19
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    20
__authors__ = [
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    21
  '"Daniel Diniz" <ajaksu@gmail.com>',
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    22
  '"James Levy" <jamesalexanderlevy@gmail.com>',
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    23
  '"Lennard de Rijk" <ljvderijk@gmail.com>',
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    24
  ]
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    25
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    26
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    27
from itertools import chain
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    28
import datetime
2442
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
    29
import logging
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    30
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    31
from google.appengine.ext.db import djangoforms
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    32
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    33
from django import forms
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    34
from django.forms import widgets
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    35
from django.forms.fields import CharField
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    36
from django.template import loader
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    37
from django.utils.encoding import force_unicode
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    38
from django.utils.html import escape
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    39
from django.utils.safestring import mark_safe
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    40
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    41
from soc.logic import dicts
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    42
from soc.logic.lists import Lists
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    43
from soc.models.survey import SurveyContent
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    44
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    45
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    46
class SurveyForm(djangoforms.ModelForm):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    47
  """Main SurveyContent form.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    48
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    49
  This class is used to produce survey forms for several circumstances:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    50
    - Admin creating survey from scratch
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    51
    - Admin updating existing survey
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    52
    - User taking survey
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    53
    - User updating already taken survey
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    54
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    55
  Using dynamic properties of the survey model (if passed as an arg) the
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    56
  survey form is dynamically formed.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    57
  """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    58
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    59
  def __init__(self, *args, **kwargs):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    60
    """Store special kwargs as attributes.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    61
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    62
      read_only: controls whether the survey taking UI allows data entry.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    63
      editing: controls whether to show the edit or show form.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    64
    """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    65
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    66
    self.kwargs = kwargs
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
    67
    self.survey_content = self.kwargs.pop('survey_content', None)
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
    68
    self.this_user = self.kwargs.pop('this_user', None)
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
    69
    self.project = self.kwargs.pop('project', None)
2463
05af53239799 Add more flexibility to the Survey helper for use with the new Survey types.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2451
diff changeset
    70
    self.survey_logic = self.kwargs.pop('survey_logic', None)
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
    71
    self.survey_record = self.kwargs.pop('survey_record', None)
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
    72
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
    73
    self.read_only = self.kwargs.pop('read_only', None)
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
    74
    self.editing = self.kwargs.pop('editing', None)
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    75
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
    76
    self.fields_map = dict(
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
    77
        long_answer=self.addLongField,
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
    78
        short_answer=self.addShortField,
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
    79
        selection=self.addSingleField,
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
    80
        pick_multi=self.addMultiField,
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
    81
        pick_quant=self.addQuantField,
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
    82
        )
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    83
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    84
    super(SurveyForm, self).__init__(*args, **self.kwargs)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    85
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    86
  def getFields(self):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    87
    """Build the SurveyContent (questions) form fields.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    88
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    89
    Populates self.survey_fields, which will be ordered in self.insert_fields.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    90
    """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    91
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    92
    if not self.survey_content:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    93
      return
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    94
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    95
    self.survey_fields = {}
2442
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
    96
    schema = SurveyContentSchema(self.survey_content.schema)
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    97
    has_record = (not self.editing) and self.survey_record
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    98
    extra_attrs = {}
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    99
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   100
    # figure out whether we want a read-only view
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   101
    if not self.editing:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   102
      # only survey taking can be read-only
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   103
      read_only = self.read_only
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   104
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   105
      if not read_only:
2463
05af53239799 Add more flexibility to the Survey helper for use with the new Survey types.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2451
diff changeset
   106
        survey_content = self.survey_content
05af53239799 Add more flexibility to the Survey helper for use with the new Survey types.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2451
diff changeset
   107
        survey_entity = self.survey_logic.getSurveyForContent(survey_content)
05af53239799 Add more flexibility to the Survey helper for use with the new Survey types.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2451
diff changeset
   108
        deadline = survey_entity.survey_end
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   109
        read_only =  deadline and (datetime.datetime.now() > deadline)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   110
      else:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   111
        extra_attrs['disabled'] = 'disabled'
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   112
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   113
    # add unordered fields to self.survey_fields
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   114
    for field in self.survey_content.dynamic_properties():
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   115
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   116
      # a comment made by the user
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   117
      comment = ''
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   118
      if has_record and hasattr(self.survey_record, field):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   119
        # previously entered value
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   120
        value = getattr(self.survey_record, field)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   121
        if hasattr(self.survey_record, 'comment_for_' + field):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   122
          comment = getattr(self.survey_record, 'comment_for_' + field)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   123
      else:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   124
        # use prompts set by survey creator
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   125
        value = getattr(self.survey_content, field)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   126
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   127
      label = schema.getLabel(field)
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   128
      if label is None:
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   129
        continue
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   130
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   131
      # dispatch to field-specific methods
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   132
      addField = self.fields_map[schema.getType(field)]
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   133
      addField(field, value, extra_attrs, schema, label=label, comment=comment)
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   134
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   135
    return self.insertFields()
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   136
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   137
  def insertFields(self):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   138
    """Add ordered fields to self.fields.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   139
    """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   140
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   141
    survey_order = self.survey_content.getSurveyOrder()
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   142
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   143
    # first, insert dynamic survey fields
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   144
    for position, property in survey_order.items():
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   145
      position = position * 2
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   146
      self.fields.insert(position, property, self.survey_fields[property])
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   147
      if not self.editing:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   148
        property = 'comment_for_' + property
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   149
        self.fields.insert(position - 1, property,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   150
                           self.survey_fields[property])
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   151
    return self.fields
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   152
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   153
  def addLongField(self, field, value, attrs, schema, req=False, label='', tip='',
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   154
                   comment=''):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   155
    """Add a long answer fields to this form.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   156
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   157
    params:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   158
      field: the current field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   159
      value: the initial value for this field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   160
      attrs: additional attributes for field
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   161
      schema: schema for survey
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   162
      req: required bool
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   163
      label: label for field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   164
      tip: tooltip text for field
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   165
      comment: initial comment value for field
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   166
    """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   167
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   168
    widget = widgets.Textarea(attrs=attrs)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   169
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   170
    if not tip:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   171
      tip = 'Please provide a long answer to this question.'
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   172
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   173
    question = CharField(help_text=tip, required=req, label=label,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   174
                         widget=widget, initial=value)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   175
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   176
    self.survey_fields[field] = question
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   177
    self.addCommentField(field, comment, attrs, tip)
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   178
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   179
  def addShortField(self, field, value, attrs, schema, req=False, label='', tip='',
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   180
                    comment=''):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   181
    """Add a short answer fields to this form.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   182
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   183
    params:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   184
      field: the current field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   185
      value: the initial value for this field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   186
      attrs: additional attributes for field
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   187
      schema: schema for survey
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   188
      req: required bool
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   189
      label: label for field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   190
      tip: tooltip text for field
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   191
      comment: initial comment value for field
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   192
    """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   193
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   194
    attrs['class'] = "text_question"
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   195
    widget = widgets.TextInput(attrs=attrs)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   196
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   197
    if not tip:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   198
      tip = 'Please provide a short answer to this question.'
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   199
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   200
    question = CharField(help_text=tip, required=req, label=label,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   201
                         widget=widget, max_length=140, initial=value)
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   202
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   203
    self.survey_fields[field] = question
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   204
    self.addCommentField(field, comment, attrs, tip)
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   205
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   206
  def addSingleField(self, field, value, attrs, schema, req=False, label='',
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   207
                     tip='', comment=''):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   208
    """Add a selection field to this form.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   209
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   210
    params:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   211
      field: the current field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   212
      value: the initial value for this field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   213
      attrs: additional attributes for field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   214
      schema: schema for survey
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   215
      req: required bool
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   216
      label: label for field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   217
      tip: tooltip text for field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   218
      comment: initial comment value for field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   219
    """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   220
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   221
    widget = schema.getWidget(field, self.editing, attrs)
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   222
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   223
    these_choices = []
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   224
    # add all properties, but select chosen one
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   225
    options = getattr(self.survey_content, field)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   226
    has_record = not self.editing and self.survey_record
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   227
    if has_record and hasattr(self.survey_record, field):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   228
      these_choices.append((value, value))
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   229
      if value in options:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   230
        options.remove(value)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   231
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   232
    for option in options:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   233
      these_choices.append((option, option))
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   234
    if not tip:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   235
      tip = 'Please select an answer this question.'
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   236
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   237
    question = PickOneField(help_text=tip, required=req, label=label,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   238
                            choices=tuple(these_choices), widget=widget)
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   239
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   240
    self.survey_fields[field] = question
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   241
    self.addCommentField(field, comment, attrs, tip)
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   242
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   243
  def addMultiField(self, field, value, attrs, schema, req=False, label='',
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   244
                    tip='', comment=''):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   245
    """Add a pick_multi field to this form.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   246
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   247
    params:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   248
      field: the current field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   249
      value: the initial value for this field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   250
      attrs: additional attributes for field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   251
      schema: schema for survey
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   252
      req: required bool
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   253
      label: label for field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   254
      tip: tooltip text for field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   255
      comment: initial comment value for field
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   256
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   257
    """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   258
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   259
    widget = schema.getWidget(field, self.editing, attrs)
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   260
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   261
    # TODO(ajaksu) need to allow checking checkboxes by default
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   262
    if self.survey_record and isinstance(value, basestring):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   263
      # pass value as 'initial' so MultipleChoiceField renders checked boxes
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   264
      value = value.split(',')
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   265
    else:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   266
      value = None
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   267
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   268
    these_choices = [(v,v) for v in getattr(self.survey_content, field)]
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   269
    if not tip:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   270
      tip = 'Please select one or more of these choices.'
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   271
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   272
    question = PickManyField(help_text=tip, required=req, label=label,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   273
                             choices=tuple(these_choices), widget=widget,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   274
                             initial=value)
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   275
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   276
    self.survey_fields[field] = question
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   277
    self.addCommentField(field, comment, attrs, tip)
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   278
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   279
  def addQuantField(self, field, value, attrs, schema, req=False, label='',
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   280
                    tip='', comment=''):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   281
    """Add a pick_quant field to this form.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   282
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   283
    params:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   284
      field: the current field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   285
      value: the initial value for this field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   286
      attrs: additional attributes for field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   287
      schema: schema for survey
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   288
      req: required bool
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   289
      label: label for field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   290
      tip: tooltip text for field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   291
      comment: initial comment value for field
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   292
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   293
    """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   294
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   295
    widget = schema.getWidget(field, self.editing, attrs)
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   296
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   297
    if self.survey_record:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   298
      value = value
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   299
    else:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   300
      value = None
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   301
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   302
    these_choices = [(v,v) for v in getattr(self.survey_content, field)]
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   303
    if not tip:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   304
      tip = 'Please select one of these choices.'
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   305
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   306
    question = PickQuantField(help_text=tip, required=req, label=label,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   307
                             choices=tuple(these_choices), widget=widget,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   308
                             initial=value)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   309
    self.survey_fields[field] = question
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   310
    self.addCommentField(field, comment, attrs, tip)
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   311
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   312
  def addCommentField(self, field, comment, attrs, tip):
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   313
    if not self.editing:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   314
      widget = widgets.Textarea(attrs=attrs)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   315
      comment = CharField(help_text=tip, required=False, label='Comments',
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   316
                          widget=widget, initial=comment)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   317
      self.survey_fields['comment_for_' + field] = comment
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   318
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   319
  class Meta(object):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   320
    model = SurveyContent
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   321
    exclude = ['schema']
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   322
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   323
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   324
class SurveyContentSchema(object):
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   325
  """Abstract question metadata handling.
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   326
  """
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   327
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   328
  def __init__(self, schema):
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   329
    self.schema = eval(schema)
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   330
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   331
  def getType(self, field):
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   332
    return self.schema[field]["type"]
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   333
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   334
  def getRender(self, field):
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   335
    return self.schema[field]["render"]
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   336
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   337
  def getWidget(self, field, editing, attrs):
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   338
    """Get survey editing or taking widget for choice questions.
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   339
    """
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   340
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   341
    if editing:
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   342
      kind = self.getType(field)
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   343
      render = self.getRender(field)
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   344
      widget = UniversalChoiceEditor(kind, render)
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   345
    else:
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   346
      widget = WIDGETS[self.schema[field]['render']](attrs=attrs)
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   347
    return widget
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   348
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   349
  def getLabel(self, field):
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   350
    """Fetch the free text 'question' or use field name as label.
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   351
    """
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   352
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   353
    if field not in self.schema:
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   354
      logging.error('field %s not found in schema %s' %
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   355
                    (field, str(self.schema)))
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   356
      return
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   357
    elif 'question' in self.schema[field]:
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   358
      label = self.schema[field].get('question') or field
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   359
    else:
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   360
      label = field
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   361
    return label
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   362
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   363
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   364
class UniversalChoiceEditor(widgets.Widget):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   365
  """Edit interface for choice questions.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   366
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   367
  Allows adding and removing options, re-ordering and editing option text.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   368
  """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   369
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   370
  def __init__(self, kind, render, attrs=None, choices=()):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   371
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   372
    self.attrs = attrs or {}
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   373
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   374
    # Choices can be any iterable, but we may need to render this widget
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   375
    # multiple times. Thus, collapse it into a list so it can be consumed
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   376
    # more than once.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   377
    self.choices = list(choices)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   378
    self.kind = kind
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   379
    self.render_as = render
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   380
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   381
  def render(self, name, value, attrs=None, choices=()):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   382
    """ renders UCE widget
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   383
    """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   384
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   385
    if value is None:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   386
      value = ''
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   387
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   388
    final_attrs = self.build_attrs(attrs, name=name)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   389
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   390
    # find out which options should be selected in type and render drop-downs.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   391
    selected = 'selected="selected"'
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   392
    context =  dict(
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   393
        name=name,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   394
        is_selection=selected * (self.kind == 'selection'),
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   395
        is_pick_multi=selected * (self.kind == 'pick_multi'),
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   396
        is_pick_quant=selected * (self.kind == 'pick_quant'),
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   397
        is_select=selected * (self.render_as == 'single_select'),
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   398
        is_checkboxes=selected * (self.render_as == 'multi_checkbox'),
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   399
        is_radio_buttons=selected * (self.render_as == 'quant_radio'),
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   400
        )
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   401
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   402
    str_value = forms.util.smart_unicode(value) # normalize to string.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   403
    chained_choices = enumerate(chain(self.choices, choices))
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   404
    choices = {}
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   405
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   406
    for i, (option_value, option_label) in chained_choices:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   407
      option_value = escape(forms.util.smart_unicode(option_value))
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   408
      choices[i] = option_value
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   409
    context['choices'] = choices
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   410
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   411
    template = 'soc/survey/universal_choice_editor.html'
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   412
    return loader.render_to_string(template, context)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   413
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   414
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   415
class PickOneField(forms.ChoiceField):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   416
  """Stub for customizing the single choice field.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   417
  """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   418
  #TODO(james): Ensure that more than one option cannot be selected
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   419
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   420
  def __init__(self, *args, **kwargs):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   421
    super(PickOneField, self).__init__(*args, **kwargs)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   422
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   423
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   424
class PickManyField(forms.MultipleChoiceField):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   425
  """Stub for customizing the multiple choice field.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   426
  """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   427
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   428
  def __init__(self, *args, **kwargs):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   429
    super(PickManyField, self).__init__(*args, **kwargs)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   430
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   431
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   432
class PickQuantField(forms.MultipleChoiceField):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   433
  """Stub for customizing the multiple choice field.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   434
  """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   435
  #TODO(james): Ensure that more than one quant cannot be selected
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   436
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   437
  def __init__(self, *args, **kwargs):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   438
    super(PickQuantField, self).__init__(*args, **kwargs)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   439
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   440
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   441
class PickOneSelect(forms.Select):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   442
  """Stub for customizing the single choice select widget.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   443
  """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   444
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   445
  def __init__(self, *args, **kwargs):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   446
    super(PickOneSelect, self).__init__(*args, **kwargs)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   447
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   448
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   449
class PickManyCheckbox(forms.CheckboxSelectMultiple):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   450
  """Customized multiple choice checkbox widget.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   451
  """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   452
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   453
  def __init__(self, *args, **kwargs):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   454
    super(PickManyCheckbox, self).__init__(*args, **kwargs)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   455
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   456
  def render(self, name, value, attrs=None, choices=()):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   457
    """Render checkboxes as list items grouped in a fieldset.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   458
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   459
    This is the pick_multi widget for survey taking
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   460
    """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   461
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   462
    if value is None:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   463
      value = []
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   464
    has_id = attrs and attrs.has_key('id')
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   465
    final_attrs = self.build_attrs(attrs, name=name)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   466
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   467
    # normalize to strings.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   468
    str_values = set([forms.util.smart_unicode(v) for v in value])
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   469
    is_checked = lambda value: value in str_values
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   470
    smart_unicode = forms.util.smart_unicode
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   471
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   472
    # set container fieldset and list
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   473
    output = [u'<fieldset id="id_%s">\n  <ul class="pick_multi">' % name]
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   474
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   475
    # add numbered checkboxes wrapped in list items
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   476
    chained_choices = enumerate(chain(self.choices, choices))
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   477
    for i, (option_value, option_label) in chained_choices:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   478
      option_label = escape(smart_unicode(option_label))
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   479
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   480
      # If an ID attribute was given, add a numeric index as a suffix,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   481
      # so that the checkboxes don't all have the same ID attribute.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   482
      if has_id:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   483
        final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i))
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   484
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   485
      cb = widgets.CheckboxInput(final_attrs, check_test=is_checked)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   486
      rendered_cb = cb.render(name, option_value)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   487
      cb_label = (rendered_cb, option_label)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   488
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   489
      output.append(u'    <li><label>%s %s</label></li>' % cb_label)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   490
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   491
    output.append(u'  </ul>\n</fieldset>')
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   492
    return u'\n'.join(output)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   493
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   494
  def id_for_label(self, id_):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   495
    # see the comment for RadioSelect.id_for_label()
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   496
    if id_:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   497
      id_ += '_fieldset'
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   498
    return id_
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   499
  id_for_label = classmethod(id_for_label)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   500
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   501
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   502
class PickQuantRadioRenderer(widgets.RadioFieldRenderer):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   503
  """Used by PickQuantRadio to enable customization of radio widgets.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   504
  """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   505
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   506
  def __init__(self, *args, **kwargs):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   507
    super(PickQuantRadioRenderer, self).__init__(*args, **kwargs)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   508
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   509
  def render(self):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   510
    """Outputs set of radio fields in a div.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   511
    """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   512
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   513
    return mark_safe(u'<div class="quant_radio">\n%s\n</div>'
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   514
                     % u'\n'.join([u'%s' % force_unicode(w) for w in self]))
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   515
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   516
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   517
class PickQuantRadio(forms.RadioSelect):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   518
  """TODO(James,Ajaksu) Fix Docstring
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   519
  """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   520
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   521
  renderer = PickQuantRadioRenderer
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   522
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   523
  def __init__(self, *args, **kwargs):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   524
    super(PickQuantRadio, self).__init__(*args, **kwargs)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   525
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   526
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   527
# in the future, we'll have more widget types here
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   528
WIDGETS = {'multi_checkbox': PickManyCheckbox,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   529
           'single_select': PickOneSelect,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   530
           'quant_radio': PickQuantRadio}
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   531
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   532
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   533
class SurveyResults(widgets.Widget):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   534
  """Render List of Survey Results For Given Survey.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   535
  """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   536
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   537
  def render(self, survey, params, filter=filter, limit=1000, offset=0,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   538
             order=[], idx=0, context={}):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   539
    """ renders list of survey results
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   540
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   541
    params:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   542
      survey: current survey
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   543
      params: dict of params for rendering list
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   544
      filter: filter for list results
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   545
      limit: limit for list results
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   546
      offset: offset for list results
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   547
      order: order for list results
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   548
      idx: index for list results
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   549
      context: context dict for template
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   550
    """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   551
2463
05af53239799 Add more flexibility to the Survey helper for use with the new Survey types.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2451
diff changeset
   552
    survey_logic = params['logic']
05af53239799 Add more flexibility to the Survey helper for use with the new Survey types.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2451
diff changeset
   553
    record_logic = survey_logic.getRecordLogic()
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   554
    filter = {'survey': survey}
2463
05af53239799 Add more flexibility to the Survey helper for use with the new Survey types.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2451
diff changeset
   555
    data = record_logic.getForFields(filter=filter, limit=limit, offset=offset,
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   556
                              order=order)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   557
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   558
    params['name'] = "Survey Results"
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   559
    content = {
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   560
      'idx': idx,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   561
      'data': data,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   562
      'logic': logic,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   563
      'limit': limit,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   564
     }
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   565
    updates = dicts.rename(params, params['list_params'])
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   566
    content.update(updates)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   567
    contents = [content]
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   568
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   569
    if len(content) == 1:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   570
      content = content[0]
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   571
      key_order = content.get('key_order')
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   572
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   573
    context['list'] = Lists(contents)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   574
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   575
    # TODO(ajaksu) is this the best way to build the results list?
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   576
    for list_ in context['list']._contents:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   577
      if len(list_['data']) < 1:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   578
        return "<p>No Survey Results Have Been Submitted</p>"
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   579
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   580
      list_['row'] = 'soc/survey/list/results_row.html'
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   581
      list_['heading'] = 'soc/survey/list/results_heading.html'
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   582
      list_['description'] = 'Survey Results:'
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   583
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   584
    context['properties'] = survey.survey_content.orderedProperties()
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   585
    context['entity_type'] = "Survey Results"
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   586
    context['entity_type_plural'] = "Results"
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   587
    context['no_lists_msg'] = "No Survey Results"
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   588
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   589
    path = (survey.entity_type().lower(), survey.prefix,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   590
            survey.scope_path, survey.link_id)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   591
    context['grade_action'] = "/%s/grade/%s/%s/%s" % path
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   592
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   593
    markup = loader.render_to_string('soc/survey/results.html',
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   594
                                     dictionary=context).strip('\n')
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   595
    return markup
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   596
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   597
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   598
def getRoleSpecificFields(survey, user, this_project, survey_form,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   599
                          survey_record):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   600
  """For evaluations, mentors get required Project and Grade fields, and
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   601
  students get a required Project field.
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   602
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   603
  Because we need to get a list of the user's projects, we call the
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   604
  logic getProjects method, which doubles as an access check.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   605
  (No projects means that the survey cannot be taken.)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   606
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   607
  params:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   608
    survey: the survey being taken
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   609
    user: the survey-taking user
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   610
    this_project: either an already-selected project, or None
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   611
    survey_form: the surveyForm widget for this survey
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   612
    survey_record: an existing survey record for a user-project-survey combo,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   613
      or None
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   614
  """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   615
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   616
  field_count = len(eval(survey.survey_content.schema).items())
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   617
  these_projects = survey_logic.getProjects(survey, user)
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   618
  if not these_projects:
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   619
    return False # no projects found
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   620
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   621
  project_pairs = []
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   622
  #insert a select field with options for each project
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   623
  for project in these_projects:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   624
    project_pairs.append((project.key(), project.title))
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   625
  if project_pairs:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   626
    project_tuples = tuple(project_pairs)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   627
    # add select field containing list of projects
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   628
    projectField =  forms.fields.ChoiceField(
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   629
                              choices=project_tuples,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   630
                              required=True,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   631
                              widget=forms.Select())
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   632
    projectField.choices.insert(0, (None, "Choose a Project")  )
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   633
    # if editing an existing survey
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   634
    if not this_project and survey_record:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   635
      this_project = survey_record.project
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   636
    if this_project:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   637
      for tup in project_tuples:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   638
        if tup[1] == this_project.title:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   639
          if survey_record: project_name = tup[1] + " (Saved)"
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   640
          else: project_name = tup[1]
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   641
          projectField.choices.remove(tup)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   642
          projectField.choices.insert(0, (tup[0], project_name)  )
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   643
          break
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   644
    survey_form.fields.insert(0, 'project', projectField )
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   645
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   646
  if survey.taking_access == "mentor evaluation":
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   647
    # If this is a mentor, add a field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   648
    # determining if student passes or fails.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   649
    # Activate grades handler should determine whether new status
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   650
    # is midterm_passed, final_passed, etc.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   651
    grade_choices = (('pass', 'Pass'), ('fail', 'Fail'))
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   652
    grade_vals = { 'pass': True, 'fail': False }
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   653
    gradeField = forms.fields.ChoiceField(choices=grade_choices,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   654
                                           required=True,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   655
                                           widget=forms.Select())
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   656
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   657
    gradeField.choices.insert(0, (None, "Choose a Grade")  )
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   658
    if survey_record:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   659
      for g in grade_choices:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   660
        if grade_vals[g[0]] == survey_record.grade:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   661
          gradeField.choices.insert(0, (g[0],g[1] + " (Saved)")   )
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   662
          gradeField.choices.remove(g)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   663
          break;
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   664
      gradeField.show_hidden_initial = True
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   665
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   666
    survey_form.fields.insert(field_count + 1, 'grade', gradeField)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   667
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   668
  return survey_form