app/soc/views/helper/surveys.py
author Lennard de Rijk <ljvderijk@gmail.com>
Fri, 03 Jul 2009 15:00:00 +0200
changeset 2506 c98bf890156e
parent 2502 2e096acc8720
child 2507 4a6b8d6674b6
permissions -rw-r--r--
Removed getSurveyResponseFromPost from Surveys helper. We will use the power of Django Forms instead of getting our own properties. This fits better into the rest of Melange which also uses this approach.
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
2478
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
    28
import csv
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    29
import datetime
2442
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
    30
import logging
2478
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
    31
import StringIO
2432
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 google.appengine.ext.db import djangoforms
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    34
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    35
from django import forms
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    36
from django.forms import widgets
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    37
from django.forms.fields import CharField
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    38
from django.template import loader
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    39
from django.utils.encoding import force_unicode
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    40
from django.utils.html import escape
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    41
from django.utils.safestring import mark_safe
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    42
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    43
from soc.logic import dicts
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    44
from soc.logic.lists import Lists
2501
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
    45
from soc.models.survey import COMMENT_PREFIX
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    46
from soc.models.survey import SurveyContent
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
2502
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
    49
# TODO(ajaksu) add this to template
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
    50
REQUIRED_COMMENT_TPL = """
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
    51
  <label for="required_for_{{ name }}">Required</label>
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
    52
  <select id="required_for_{{ name }}" name="required_for_{{ name }}">
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
    53
    <option value="True" {% if is_required %} selected='selected' {% endif %}
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
    54
     >True</option>
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
    55
    <option value="False" {% if not is_required %} selected='selected'
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
    56
     {% endif %}>False</option>
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
    57
  </select><br/>
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
    58
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
    59
  <label for="comment_for_{{ name }}">Allow Comments</label>
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
    60
  <select id="comment_for_{{ name }}" name="comment_for_{{ name }}">
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
    61
    <option value="True" {% if has_comment %} selected='selected' {% endif %}
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
    62
     >True</option>
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
    63
    <option value="False" {% if not has_comment %} selected='selected'
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
    64
     {% endif %}>False</option>
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
    65
  </select><br/>
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
    66
"""
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
    67
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
    68
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    69
class SurveyForm(djangoforms.ModelForm):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    70
  """Main SurveyContent form.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    71
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    72
  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
    73
    - Admin creating survey from scratch
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    74
    - Admin updating existing survey
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    75
    - User taking survey
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    76
    - User updating already taken survey
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    77
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    78
  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
    79
  survey form is dynamically formed.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    80
  """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    81
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    82
  def __init__(self, *args, **kwargs):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    83
    """Store special kwargs as attributes.
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
      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
    86
      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
    87
    """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    88
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    89
    self.kwargs = kwargs
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
    90
    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
    91
    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
    92
    self.project = self.kwargs.pop('project', None)
2463
05af53239799 Add more flexibility to the Survey helper for use with the new Survey types.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2451
diff changeset
    93
    self.survey_logic = self.kwargs.pop('survey_logic', None)
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
    94
    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
    95
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
    96
    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
    97
    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
    98
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
    99
    self.fields_map = dict(
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   100
        long_answer=self.addLongField,
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   101
        short_answer=self.addShortField,
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   102
        selection=self.addSingleField,
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   103
        pick_multi=self.addMultiField,
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   104
        pick_quant=self.addQuantField,
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   105
        )
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   106
2501
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   107
    self.kwargs['data'] = {}
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   108
    super(SurveyForm, self).__init__(*args, **self.kwargs)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   109
2501
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   110
  def getFields(self, post_dict=None):
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   111
    """Build the SurveyContent (questions) form fields.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   112
2501
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   113
    params:
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   114
      post_dict: dict with POST data that will be used for validation
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   115
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   116
    Populates self.survey_fields, which will be ordered in self.insert_fields.
2501
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   117
    Also populates self.data, which will be used in form validation.
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   118
    """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   119
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   120
    if not self.survey_content:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   121
      return
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   122
2501
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   123
    post_dict = post_dict or {}
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   124
    self.survey_fields = {}
2442
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
   125
    schema = SurveyContentSchema(self.survey_content.schema)
2501
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   126
    has_record = (not self.editing) and (self.survey_record or post_dict)
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   127
    extra_attrs = {}
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   128
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   129
    # 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
   130
    if not self.editing:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   131
      # only survey taking can be read-only
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   132
      read_only = self.read_only
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
      if not read_only:
