app/soc/views/helper/surveys.py
author Daniel Diniz <ajaksu@gmail.com>
Mon, 29 Jun 2009 19:29:15 +0200
changeset 2451 c58a7ea6c126
parent 2442 dd1f94c3594c
child 2463 05af53239799
permissions -rw-r--r--
Added SurveyContentSchema to improve readability for the survey helper. Reviewed by: Lennard de Rijk
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.logic.models.survey import logic as survey_logic
2439
7fac0da44bbf Put ResultsLogic in its own Logic module named survey_record.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2432
diff changeset
    44
from soc.logic.models.survey_record import logic as results_logic
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    45
from soc.models.survey import SurveyContent
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    46
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    47
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    48
class SurveyForm(djangoforms.ModelForm):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    49
  """Main SurveyContent form.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    50
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    51
  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
    52
    - Admin creating survey from scratch
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    53
    - Admin updating existing survey
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    54
    - User taking survey
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    55
    - User updating already taken survey
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    56
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    57
  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
    58
  survey form is dynamically formed.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    59
  """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    60
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    61
  def __init__(self, *args, **kwargs):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    62
    """Store special kwargs as attributes.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    63
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    64
      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
    65
      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
    66
    """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    67
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    68
    self.kwargs = kwargs
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
    69
    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
    70
    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
    71
    self.project = self.kwargs.pop('project', None)
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
    72
    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
    73
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
    74
    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
    75
    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
    76
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
    77
    self.fields_map = dict(
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
    78
        long_answer=self.addLongField,
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
    79
        short_answer=self.addShortField,
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
    80
        selection=self.addSingleField,
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
    81
        pick_multi=self.addMultiField,
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
    82
        pick_quant=self.addQuantField,
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
    83
        )
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    84
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    85
    super(SurveyForm, self).__init__(*args, **self.kwargs)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    86
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    87
  def getFields(self):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    88
    """Build the SurveyContent (questions) form fields.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    89
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    90
    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
    91
    """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    92
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    93
    if not self.survey_content:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    94
      return
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    95
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    96
    self.survey_fields = {}
2442
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
    97
    schema = SurveyContentSchema(self.survey_content.schema)
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    98
    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
    99
    extra_attrs = {}
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   100
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   101
    # 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
   102
    if not self.editing:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   103
      # only survey taking can be read-only
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   104
      read_only = self.read_only
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   105
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   106
      if not read_only:
