app/soc/views/helper/surveys.py
author Lennard de Rijk <ljvderijk@gmail.com>
Fri, 03 Jul 2009 18:20:33 +0200
changeset 2510 a85484d1f441
parent 2507 4a6b8d6674b6
child 2517 97117d341f62
permissions -rw-r--r--
Ensure that the params from subclasses get merged properly with ProjectSurvey view.
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
2507
4a6b8d6674b6 Fix pick_multi only returning one answer as maximum.
Daniel Diniz <ajaksu@gmail.com>
parents: 2506
diff changeset
   142
    # flag whether we can use getlist to retrieve multiple values
4a6b8d6674b6 Fix pick_multi only returning one answer as maximum.
Daniel Diniz <ajaksu@gmail.com>
parents: 2506
diff changeset
   143
    is_post = hasattr(post_dict, 'getlist')
4a6b8d6674b6 Fix pick_multi only returning one answer as maximum.
Daniel Diniz <ajaksu@gmail.com>
parents: 2506
diff changeset
   144
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   145
    # add unordered fields to self.survey_fields
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   146
    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
   147
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   148
      # a comment made by the user
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   149
      comment = ''
2501
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
      # flag to know where the value came from
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   152
      from_content = False
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   153
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   154
      if has_record and field in post_dict:
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   155
        # entered value that is not yet saved
2507
4a6b8d6674b6 Fix pick_multi only returning one answer as maximum.
Daniel Diniz <ajaksu@gmail.com>
parents: 2506
diff changeset
   156
        if schema.getType(field) == 'pick_multi' and is_post:
4a6b8d6674b6 Fix pick_multi only returning one answer as maximum.
Daniel Diniz <ajaksu@gmail.com>
parents: 2506
diff changeset
   157
          value = post_dict.getlist(field)
4a6b8d6674b6 Fix pick_multi only returning one answer as maximum.
Daniel Diniz <ajaksu@gmail.com>
parents: 2506
diff changeset
   158
        else:
4a6b8d6674b6 Fix pick_multi only returning one answer as maximum.
Daniel Diniz <ajaksu@gmail.com>
parents: 2506
diff changeset
   159
          value = post_dict[field]
2501
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   160
        if COMMENT_PREFIX + field in post_dict:
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   161
          comment = post_dict[COMMENT_PREFIX + field]
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   162
      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
   163
        # previously entered value
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   164
        value = getattr(self.survey_record, field)
2501
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   165
        if hasattr(self.survey_record, COMMENT_PREFIX + field):
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   166
          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
   167
      else:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   168
        # use prompts set by survey creator
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   169
        value = getattr(self.survey_content, field)
2501
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   170
        from_content = True
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   171
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   172
      label = schema.getLabel(field)
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   173
      if label is None:
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   174
        continue
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   175
2501
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   176
      # fix validation for pick_multi fields
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   177
      is_multi = schema.getType(field) == 'pick_multi'
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   178
      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
   179
        if isinstance(value, basestring):
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   180
          value = value.split(',')
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   181
      elif from_content and is_multi:
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   182
        value = []
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   183
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   184
      # record field value for validation
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   185
      if not from_content:
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   186
        self.data[field] = value
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   187
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   188
      # find correct field type
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   189
      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
   190
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   191
      # 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
   192
      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
   193
      kwargs = dict(label=label, req=required)
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   194
2502
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   195
      # 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
   196
      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
   197
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   198
      # 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
   199
      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
   200
        self.data[COMMENT_PREFIX + field] = comment
2501
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   201
        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
   202
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   203
    return self.insertFields()
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   204
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   205
  def insertFields(self):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   206
    """Add ordered fields to self.fields.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   207
    """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   208
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   209
    survey_order = self.survey_content.getSurveyOrder()
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   210
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   211
    # first, insert dynamic survey fields
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   212
    for position, property in survey_order.items():
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   213
      position = position * 2
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   214
      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
   215
      if not self.editing:
2501
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   216
        # 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
   217
        property = COMMENT_PREFIX + property
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   218
        if property in self.survey_fields:
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   219
          self.fields.insert(position - 1, property,
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   220
                             self.survey_fields[property])
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   221
    return self.fields
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   222
2502
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   223
  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
   224
                   tip='', comment=''):
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   225
    """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
   226
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   227
    params:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   228
      field: the current field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   229
      value: the initial value for this field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   230
      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
   231
      schema: schema for survey
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   232
      req: required bool
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   233
      label: label for field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   234
      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
   235
      comment: initial comment value for field
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   236
    """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   237