2463
05af53239799 Add more flexibility to the Survey helper for use with the new Survey types.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2451
diff changeset
   135
        survey_content = self.survey_content
05af53239799 Add more flexibility to the Survey helper for use with the new Survey types.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2451
diff changeset
   136
        survey_entity = self.survey_logic.getSurveyForContent(survey_content)
05af53239799 Add more flexibility to the Survey helper for use with the new Survey types.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2451
diff changeset
   137
        deadline = survey_entity.survey_end
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   138
        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
   139
      else:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   140
        extra_attrs['disabled'] = 'disabled'
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
    # add unordered fields to self.survey_fields
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   143
    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
   144
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   145
      # a comment made by the user
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   146
      comment = ''
2501
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   147
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   148
      # flag to know where the value came from
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   149
      from_content = False
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   150
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   151
      if has_record and field in post_dict:
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   152
        # entered value that is not yet saved
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   153
        value = post_dict[field]
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   154
        if COMMENT_PREFIX + field in post_dict:
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   155
          comment = post_dict[COMMENT_PREFIX + field]
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   156
      elif has_record and hasattr(self.survey_record, field):
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   157
        # previously entered value
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   158
        value = getattr(self.survey_record, field)
2501
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   159
        if hasattr(self.survey_record, COMMENT_PREFIX + field):
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   160
          comment = getattr(self.survey_record, COMMENT_PREFIX + field)
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   161
      else:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   162
        # use prompts set by survey creator
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   163
        value = getattr(self.survey_content, field)
2501
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   164
        from_content = True
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   165
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   166
      label = schema.getLabel(field)
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   167
      if label is None:
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   168
        continue
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   169
2501
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   170
      # fix validation for pick_multi fields
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   171
      is_multi = schema.getType(field) == 'pick_multi'
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   172
      if not from_content and schema.getType(field) == 'pick_multi':
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   173
        if isinstance(value, basestring):
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   174
          value = value.split(',')
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   175
      elif from_content and is_multi:
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   176
        value = []
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   177
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   178
      # record field value for validation
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   179
      if not from_content:
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   180
        self.data[field] = value
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   181
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   182
      # find correct field type
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   183
      addField = self.fields_map[schema.getType(field)]
2502
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   184
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   185
      # check if question is required, it's never required when editing
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   186
      required = not self.editing and schema.getRequired(field)
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   187
      kwargs = dict(label=label, req=required)
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   188
2502
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   189
      # add new field
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   190
      addField(field, value, extra_attrs, schema, **kwargs)
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   191
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   192
      # handle comments if question allows them
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   193
      if schema.getHasComment(field):
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   194
        self.data[COMMENT_PREFIX + field] = comment
2501
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   195
        self.addCommentField(field, comment, extra_attrs, tip='Add a comment.')
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   196
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   197
    return self.insertFields()
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
  def insertFields(self):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   200
    """Add ordered fields to self.fields.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   201
    """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   202
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   203
    survey_order = self.survey_content.getSurveyOrder()
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
    # first, insert dynamic survey fields
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   206
    for position, property in survey_order.items():
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   207
      position = position * 2
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   208
      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
   209
      if not self.editing:
2501
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   210
        # add comment if field has one and this isn't an edit view
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   211
        property = COMMENT_PREFIX + property
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   212
        if property in self.survey_fields:
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   213
          self.fields.insert(position - 1, property,
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   214
                             self.survey_fields[property])
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   215
    return self.fields
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   216
2502
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   217
  def addLongField(self, field, value, attrs, schema, req=True, label='',
2470
6c3b7dd8b770 Style fix in Survey views helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2468
diff changeset
   218
                   tip='', comment=''):
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   219
    """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
   220
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   221
    params:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   222
      field: the current field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   223
      value: the initial value for this field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   224
      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
   225
      schema: schema for survey
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   226
      req: required bool
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   227
      label: label for field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   228
      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
   229
      comment: initial comment value for field
2432
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
2502
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   232
    # use a widget that allows setting required and comments
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   233
    has_comment = schema.getHasComment(field)
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   234
    is_required = schema.getRequired(field)
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   235
    widget = LongTextarea(is_required, has_comment, attrs=attrs,
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   236
                          editing=self.editing)
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   237
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   238
    if not tip:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   239
      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
   240
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   241
    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
   242
                         widget=widget, initial=value)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   243
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   244
    self.survey_fields[field] = question
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   245
2468
0273c1a8d708 Fixed too long line in Surveys helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2465
diff changeset
   246
  def addShortField(self, field, value, attrs, schema, req=False, label='',
0273c1a8d708 Fixed too long line in Surveys helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2465
diff changeset
   247
                    tip='', comment=''):
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   248
    """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
   249
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   250
    params:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   251
      field: the current field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   252
      value: the initial value for this field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   253
      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
   254
      schema: schema for survey
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   255
      req: required bool
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   256
      label: label for field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   257
      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
   258
      comment: initial comment value for field
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
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   261
    attrs['class'] = "text_question"