2442
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
   107
        deadline = self.survey_content.survey_parent.get().survey_end
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   108
        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
   109
      else:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   110
        extra_attrs['disabled'] = 'disabled'
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   111
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   112
    # add unordered fields to self.survey_fields
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   113
    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
   114
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   115
      # a comment made by the user
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   116
      comment = ''
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   117
      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
   118
        # previously entered value
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   119
        value = getattr(self.survey_record, field)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   120
        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
   121
          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
   122
      else:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   123
        # use prompts set by survey creator
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   124
        value = getattr(self.survey_content, field)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   125
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   126
      label = schema.getLabel(field)
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   127
      if label is None:
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   128
        continue
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   129
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   130
      # 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
   131
      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
   132
      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
   133
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   134
    return self.insertFields()
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   135
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   136
  def insertFields(self):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   137
    """Add ordered fields to self.fields.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   138
    """
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
    survey_order = self.survey_content.getSurveyOrder()
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   141
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   142
    # first, insert dynamic survey fields
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   143
    for position, property in survey_order.items():
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   144
      position = position * 2
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   145
      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
   146
      if not self.editing:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   147
        property = 'comment_for_' + property
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   148
        self.fields.insert(position - 1, property,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   149
                           self.survey_fields[property])
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   150
    return self.fields
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   151
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   152
  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
   153
                   comment=''):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   154
    """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
   155
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   156
    params:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   157
      field: the current field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   158
      value: the initial value for this field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   159
      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
   160
      schema: schema for survey
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   161
      req: required bool
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   162
      label: label for field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   163
      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
   164
      comment: initial comment value for field
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   165
    """
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
    widget = widgets.Textarea(attrs=attrs)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   168
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   169
    if not tip:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   170
      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
   171
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   172
    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
   173
                         widget=widget, initial=value)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   174
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   175
    self.survey_fields[field] = question
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   176
    self.addCommentField(field, comment, attrs, tip)
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   177
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   178
  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
   179
                    comment=''):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   180
    """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
   181
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   182
    params:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   183
      field: the current field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   184
      value: the initial value for this field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   185
      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
   186
      schema: schema for survey
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   187
      req: required bool
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   188
      label: label for field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   189
      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
   190
      comment: initial comment value for field
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   191
    """
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
    attrs['class'] = "text_question"
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   194
    widget = widgets.TextInput(attrs=attrs)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   195
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   196
    if not tip:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   197
      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
   198
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   199
    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
   200
                         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
   201
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   202
    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
   203
    self.addCommentField(field, comment, attrs, tip)
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   204
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   205
  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
   206
                     tip='', comment=''):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   207
    """Add a selection field to this form.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   208
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   209
    params:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   210
      field: the current field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   211
      value: the initial value for this field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   212
      attrs: additional attributes for field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   213
      schema: schema for survey
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   214
      req: required bool
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   215
      label: label for field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   216
      tip: tooltip text for field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   217
      comment: initial comment value for field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   218
    """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   219
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   220
    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
   221
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   222
    these_choices = []
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   223
    # add all properties, but select chosen one
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   224
    options = getattr(self.survey_content, field)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   225
    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
   226
    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
   227
      these_choices.append((value, value))
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   228
      if value in options:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   229
        options.remove(value)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   230
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   231
    for option in options:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   232
      these_choices.append((option, option))
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   233
    if not tip:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   234
      tip = 'Please select an answer this question.'
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   235
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   236
    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
   237
                            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
   238
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   239
    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
   240
    self.addCommentField(field, comment, attrs, tip)
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   241
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   242
  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
   243
                    tip='', comment=''):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   244
    """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
   245
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   246
    params:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   247
      field: the current field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   248
      value: the initial value for this field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   249
      attrs: additional attributes for field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   250
      schema: schema for survey
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   251
      req: required bool
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   252
      label: label for field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   253
      tip: tooltip text for field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   254
      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
   255
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   256
    """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   257
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   258
    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
   259
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   260
    # 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
   261
    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
   262
      # 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
   263
      value = value.split(',')
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   264
    else:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   265
      value = None
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   266
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   267
    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
   268
    if not tip:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   269
      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
   270
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   271
    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
   272
                             choices=tuple(these_choices), widget=widget,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   273
                             initial=value)
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   274
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   275
    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
   276
    self.addCommentField(field, comment, attrs, tip)
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   277
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   278
  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
   279
                    tip='', comment=''):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   280
    """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
   281
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   282
    params:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   283
      field: the current field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   284
      value: the initial value for this field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   285
      attrs: additional attributes for field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   286
      schema: schema for survey
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   287
      req: required bool
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   288
      label: label for field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   289
      tip: tooltip text for field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   290
      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
   291
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   292
    """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   293
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   294
    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
   295
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   296
    if self.survey_record:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   297
      value = value
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   298
    else:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   299
      value = None
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   300
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   301
    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
   302
    if not tip:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   303
      tip = 'Please select one of these choices.'
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   304
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   305
    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
   306
                             choices=tuple(these_choices), widget=widget,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   307
                             initial=value)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   308
    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
   309
    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
   310
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   311
  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
   312
    if not self.editing:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   313
      widget = widgets.Textarea(attrs=attrs)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   314
      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
   315
                          widget=widget, initial=comment)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   316
      self.survey_fields['comment_for_' + field] = comment
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   317
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   318
  class Meta(object):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   319
    model = SurveyContent
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   320
    exclude = ['schema']
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   321
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   322
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   323
class SurveyContentSchema(object):
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   324
  """Abstract question metadata handling.
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   325
  """
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
  def __init__(self, schema):
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   328
    self.schema = eval(schema)
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   329
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   330
  def getType(self, field):
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   331
    return self.schema[field]["type"]
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   332
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   333
  def getRender(self, field):
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   334
    return self.schema[field]["render"]
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   335
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   336
  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
   337
    """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
   338
    """
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
    if editing:
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   341
      kind = self.getType(field)
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   342
      render = self.getRender(field)
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   343
      widget = UniversalChoiceEditor(kind, render)
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   344
    else:
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   345
      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
   346
    return widget
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   347
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   348
  def getLabel(self, field):
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   349
    """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
   350
    """
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
    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
   353
      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
   354
                    (field, str(self.schema)))
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   355
      return
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   356
    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
   357
      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
   358
    else:
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   359
      label = field
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   360
    return label
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   361
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   362
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   363
class UniversalChoiceEditor(widgets.Widget):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   364
  """Edit interface for choice questions.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   365
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   366
  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
   367
  """
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
  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
   370
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   371
    self.attrs = attrs or {}
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   372
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   373
    # 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
   374
    # 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
   375
    # more than once.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   376
    self.choices = list(choices)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   377
    self.kind = kind
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   378
    self.render_as = render
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   379
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   380
  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
   381
    """ renders UCE widget
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   382
    """
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
    if value is None:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   385
      value = ''
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   386
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   387
    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
   388
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   389
    # 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
   390
    selected = 'selected="selected"'
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   391
    context =  dict(
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   392
        name=name,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   393
        is_selection=selected * (self.kind == 'selection'),
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   394
        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
   395
        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
   396
        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
   397
        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
   398
        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
   399
        )
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
    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
   402
    chained_choices = enumerate(chain(self.choices, choices))
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   403
    choices = {}
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   404
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   405
    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
   406
      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
   407
      choices[i] = option_value
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   408
    context['choices'] = choices
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   409
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   410
    template = 'soc/survey/universal_choice_editor.html'
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   411
    return loader.render_to_string(template, context)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   412
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
class PickOneField(forms.ChoiceField):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   415
  """Stub for customizing the single choice field.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   416
  """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   417
  #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
   418
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   419
  def __init__(self, *args, **kwargs):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   420
    super(PickOneField, self).__init__(*args, **kwargs)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   421
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
class PickManyField(forms.MultipleChoiceField):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   424
  """Stub for customizing the multiple choice field.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   425
  """
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
  def __init__(self, *args, **kwargs):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   428
    super(PickManyField, self).__init__(*args, **kwargs)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   429
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
class PickQuantField(forms.MultipleChoiceField):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   432
  """Stub for customizing the multiple choice field.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   433
  """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   434
  #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
   435
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   436
  def __init__(self, *args, **kwargs):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   437
    super(PickQuantField, self).__init__(*args, **kwargs)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   438
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
class PickOneSelect(forms.Select):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   441
  """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
   442
  """
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
  def __init__(self, *args, **kwargs):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   445
    super(PickOneSelect, self).__init__(*args, **kwargs)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   446
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
class PickManyCheckbox(forms.CheckboxSelectMultiple):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   449
  """Customized multiple choice checkbox widget.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   450
  """
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
  def __init__(self, *args, **kwargs):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   453
    super(PickManyCheckbox, self).__init__(*args, **kwargs)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   454
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   455
  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
   456
    """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
   457
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   458
    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
   459
    """
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
    if value is None:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   462
      value = []
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   463
    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
   464
    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
   465
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   466
    # normalize to strings.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   467
    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
   468
    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
   469
    smart_unicode = forms.util.smart_unicode
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   470
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   471
    # set container fieldset and list
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   472
    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
   473
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   474
    # add numbered checkboxes wrapped in list items
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   475
    chained_choices = enumerate(chain(self.choices, choices))
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   476
    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
   477
      option_label = escape(smart_unicode(option_label))
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   478
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   479
      # 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
   480
      # 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
   481
      if has_id:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   482
        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
   483
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   484
      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
   485
      rendered_cb = cb.render(name, option_value)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   486
      cb_label = (rendered_cb, option_label)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   487
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   488
      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
   489
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   490
    output.append(u'  </ul>\n</fieldset>')
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   491
    return u'\n'.join(output)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   492
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   493
  def id_for_label(self, id_):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   494
    # 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
   495
    if id_:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   496
      id_ += '_fieldset'
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   497
    return id_
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   498
  id_for_label = classmethod(id_for_label)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   499
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
class PickQuantRadioRenderer(widgets.RadioFieldRenderer):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   502
  """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
   503
  """
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
  def __init__(self, *args, **kwargs):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   506
    super(PickQuantRadioRenderer, self).__init__(*args, **kwargs)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   507
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   508
  def render(self):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   509
    """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
   510
    """
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
    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
   513
                     % 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
   514
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
class PickQuantRadio(forms.RadioSelect):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   517
  """TODO(James,Ajaksu) Fix Docstring
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   518
  """
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
  renderer = PickQuantRadioRenderer
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   521
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   522
  def __init__(self, *args, **kwargs):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   523
    super(PickQuantRadio, self).__init__(*args, **kwargs)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   524
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
# 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
   527