2502
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   238
    # 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
   239
    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
   240
    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
   241
    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
   242
                          editing=self.editing)
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   243
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   244
    if not tip:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   245
      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
   246
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   247
    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
   248
                         widget=widget, initial=value)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   249
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   250
    self.survey_fields[field] = question
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   251
2468
0273c1a8d708 Fixed too long line in Surveys helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2465
diff changeset
   252
  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
   253
                    tip='', comment=''):
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   254
    """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
   255
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   256
    params:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   257
      field: the current field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   258
      value: the initial value for this field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   259
      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
   260
      schema: schema for survey
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   261
      req: required bool
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   262
      label: label for field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   263
      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
   264
      comment: initial comment value for field
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   265
    """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   266
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   267
    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
   268
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   269
    # 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
   270
    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
   271
    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
   272
    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
   273
                          editing=self.editing)
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   274
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   275
    if not tip:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   276
      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
   277
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   278
    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
   279
                         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
   280
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   281
    self.survey_fields[field] = question
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   282
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   283
  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
   284
                     tip='', comment=''):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   285
    """Add a selection field to this form.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   286
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   287
    params:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   288
      field: the current field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   289
      value: the initial value for this field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   290
      attrs: additional attributes for field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   291
      schema: schema for survey
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   292
      req: required bool
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   293
      label: label for field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   294
      tip: tooltip text for field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   295
      comment: initial comment value for field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   296
    """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   297
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   298
    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
   299
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   300
    these_choices = []
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   301
    # add all properties, but select chosen one
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   302
    options = getattr(self.survey_content, field)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   303
    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
   304
    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
   305
      these_choices.append((value, value))
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   306
      if value in options:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   307
        options.remove(value)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   308
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   309
    for option in options:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   310
      these_choices.append((option, option))
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   311
    if not tip:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   312
      tip = 'Please select an answer this question.'
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   313
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   314
    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
   315
                            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
   316
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   317
    self.survey_fields[field] = question
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   318
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   319
  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
   320
                    tip='', comment=''):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   321
    """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
   322
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   323
    params:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   324
      field: the current field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   325
      value: the initial value for this field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   326
      attrs: additional attributes for field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   327
      schema: schema for survey
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   328
      req: required bool
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   329
      label: label for field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   330
      tip: tooltip text for field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   331
      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
   332
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   333
    """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   334
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   335
    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
   336
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   337
    # 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
   338
    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
   339
      # 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
   340
      value = value.split(',')
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   341
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   342
    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
   343
    if not tip:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   344
      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
   345
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   346
    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
   347
                             choices=tuple(these_choices), widget=widget,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   348
                             initial=value)
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   349
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   350
    self.survey_fields[field] = question
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   351
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   352
  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
   353
                    tip='', comment=''):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   354
    """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
   355
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   356
    params:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   357
      field: the current field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   358
      value: the initial value for this field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   359
      attrs: additional attributes for field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   360
      schema: schema for survey
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   361
      req: required bool
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   362
      label: label for field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   363
      tip: tooltip text for field
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   364
      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
   365
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   366
    """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   367
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   368
    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
   369
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   370
    if self.survey_record:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   371
      value = value
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   372
    else:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   373
      value = None
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   374
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   375
    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
   376
    if not tip:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   377
      tip = 'Please select one of these choices.'
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   378
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   379
    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
   380
                             choices=tuple(these_choices), widget=widget,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   381
                             initial=value)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   382
    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
   383
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   384
  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
   385
    if not self.editing:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   386
      widget = widgets.Textarea(attrs=attrs)
2501
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   387
      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
   388
                          widget=widget, initial=comment)