2502
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   262
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   263
    # use a widget that allows setting required and comments
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   264
    has_comment = schema.getHasComment(field)
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   265
    is_required = schema.getRequired(field)
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   266
    widget = ShortTextInput(is_required, has_comment, attrs=attrs,
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   267
                          editing=self.editing)
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   268
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   269
    if not tip:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   270
      tip = 'Please provide a short answer to this question.'
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   271
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   272
    question = 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
   273
                         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
   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
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   276
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   277
  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
   278
                     tip='', comment=''):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   279
    """Add a selection field to this form.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   280
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   281
    params:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   282
      field: the current field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   283
      value: the initial value for this field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   284
      attrs: additional attributes for field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   285
      schema: schema for survey
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   286
      req: required bool
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   287
      label: label for field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   288
      tip: tooltip text for field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   289
      comment: initial comment value for field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   290
    """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   291
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   292
    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
   293
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   294
    these_choices = []
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   295
    # add all properties, but select chosen one
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   296
    options = getattr(self.survey_content, field)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   297
    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
   298
    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
   299
      these_choices.append((value, value))
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   300
      if value in options:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   301
        options.remove(value)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   302
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   303
    for option in options:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   304
      these_choices.append((option, option))
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   305
    if not tip:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   306
      tip = 'Please select an answer this question.'
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   307
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   308
    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
   309
                            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
   310
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   311
    self.survey_fields[field] = question
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   312
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   313
  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
   314
                    tip='', comment=''):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   315
    """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
   316
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   317
    params:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   318
      field: the current field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   319
      value: the initial value for this field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   320
      attrs: additional attributes for field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   321
      schema: schema for survey
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   322
      req: required bool
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   323
      label: label for field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   324
      tip: tooltip text for field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   325
      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
   326
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   327
    """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   328
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   329
    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
   330
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   331
    # 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
   332
    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
   333
      # 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
   334
      value = value.split(',')
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   335
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   336
    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
   337
    if not tip:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   338
      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
   339
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   340
    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
   341
                             choices=tuple(these_choices), widget=widget,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   342
                             initial=value)
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   343
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   344
    self.survey_fields[field] = question
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   345
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   346
  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
   347
                    tip='', comment=''):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   348
    """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
   349
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   350
    params:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   351
      field: the current field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   352
      value: the initial value for this field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   353
      attrs: additional attributes for field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   354
      schema: schema for survey
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   355
      req: required bool
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   356
      label: label for field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   357
      tip: tooltip text for field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   358
      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
   359
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   360
    """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   361
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   362
    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
   363
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   364
    if self.survey_record:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   365
      value = value
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   366
    else:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   367
      value = None
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
    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
   370
    if not tip:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   371
      tip = 'Please select one of these choices.'
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
    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
   374
                             choices=tuple(these_choices), widget=widget,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   375
                             initial=value)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   376
    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
   377
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   378
  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
   379
    if not self.editing:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   380
      widget = widgets.Textarea(attrs=attrs)
2501
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   381
      comment_field = CharField(help_text=tip, required=False, label='Comments',
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   382
                          widget=widget, initial=comment)
2501
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   383
      self.survey_fields[COMMENT_PREFIX + field] = comment_field
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   384
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   385
  class Meta(object):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   386
    model = SurveyContent
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   387
    exclude = ['schema']
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
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   390
class SurveyContentSchema(object):
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   391
  """Abstract question metadata handling.
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   392
  """
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   393
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   394
  def __init__(self, schema):
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   395
    self.schema = eval(schema)
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   396
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   397
  def getType(self, field):
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   398
    return self.schema[field]["type"]
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   399
2502
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   400
  def getRequired(self, field):
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   401
    """Check whether survey question is required.
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   402
    """
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   403
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   404
    return self.schema[field]["required"]
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   405
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   406
  def getHasComment(self, field):
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   407
    """Check whether survey question allows adding a comment.
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   408
    """
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   409
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   410
    return self.schema[field]["has_comment"]
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   411
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   412
  def getRender(self, field):
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   413
    return self.schema[field]["render"]
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   414
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   415
  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
   416
    """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
   417
    """
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   418
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   419
    if editing:
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   420
      kind = self.getType(field)
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   421
      render = self.getRender(field)