WIDGETS = {'multi_checkbox': PickManyCheckbox,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   528
           'single_select': PickOneSelect,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   529
           'quant_radio': PickQuantRadio}
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   530
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
class SurveyResults(widgets.Widget):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   533
  """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
   534
  """
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
  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
   537
             order=[], idx=0, context={}):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   538
    """ renders list of survey results
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   539
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   540
    params:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   541
      survey: current survey
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   542
      params: dict of params for rendering list
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   543
      filter: filter for list results
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   544
      limit: limit for list results
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   545
      offset: offset for list results
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   546
      order: order for list results
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   547
      idx: index for list results
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   548
      context: context dict for template
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   549
    """
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
    logic = results_logic
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   552
    filter = {'survey': survey}
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   553
    data = logic.getForFields(filter=filter, limit=limit, offset=offset,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   554
                              order=order)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   555
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   556
    params['name'] = "Survey Results"
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   557
    content = {
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   558
      'idx': idx,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   559
      'data': data,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   560
      'logic': logic,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   561
      'limit': limit,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   562
     }
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   563
    updates = dicts.rename(params, params['list_params'])
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   564
    content.update(updates)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   565
    contents = [content]
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   566
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   567
    if len(content) == 1:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   568
      content = content[0]
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   569
      key_order = content.get('key_order')
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   570
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   571
    context['list'] = Lists(contents)
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
    # 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
   574
    for list_ in context['list']._contents:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   575
      if len(list_['data']) < 1:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   576
        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
   577
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   578
      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
   579
      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
   580
      list_['description'] = 'Survey Results:'
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   581
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   582
    context['properties'] = survey.survey_content.orderedProperties()
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   583
    context['entity_type'] = "Survey Results"
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   584
    context['entity_type_plural'] = "Results"
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   585
    context['no_lists_msg'] = "No Survey Results"
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   586
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   587
    path = (survey.entity_type().lower(), survey.prefix,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   588
            survey.scope_path, survey.link_id)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   589
    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
   590
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   591
    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
   592
                                     dictionary=context).strip('\n')
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   593
    return markup
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   594
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   595
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   596
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
   597
                          survey_record):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   598
  """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
   599
  students get a required Project field.
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   600
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   601
  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
   602
  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
   603
  (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
   604
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   605
  params:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   606
    survey: the survey being taken
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   607
    user: the survey-taking user
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   608
    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
   609
    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
   610
    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
   611
      or None
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   612
  """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   613
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   614
  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
   615
  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
   616
  if not these_projects:
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   617
    return False # no projects found
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   618
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   619
  project_pairs = []
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   620
  #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
   621
  for project in these_projects:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   622
    project_pairs.append((project.key(), project.title))
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   623
  if project_pairs:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   624
    project_tuples = tuple(project_pairs)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   625
    # add select field containing list of projects
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   626
    projectField =  forms.fields.ChoiceField(
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   627
                              choices=project_tuples,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   628
                              required=True,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   629
                              widget=forms.Select())
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   630
    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
   631
    # if editing an existing survey
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   632
    if not this_project and survey_record:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   633
      this_project = survey_record.project
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   634
    if this_project:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   635
      for tup in project_tuples:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   636
        if tup[1] == this_project.title:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   637
          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
   638
          else: project_name = tup[1]
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   639
          projectField.choices.remove(tup)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   640
          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
   641
          break
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   642
    survey_form.fields.insert(0, 'project', projectField )
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   643
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   644
  if survey.taking_access == "mentor evaluation":
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   645
    # 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
   646
    # determining if student passes or fails.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   647
    # 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
   648
    # is midterm_passed, final_passed, etc.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   649
    grade_choices = (('pass', 'Pass'), ('fail', 'Fail'))
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   650
    grade_vals = { 'pass': True, 'fail': False }
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   651
    gradeField = forms.fields.ChoiceField(choices=grade_choices,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   652
                                           required=True,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   653
                                           widget=forms.Select())
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   654
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   655
    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
   656
    if survey_record:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   657
      for g in grade_choices:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   658
        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
   659
          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
   660
          gradeField.choices.remove(g)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   661
          break;
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   662
      gradeField.show_hidden_initial = True
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   663
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   664
    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
   665
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   666
  return survey_form