2501
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   389
      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
   390
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   391
  class Meta(object):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   392
    model = SurveyContent
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   393
    exclude = ['schema']
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   394
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   395
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   396
class SurveyContentSchema(object):
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   397
  """Abstract question metadata handling.
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   398
  """
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   399
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   400
  def __init__(self, schema):
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   401
    self.schema = eval(schema)
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   402
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   403
  def getType(self, field):
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   404
    return self.schema[field]["type"]
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   405
2502
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 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
   407
    """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
   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]["required"]
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   411
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   412
  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
   413
    """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
   414
    """
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   415
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   416
    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
   417
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   418
  def getRender(self, field):
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   419
    return self.schema[field]["render"]
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   420
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   421
  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
   422
    """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
   423
    """
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   424
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   425
    if editing:
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   426
      kind = self.getType(field)
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   427
      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
   428
      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
   429
      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
   430
      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
   431
    else:
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   432
      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
   433
    return widget
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   434
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   435
  def getLabel(self, field):
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   436
    """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
   437
    """
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   438
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   439
    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
   440
      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
   441
                    (field, str(self.schema)))
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   442
      return
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   443
    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
   444
      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
   445
    else:
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   446
      label = field
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   447
    return label
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   448
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   449
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   450
class UniversalChoiceEditor(widgets.Widget):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   451
  """Edit interface for choice questions.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   452
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   453
  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
   454
  """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   455
2502
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   456
  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
   457
               choices=()):
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   458
    """
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   459
    params:
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   460
      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
   461
      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
   462
      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
   463
      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
   464
    """
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   465
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   466
    self.attrs = attrs or {}
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   467
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   468
    # 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
   469
    # 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
   470
    # more than once.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   471
    self.choices = list(choices)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   472
    self.kind = kind
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   473
    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
   474
    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
   475
    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
   476
2432
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
  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
   479
    """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
   480
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   481
    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
   482
    """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   483
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   484
    if value is None:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   485
      value = ''
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   486
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   487
    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
   488
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   489
    # 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
   490
    selected = 'selected="selected"'
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   491
    context =  dict(
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   492
        name=name,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   493
        is_selection=selected * (self.kind == 'selection'),
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   494
        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
   495
        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
   496
        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
   497
        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
   498
        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
   499
        )
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   500
2502
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   501
    # 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
   502
    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
   503
        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
   504
        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
   505
        ))
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   506
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   507
    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
   508
    chained_choices = enumerate(chain(self.choices, choices))
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   509
    choices = {}
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   510
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   511
    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
   512
      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
   513
      choices[i] = option_value
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   514
    context['choices'] = choices
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   515
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   516
    template = 'soc/survey/universal_choice_editor.html'
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   517
    return loader.render_to_string(template, context)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   518
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   519
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   520
class PickOneField(forms.ChoiceField):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   521
  """Stub for customizing the single choice field.
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
  #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
   524
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   525
  def __init__(self, *args, **kwargs):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   526
    super(PickOneField, self).__init__(*args, **kwargs)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   527
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   528
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   529
class PickManyField(forms.MultipleChoiceField):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   530
  """Stub for customizing the multiple choice field.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   531
  """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   532
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   533
  def __init__(self, *args, **kwargs):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   534
    super(PickManyField, self).__init__(*args, **kwargs)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   535
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   536
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   537
class PickQuantField(forms.MultipleChoiceField):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   538
  """Stub for customizing the multiple choice field.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   539
  """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   540
  #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
   541
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   542
  def __init__(self, *args, **kwargs):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   543
    super(PickQuantField, self).__init__(*args, **kwargs)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   544
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   545
2502
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   546
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
   547
  """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
   548
  """
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   549
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   550
  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
   551
    """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
   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
    params:
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   554
      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
   555
      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
   556
      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
   557
    """
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
    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
   560
    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
   561
    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
   562
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   563
    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
   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
  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
   566
    """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
   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
    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
   569
    """
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   570
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   571
    # 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
   572
    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
   573
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   574
    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
   575
      # 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
   576
      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
   577
                     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
   578
      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
   579
      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
   580
      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
   581
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   582
      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
   583
    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
   584
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   585
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   586
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
   587
  """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
   588
  """
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   589
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   590
  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
   591
    """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
   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
    params:
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   594
      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
   595
      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
   596
      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
   597
    """
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
    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
   600
    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
   601
    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
   602
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   603
    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
   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
  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
   606
    """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
   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
    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
   609
    """
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   610
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   611
    # 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
   612
    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
   613
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   614
    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
   615
      # 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
   616
      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
   617
                     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
   618
      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
   619
      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
   620
      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
   621
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   622
      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
   623
    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
   624