2502
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   422
      is_required = self.getRequired(field)
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   423
      has_comment = self.getHasComment(field)
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   424
      widget = UniversalChoiceEditor(kind, render, is_required, has_comment)
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   425
    else:
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   426
      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
   427
    return widget
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   428
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   429
  def getLabel(self, field):
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   430
    """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
   431
    """
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   432
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   433
    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
   434
      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
   435
                    (field, str(self.schema)))
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   436
      return
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   437
    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
   438
      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
   439
    else:
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   440
      label = field
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   441
    return label
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   442
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   443
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   444
class UniversalChoiceEditor(widgets.Widget):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   445
  """Edit interface for choice questions.
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
  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
   448
  """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   449
2502
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   450
  def __init__(self, kind, render, is_required, has_comment, attrs=None,
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   451
               choices=()):
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   452
    """
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   453
    params:
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   454
      kind: question kind (one of selection, pick_multi or pick_quant)
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   455
      render: question widget (single_select, multi_checkbox or quant_radio)
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   456
      is_required: bool, controls selection in the required_for field
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   457
      has_comments: bool, controls selection in the has_comments field
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   458
    """
2432
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
    self.attrs = attrs or {}
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   461
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   462
    # 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
   463
    # 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
   464
    # more than once.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   465
    self.choices = list(choices)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   466
    self.kind = kind
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   467
    self.render_as = render
2502
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   468
    self.is_required = is_required
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   469
    self.has_comment = has_comment
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   470
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   471
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   472
  def render(self, name, value, attrs=None, choices=()):
2502
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   473
    """Render UCE widget.
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   474
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   475
    Option reordering, editing, addition and deletion are added here.
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   476
    """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   477
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   478
    if value is None:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   479
      value = ''
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   480
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   481
    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
   482
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   483
    # 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
   484
    selected = 'selected="selected"'
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   485
    context =  dict(
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   486
        name=name,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   487
        is_selection=selected * (self.kind == 'selection'),
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   488
        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
   489
        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
   490
        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
   491
        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
   492
        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
   493
        )
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   494
2502
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   495
    # set required and has_comment selects
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   496
    context.update(dict(
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   497
        is_required = self.is_required,
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   498
        has_comment = self.has_comment,
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   499
        ))
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   500
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   501
    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
   502
    chained_choices = enumerate(chain(self.choices, choices))
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   503
    choices = {}
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
    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
   506
      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
   507
      choices[i] = option_value
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   508
    context['choices'] = choices
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   509
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   510
    template = 'soc/survey/universal_choice_editor.html'
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   511
    return loader.render_to_string(template, context)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   512
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   513
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   514
class PickOneField(forms.ChoiceField):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   515
  """Stub for customizing the single choice field.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   516
  """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   517
  #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
   518
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   519
  def __init__(self, *args, **kwargs):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   520
    super(PickOneField, self).__init__(*args, **kwargs)
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
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   523
class PickManyField(forms.MultipleChoiceField):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   524
  """Stub for customizing the multiple choice field.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   525
  """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   526
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   527
  def __init__(self, *args, **kwargs):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   528
    super(PickManyField, self).__init__(*args, **kwargs)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   529
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
class PickQuantField(forms.MultipleChoiceField):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   532
  """Stub for customizing the multiple choice field.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   533
  """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   534
  #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
   535
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   536
  def __init__(self, *args, **kwargs):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   537
    super(PickQuantField, self).__init__(*args, **kwargs)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   538
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   539
2502
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   540
class LongTextarea(widgets.Textarea):
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   541
  """Set whether long question is required or allows comments.
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   542
  """
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   543
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   544
  def __init__(self, is_required, has_comment, attrs=None, editing=False):
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   545
    """Initialize widget and store editing mode.
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   546
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   547
    params:
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   548
      is_required: bool, controls selection in the 'required' extra field
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   549
      has_comments: bool, controls selection in the 'has_comment' extra field
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   550
      editing: bool, controls rendering as plain textarea or with extra fields
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   551
    """
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   552
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   553
    self.editing = editing
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   554
    self.is_required = is_required
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   555
    self.has_comment = has_comment
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   556
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   557
    super(LongTextarea, self).__init__(attrs)
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   558
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   559
  def render(self, name, value, attrs=None):
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   560
    """Render plain textarea or widget with extra fields.
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   561
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   562
    Extra fields are 'required' and 'has_comment'.
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   563
    """
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   564
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   565
    # plain text area
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   566
    output = super(LongTextarea, self).render(name, value, attrs)
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   567
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   568
    if self.editing:
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   569
      # add 'required' and 'has_comment' fields
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   570
      context = dict(name=name, is_required=self.is_required,
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   571
                     has_comment=self.has_comment)
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   572
      template = loader.get_template_from_string(REQUIRED_COMMENT_TPL)
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   573
      rendered = template.render(context=loader.Context(dict_=context))
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   574
      output =  rendered + output
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   575
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   576
      output = '<fieldset>' + output + '</fieldset>'
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   577
    return output
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   578
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   579
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   580
class ShortTextInput(widgets.TextInput):
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   581
  """Set whether short answer question is required or allows comments.
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   582
  """
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   583
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   584
  def __init__(self, is_required, has_comment, attrs=None, editing=False):
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   585
    """Initialize widget and store editing mode.
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   586
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   587
    params:
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   588
      is_required: bool, controls selection in the 'required' extra field
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   589
      has_comments: bool, controls selection in the 'has_comment' extra field
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   590
      editing: bool, controls rendering as plain text input or with extra fields
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   591
    """
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   592
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   593
    self.editing = editing
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   594
    self.is_required = is_required
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   595
    self.has_comment = has_comment
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   596
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   597
    super(ShortTextInput, self).__init__(attrs)
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   598
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   599
  def render(self, name, value, attrs=None):
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   600
    """Render plain text input or widget with extra fields.
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   601
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   602
    Extra fields are 'required' and 'has_comment'.
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   603
    """
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   604
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   605
    # plain text area
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   606
    output = super(ShortTextInput, self).render(name, value, attrs)
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   607
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   608
    if self.editing:
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   609
      # add 'required' and 'has_comment' fields
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   610
      context = dict(name=name, is_required=self.is_required,
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   611
                     has_comment=self.has_comment)
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   612
      template = loader.get_template_from_string(REQUIRED_COMMENT_TPL)
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   613
      rendered = template.render(context=loader.Context(dict_=context))
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   614
      output =  rendered + output
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   615
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   616
      output = '<fieldset>' + output + '</fieldset>'
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   617
    return output
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   618
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   619
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   620
class PickOneSelect(forms.Select):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   621
  """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
   622
  """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   623
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   624
  def __init__(self, *args, **kwargs):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   625
    super(PickOneSelect, self).__init__(*args, **kwargs)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   626
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   627
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   628
class PickManyCheckbox(forms.CheckboxSelectMultiple):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   629
  """Customized multiple choice checkbox widget.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   630
  """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   631
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   632
  def __init__(self, *args, **kwargs):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   633
    super(PickManyCheckbox, self).__init__(*args, **kwargs)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   634
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   635
  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
   636
    """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
   637
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   638
    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
   639
    """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   640
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   641
    if value is None:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   642
      value = []
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   643
    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
   644
    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
   645
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   646
    # normalize to strings.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   647
    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
   648
    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
   649
    smart_unicode = forms.util.smart_unicode
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   650
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   651
    # set container fieldset and list
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   652
    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
   653
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   654
    # add numbered checkboxes wrapped in list items
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   655
    chained_choices = enumerate(chain(self.choices, choices))
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   656
    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
   657
      option_label = escape(smart_unicode(option_label))
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   658
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   659
      # 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
   660
      # 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
   661
      if has_id:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   662
        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
   663
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   664
      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
   665
      rendered_cb = cb.render(name, option_value)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   666
      cb_label = (rendered_cb, option_label)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   667
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   668
      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
   669
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   670
    output.append(u'  </ul>\n</fieldset>')
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   671
    return u'\n'.join(output)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   672
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   673
  def id_for_label(self, id_):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   674
    # 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
   675
    if id_:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   676
      id_ += '_fieldset'
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   677
    return id_
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   678
  id_for_label = classmethod(id_for_label)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   679
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   680
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   681
class PickQuantRadioRenderer(widgets.RadioFieldRenderer):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   682
  """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
   683
  """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   684
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   685
  def __init__(self, *args, **kwargs):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   686
    super(PickQuantRadioRenderer, self).__init__(*args, **kwargs)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   687
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   688
  def render(self):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   689
    """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
   690
    """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   691
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   692
    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
   693
                     % 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
   694
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   695
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   696
class PickQuantRadio(forms.RadioSelect):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   697
  """TODO(James,Ajaksu) Fix Docstring
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   698
  """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   699
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   700
  renderer = PickQuantRadioRenderer
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   701
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   702
  def __init__(self, *args, **kwargs):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   703
    super(PickQuantRadio, self).__init__(*args, **kwargs)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   704
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   705
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   706
# 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
   707