2e096acc8720 Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents: 2501
diff changeset
   625
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   626
class PickOneSelect(forms.Select):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   627
  """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
   628
  """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   629
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   630
  def __init__(self, *args, **kwargs):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   631
    super(PickOneSelect, self).__init__(*args, **kwargs)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   632
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   633
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   634
class PickManyCheckbox(forms.CheckboxSelectMultiple):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   635
  """Customized multiple choice checkbox widget.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   636
  """
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
  def __init__(self, *args, **kwargs):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   639
    super(PickManyCheckbox, self).__init__(*args, **kwargs)
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
  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
   642
    """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
   643
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   644
    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
   645
    """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   646
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   647
    if value is None:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   648
      value = []
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   649
    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
   650
    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
   651
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   652
    # normalize to strings.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   653
    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
   654
    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
   655
    smart_unicode = forms.util.smart_unicode
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   656
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   657
    # set container fieldset and list
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   658
    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
   659
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   660
    # add numbered checkboxes wrapped in list items
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   661
    chained_choices = enumerate(chain(self.choices, choices))
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   662
    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
   663
      option_label = escape(smart_unicode(option_label))
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   664
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   665
      # 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
   666
      # 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
   667
      if has_id:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   668
        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
   669
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   670
      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
   671
      rendered_cb = cb.render(name, option_value)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   672
      cb_label = (rendered_cb, option_label)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   673
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   674
      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
   675
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   676
    output.append(u'  </ul>\n</fieldset>')
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   677
    return u'\n'.join(output)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   678
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   679
  def id_for_label(self, id_):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   680
    # 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
   681
    if id_:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   682
      id_ += '_fieldset'
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   683
    return id_
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   684
  id_for_label = classmethod(id_for_label)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   685
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   686
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   687
class PickQuantRadioRenderer(widgets.RadioFieldRenderer):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   688
  """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
   689
  """
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
  def __init__(self, *args, **kwargs):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   692
    super(PickQuantRadioRenderer, self).__init__(*args, **kwargs)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   693
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   694
  def render(self):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   695
    """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
   696
    """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   697
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   698
    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
   699
                     % 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
   700
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
class PickQuantRadio(forms.RadioSelect):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   703
  """TODO(James,Ajaksu) Fix Docstring
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
  renderer = PickQuantRadioRenderer
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   707
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   708
  def __init__(self, *args, **kwargs):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   709
    super(PickQuantRadio, self).__init__(*args, **kwargs)
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
# 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
   713
WIDGETS = {'multi_checkbox': PickManyCheckbox,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   714
           'single_select': PickOneSelect,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   715
           'quant_radio': PickQuantRadio}
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   716
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   717
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   718
class SurveyResults(widgets.Widget):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   719
  """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
   720
  """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   721
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   722
  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
   723
             order=[], idx=0, context={}):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   724
    """ renders list of survey results
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   725
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   726
    params:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   727
      survey: current survey
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   728
      params: dict of params for rendering list
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   729
      filter: filter for list results
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   730
      limit: limit for list results
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   731
      offset: offset for list results
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   732
      order: order for list results
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   733
      idx: index for list results
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   734
      context: context dict for template
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   735
    """
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   736
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
   737
    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
   738
    record_logic = survey_logic.getRecordLogic()
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   739
    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
   740
    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
   741
                              order=order)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   742
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   743
    params['name'] = "Survey Results"
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   744
    content = {
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   745
      'idx': idx,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   746
      'data': data,
2465
037a7e79a635 Use an existing logic for showing Survey Results.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2463
diff changeset
   747
      'logic': record_logic,
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   748
      'limit': limit,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   749
     }
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   750
    updates = dicts.rename(params, params['list_params'])
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   751
    content.update(updates)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   752
    contents = [content]
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
    if len(content) == 1:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   755
      content = content[0]
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   756
      key_order = content.get('key_order')
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   757
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   758
    context['list'] = Lists(contents)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   759
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   760
    # 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
   761
    for list_ in context['list']._contents:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   762
      if len(list_['data']) < 1:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   763
        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
   764
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   765
      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
   766
      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
   767
      list_['description'] = 'Survey Results:'
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   768
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   769
    context['properties'] = survey.survey_content.orderedProperties()
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   770
    context['entity_type'] = "Survey Results"
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   771
    context['entity_type_plural'] = "Results"
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   772
    context['no_lists_msg'] = "No Survey Results"
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   773
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   774
    path = (survey.entity_type().lower(), survey.prefix,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   775
            survey.scope_path, survey.link_id)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   776
    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
   777
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   778
    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
   779
                                     dictionary=context).strip('\n')
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   780
    return markup
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   781
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   782
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   783
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
   784
                          survey_record):
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   785
  """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
   786
  students get a required Project field.
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   787
2451
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   788
  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
   789
  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
   790
  (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
   791
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   792
  params:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   793
    survey: the survey being taken
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   794
    user: the survey-taking user
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   795
    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
   796
    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
   797
    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
   798
      or None
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
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   801
  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
   802
  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
   803
  if not these_projects:
c58a7ea6c126 Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents: 2442
diff changeset
   804
    return False # no projects found
2432
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   805
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   806
  project_pairs = []
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   807
  #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
   808
  for project in these_projects:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   809
    project_pairs.append((project.key(), project.title))
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   810
  if project_pairs:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   811
    project_tuples = tuple(project_pairs)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   812
    # add select field containing list of projects
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   813
    projectField =  forms.fields.ChoiceField(
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   814
                              choices=project_tuples,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   815
                              required=True,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   816
                              widget=forms.Select())
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   817
    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
   818
    # if editing an existing survey
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   819
    if not this_project and survey_record:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   820
      this_project = survey_record.project
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   821
    if this_project:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   822
      for tup in project_tuples:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   823
        if tup[1] == this_project.title:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   824
          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
   825
          else: project_name = tup[1]
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   826
          projectField.choices.remove(tup)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   827
          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
   828
          break
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   829
    survey_form.fields.insert(0, 'project', projectField )
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   830
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   831
  if survey.taking_access == "mentor evaluation":
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   832
    # 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
   833
    # determining if student passes or fails.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   834
    # 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
   835
    # is midterm_passed, final_passed, etc.
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   836
    grade_choices = (('pass', 'Pass'), ('fail', 'Fail'))
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   837
    grade_vals = { 'pass': True, 'fail': False }
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   838
    gradeField = forms.fields.ChoiceField(choices=grade_choices,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   839
                                           required=True,
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   840
                                           widget=forms.Select())
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   841
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   842
    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
   843
    if survey_record:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   844
      for g in grade_choices:
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   845
        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
   846
          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
   847
          gradeField.choices.remove(g)
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   848
          break;
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   849
      gradeField.show_hidden_initial = True
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   850
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   851
    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
   852
636dfd5381c2 Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   853
  return survey_form
2478
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   854
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   855
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   856
class HelperForm(object):
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   857
  """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
   858
  """
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 __init__(self, form=None):
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   861
    """Store 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
    self.form = form
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   865
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   866
  def __call__(self, instance=None):
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   867
    """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
   868
    """
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
    form = self.form(instance=instance)
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   871
    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
   872
    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
   873
    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
   874
    return form
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   875
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
def _get_csv_header(sur):
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   878
  """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
   879
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   880
  Args:
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   881
      sur: Survey entity
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   882
  """
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   883
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   884
  tpl = '# %s: %s\n'
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   885
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   886
  # add static properties
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   887
  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
   888
  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
   889
  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
   890
  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
   891
  fields.sort()
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 dynamic properties
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
  dynamic = sur.survey_content.dynamic_properties()
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   896
  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
   897
  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
   898
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   899
  # add schema
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   900
  fields += ['#\n#---\n#\n']
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   901
  schema =  sur.survey_content.schema
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   902
  indent = '},\n#' + ' ' * 9
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   903
  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
   904
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   905
  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
   906
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   907
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   908
def _get_records(recs, props):
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   909
  """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
   910
  """
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   911
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   912
  records = []
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   913
  props = props[1:]
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   914
  for rec in recs:
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   915
    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
   916
    leading = (rec.user.link_id,)
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   917
    records.append(leading + values)
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   918
  return records
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   919
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   920
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
   921
def to_csv(survey_view):
2478
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   922
  """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
   923
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
  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
   925
      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
   926
  """
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
  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
   929
    """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
   930
    """
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
    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
   932
    record_logic = survey_logic.getRecordLogic()
2478
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2470
diff changeset
   933
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
   934
    # 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
   935
    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
   936
    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
   937
    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
   938
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
    # 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
   940
    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
   941
    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
   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
    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
   944
      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
   945
    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
   946
      # 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
   947
      return header, survey.link_id
2501
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   948
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
   949
    # 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
   950
    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
   951
    recs = _get_records(recs, properties)
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
    # 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
   954
    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
   955
    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
   956
    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
   957
    writer.writerows(recs)
2501
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2492
diff changeset
   958
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
   959
    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
   960
  return wrapper