WIDGETS = {'multi_checkbox': PickManyCheckbox,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   708
           'single_select': PickOneSelect,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   709
           'quant_radio': PickQuantRadio}
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   710
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   711
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   712
class SurveyResults(widgets.Widget):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   713
  """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
   714
  """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   715
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   716
  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
   717
             order=[], idx=0, context={}):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   718
    """ renders list of survey results
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   719
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   720
    params:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   721
      survey: current survey
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   722
      params: dict of params for rendering list
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   723
      filter: filter for list results
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   724
      limit: limit for list results
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   725
      offset: offset for list results
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   726
      order: order for list results
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   727
      idx: index for list results
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   728
      context: context dict for template
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   729
    """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   730
2463
05af53239799 Add more flexibility to the Survey helper for use with the new Survey types.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2451
diff changeset
   731
    survey_logic = params['logic']
05af53239799 Add more flexibility to the Survey helper for use with the new Survey types.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2451
diff changeset
   732
    record_logic = survey_logic.getRecordLogic()
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   733
    filter = {'survey': survey}
2463
05af53239799 Add more flexibility to the Survey helper for use with the new Survey types.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2451
diff changeset
   734
    data = record_logic.getForFields(filter=filter, limit=limit, offset=offset,
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   735
                              order=order)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   736
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   737
    params['name'] = "Survey Results"
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   738
    content = {
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   739
      'idx': idx,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   740
      'data': data,
2465
037a7e79a635 Use an existing logic for showing Survey Results.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2463
diff changeset
   741
      'logic': record_logic,
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   742
      'limit': limit,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   743
     }
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   744
    updates = dicts.rename(params, params['list_params'])
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   745
    content.update(updates)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   746
    contents = [content]
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   747
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   748
    if len(content) == 1:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   749
      content = content[0]
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   750
      key_order = content.get('key_order')
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   751
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   752
    context['list'] = Lists(contents)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   753
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   754
    # 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
   755
    for list_ in context['list']._contents:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   756
      if len(list_['data']) < 1:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   757
        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
   758
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   759
      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
   760
      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
   761
      list_['description'] = 'Survey Results:'
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   762
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   763
    context['properties'] = survey.survey_content.orderedProperties()
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   764
    context['entity_type'] = "Survey Results"
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   765
    context['entity_type_plural'] = "Results"
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   766
    context['no_lists_msg'] = "No Survey Results"
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   767
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   768
    path = (survey.entity_type().lower(), survey.prefix,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   769
            survey.scope_path, survey.link_id)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   770
    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
   771
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   772
    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
   773
                                     dictionary=context).strip('\n')
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   774
    return markup
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   775
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   776
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   777
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
   778
                          survey_record):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   779
  """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
   780
  students get a required Project field.
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   781
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   782
  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
   783
  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
   784
  (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
   785
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   786
  params:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   787
    survey: the survey being taken
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   788
    user: the survey-taking user
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   789
    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
   790
    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
   791
    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
   792
      or None
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   793
  """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   794
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   795
  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
   796
  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
   797
  if not these_projects:
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   798
    return False # no projects found
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   799
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   800
  project_pairs = []
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   801
  #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
   802
  for project in these_projects:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   803
    project_pairs.append((project.key(), project.title))
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   804
  if project_pairs:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   805
    project_tuples = tuple(project_pairs)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   806
    # add select field containing list of projects
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   807
    projectField =  forms.fields.ChoiceField(
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   808
                              choices=project_tuples,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   809
                              required=True,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   810
                              widget=forms.Select())
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   811
    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
   812
    # if editing an existing survey
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   813
    if not this_project and survey_record:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   814
      this_project = survey_record.project
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   815
    if this_project:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   816
      for tup in project_tuples:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   817
        if tup[1] == this_project.title:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   818
          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
   819
          else: project_name = tup[1]
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   820
          projectField.choices.remove(tup)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   821
          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
   822
          break
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   823
    survey_form.fields.insert(0, 'project', projectField )
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   824
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   825
  if survey.taking_access == "mentor evaluation":
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   826
    # 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
   827
    # determining if student passes or fails.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   828
    # 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
   829
    # is midterm_passed, final_passed, etc.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   830
    grade_choices = (('pass', 'Pass'), ('fail', 'Fail'))
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   831
    grade_vals = { 'pass': True, 'fail': False }
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   832
    gradeField = forms.fields.ChoiceField(choices=grade_choices,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   833
                                           required=True,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   834
                                           widget=forms.Select())
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   835
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   836
    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
   837
    if survey_record:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   838
      for g in grade_choices:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   839
        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
   840
          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
   841
          gradeField.choices.remove(g)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   842
          break;
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   843
      gradeField.show_hidden_initial = True
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   844
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   845
    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
   846
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   847
  return survey_form
2478
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   848
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   849
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   850
class HelperForm(object):
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   851
  """Thin wrapper for adding values to params['edit_form'].fields.
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   852
  """
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   853
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   854
  def __init__(self, form=None):
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   855
    """Store the edit_form.
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   856
    """
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   857
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   858
    self.form = form
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   859
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   860
  def __call__(self, instance=None):
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   861
    """Transparently instantiate and add initial values to the edit_form.
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   862
    """
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   863
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   864
    form = self.form(instance=instance)
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   865
    form.fields['created_by'].initial = instance.author.name
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   866
    form.fields['last_modified_by'].initial = instance.modified_by.name
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   867
    form.fields['doc_key_name'].initial = instance.key().id_or_name()
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   868
    return form
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   869
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   870
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   871
def _get_csv_header(sur):
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   872
  """CSV header helper, needs support for comment lines in CSV.
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   873
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   874
  Args:
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   875
      sur: Survey entity
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   876
  """
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   877
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   878
  tpl = '# %s: %s\n'
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   879
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   880
  # add static properties
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   881
  fields = ['# Melange Survey export for \n#  %s\n#\n' % sur.title]
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   882
  fields += [tpl % (k,v) for k,v in sur.toDict().items()]
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   883
  fields += [tpl % (f, str(getattr(sur, f))) for f in PLAIN.split()]
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   884
  fields += [tpl % (f, str(getattr(sur, f).link_id)) for f in FIELDS.split()]
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   885
  fields.sort()
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   886
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   887
  # add dynamic properties
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   888
  fields += ['#\n#---\n#\n']
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   889
  dynamic = sur.survey_content.dynamic_properties()
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   890
  dynamic = [(prop, getattr(sur.survey_content, prop)) for prop in dynamic]
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   891
  fields += [tpl % (k,v) for k,v in sorted(dynamic)]
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   892
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   893
  # add schema
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   894
  fields += ['#\n#---\n#\n']
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   895
  schema =  sur.survey_content.schema
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   896
  indent = '},\n#' + ' ' * 9
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   897
  fields += [tpl % ('Schema', schema.replace('},', indent)) + '#\n']
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   898
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   899
  return ''.join(fields).replace('\n', '\r\n')
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   900
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   901
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   902
def _get_records(recs, props):
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   903
  """Fetch properties from SurveyRecords for CSV export.
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   904
  """
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   905
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   906
  records = []
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   907
  props = props[1:]
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   908
  for rec in recs:
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   909
    values = tuple(getattr(rec, prop, None) for prop in props)
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   910
    leading = (rec.user.link_id,)
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   911
    records.append(leading + values)
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   912
  return records
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   913
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   914
2492
6eac6cd88dad Made the CSV exporter for Survey not rely on the BackReferenceProperty in Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2489
diff changeset
   915
def to_csv(survey_view):
2478
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   916
  """CSV exporter.
2492
6eac6cd88dad Made the CSV exporter for Survey not rely on the BackReferenceProperty in Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2489
diff changeset
   917
6eac6cd88dad Made the CSV exporter for Survey not rely on the BackReferenceProperty in Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2489
diff changeset
   918
  Args:
6eac6cd88dad Made the CSV exporter for Survey not rely on the BackReferenceProperty in Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2489
diff changeset
   919
      survey_view: instance of the SurveyView
2478
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   920
  """
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   921
2492
6eac6cd88dad Made the CSV exporter for Survey not rely on the BackReferenceProperty in Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2489
diff changeset
   922
  def wrapper(survey):
6eac6cd88dad Made the CSV exporter for Survey not rely on the BackReferenceProperty in Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2489
diff changeset
   923
    """Wrapper function.
6eac6cd88dad Made the CSV exporter for Survey not rely on the BackReferenceProperty in Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2489
diff changeset
   924
    """
6eac6cd88dad Made the CSV exporter for Survey not rely on the BackReferenceProperty in Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2489
diff changeset
   925
    survey_logic = survey_view.getParams()['logic']
6eac6cd88dad Made the CSV exporter for Survey not rely on the BackReferenceProperty in Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2489
diff changeset
   926
    record_logic = survey_logic.getRecordLogic()
2478
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   927
2492
6eac6cd88dad Made the CSV exporter for Survey not rely on the BackReferenceProperty in Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2489
diff changeset
   928
    # get header and properties
6eac6cd88dad Made the CSV exporter for Survey not rely on the BackReferenceProperty in Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2489
diff changeset
   929
    header = _get_csv_header(survey)
6eac6cd88dad Made the CSV exporter for Survey not rely on the BackReferenceProperty in Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2489
diff changeset
   930
    leading = ['user', 'created', 'modified']
6eac6cd88dad Made the CSV exporter for Survey not rely on the BackReferenceProperty in Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2489
diff changeset
   931
    properties = leading + survey.survey_content.orderedProperties()
6eac6cd88dad Made the CSV exporter for Survey not rely on the BackReferenceProperty in Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2489
diff changeset
   932
6eac6cd88dad Made the CSV exporter for Survey not rely on the BackReferenceProperty in Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2489
diff changeset
   933
    # retrieve the query of the data to export
6eac6cd88dad Made the CSV exporter for Survey not rely on the BackReferenceProperty in Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2489
diff changeset
   934
    fields = {'survey': survey}
6eac6cd88dad Made the CSV exporter for Survey not rely on the BackReferenceProperty in Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2489
diff changeset
   935
    record_query = record_logic.getQueryForFields(fields)
2478
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   936
2492
6eac6cd88dad Made the CSV exporter for Survey not rely on the BackReferenceProperty in Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2489
diff changeset
   937
    try:
6eac6cd88dad Made the CSV exporter for Survey not rely on the BackReferenceProperty in Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2489
diff changeset
   938
      first = record_query.run().next()
6eac6cd88dad Made the CSV exporter for Survey not rely on the BackReferenceProperty in Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2489
diff changeset
   939
    except StopIteration:
6eac6cd88dad Made the CSV exporter for Survey not rely on the BackReferenceProperty in Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2489
diff changeset
   940
      # bail out early if survey_records.run() is empty
6eac6cd88dad Made the CSV exporter for Survey not rely on the BackReferenceProperty in Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2489
diff changeset
   941
      return header, survey.link_id
2501
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   942
2492
6eac6cd88dad Made the CSV exporter for Survey not rely on the BackReferenceProperty in Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2489
diff changeset
   943
    # generate results list
6eac6cd88dad Made the CSV exporter for Survey not rely on the BackReferenceProperty in Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2489
diff changeset
   944
    recs = record_query.run()
6eac6cd88dad Made the CSV exporter for Survey not rely on the BackReferenceProperty in Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2489
diff changeset
   945
    recs = _get_records(recs, properties)
2501
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   946
2492
6eac6cd88dad Made the CSV exporter for Survey not rely on the BackReferenceProperty in Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2489
diff changeset
   947
    # write results to CSV
6eac6cd88dad Made the CSV exporter for Survey not rely on the BackReferenceProperty in Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2489
diff changeset
   948
    output = StringIO.StringIO()
6eac6cd88dad Made the CSV exporter for Survey not rely on the BackReferenceProperty in Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2489
diff changeset
   949
    writer = csv.writer(output)
6eac6cd88dad Made the CSV exporter for Survey not rely on the BackReferenceProperty in Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2489
diff changeset
   950
    writer.writerow(properties)
6eac6cd88dad Made the CSV exporter for Survey not rely on the BackReferenceProperty in Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2489
diff changeset
   951
    writer.writerows(recs)
2501
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   952
2492
6eac6cd88dad Made the CSV exporter for Survey not rely on the BackReferenceProperty in Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2489
diff changeset
   953
    return header + output.getvalue(), survey.link_id
6eac6cd88dad Made the CSV exporter for Survey not rely on the BackReferenceProperty in Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2489
diff changeset
   954
  return wrapper