author | Lennard de Rijk <ljvderijk@gmail.com> |
Sun, 05 Jul 2009 12:59:33 +0200 | |
changeset 2547 | c7726e3ab173 |
parent 2542 | a9dec4763c6b |
child 2560 | a944c0169ad8 |
permissions | -rw-r--r-- |
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 |
|
2539
dd0322a37e44
Ensure that long questions and comments are stored as db.Text.
Daniel Diniz <ajaksu@gmail.com>
parents:
2533
diff
changeset
|
33 |
from google.appengine.ext import db |
2432
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
34 |
from google.appengine.ext.db import djangoforms |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
35 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
36 |
from django import forms |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
37 |
from django.forms import widgets |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
38 |
from django.forms.fields import CharField |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
39 |
from django.template import loader |
2542
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
40 |
from django.utils.datastructures import SortedDict |
2432
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
41 |
from django.utils.encoding import force_unicode |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
42 |
from django.utils.html import escape |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
43 |
from django.utils.safestring import mark_safe |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
44 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
45 |
from soc.logic import dicts |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
46 |
from soc.logic.lists import Lists |
2501
d612b48e6e12
Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents:
2492
diff
changeset
|
47 |
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
|
48 |
from soc.models.survey import SurveyContent |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
49 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
50 |
|
2517
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
51 |
CHOICE_TYPES = set(('selection', 'pick_multi', 'choice', 'pick_quant')) |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
52 |
|
2502
2e096acc8720
Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents:
2501
diff
changeset
|
53 |
# 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
|
54 |
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
|
55 |
<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
|
56 |
<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
|
57 |
<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
|
58 |
>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
|
59 |
<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
|
60 |
{% endif %}>False</option> |
2526
8f29bfb9eb52
Survey editing/taking Javascript and CSS fixes.
James Levy <jamesalexanderlevy@gmail.com>
parents:
2520
diff
changeset
|
61 |
</select> |
2502
2e096acc8720
Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents:
2501
diff
changeset
|
62 |
|
2e096acc8720
Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents:
2501
diff
changeset
|
63 |
<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
|
64 |
<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
|
65 |
<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
|
66 |
>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
|
67 |
<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
|
68 |
{% endif %}>False</option> |
2526
8f29bfb9eb52
Survey editing/taking Javascript and CSS fixes.
James Levy <jamesalexanderlevy@gmail.com>
parents:
2520
diff
changeset
|
69 |
</select> |
2502
2e096acc8720
Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents:
2501
diff
changeset
|
70 |
""" |
2e096acc8720
Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents:
2501
diff
changeset
|
71 |
|
2e096acc8720
Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents:
2501
diff
changeset
|
72 |
|
2517
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
73 |
class SurveyTakeForm(djangoforms.ModelForm): |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
74 |
"""SurveyContent form for recording survey answers. |
2432
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
75 |
|
2517
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
76 |
This class is used to produce survey forms for survey taking: |
2432
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
77 |
- User taking survey |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
78 |
- User updating already taken survey |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
79 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
80 |
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
|
81 |
survey form is dynamically formed. |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
82 |
""" |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
83 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
84 |
def __init__(self, *args, **kwargs): |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
85 |
"""Store special kwargs as attributes. |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
86 |
|
2520
859ada69db69
Style fixes in Survey helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2518
diff
changeset
|
87 |
params: |
2517
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
88 |
survey_content: a SuveryContent entity. |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
89 |
survey_logic: instance of SurveyLogic. |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
90 |
survey_record: a SurveyRecord entity. |
2432
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
91 |
read_only: controls whether the survey taking UI allows data entry. |
2527
ce657149b90a
Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2526
diff
changeset
|
92 |
data: dictionary mapping fields to data for validation. |
2432
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
93 |
""" |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
94 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
95 |
self.kwargs = kwargs |
2517
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
96 |
|
2451
c58a7ea6c126
Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents:
2442
diff
changeset
|
97 |
self.survey_content = self.kwargs.pop('survey_content', 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
|
98 |
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
|
99 |
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
|
100 |
self.read_only = self.kwargs.pop('read_only', None) |
2432
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
101 |
|
2451
c58a7ea6c126
Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents:
2442
diff
changeset
|
102 |
self.fields_map = dict( |
c58a7ea6c126
Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents:
2442
diff
changeset
|
103 |
long_answer=self.addLongField, |
c58a7ea6c126
Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents:
2442
diff
changeset
|
104 |
short_answer=self.addShortField, |
c58a7ea6c126
Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents:
2442
diff
changeset
|
105 |
selection=self.addSingleField, |
c58a7ea6c126
Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents:
2442
diff
changeset
|
106 |
pick_multi=self.addMultiField, |
c58a7ea6c126
Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents:
2442
diff
changeset
|
107 |
pick_quant=self.addQuantField, |
c58a7ea6c126
Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents:
2442
diff
changeset
|
108 |
) |
2432
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
109 |
|
2542
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
110 |
# get the POST data dict if present |
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
111 |
data = self.kwargs.pop('data', None) |
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
112 |
|
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
113 |
# set cleaner methods for fields, only needed if we have POST data |
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
114 |
if data: |
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
115 |
# prepare to render a bound, validating form |
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
116 |
clean_data = self.setCleaners(data) |
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
117 |
else: |
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
118 |
clean_data = self.setCleaners() |
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
119 |
|
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
120 |
# update with fields from subclasses |
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
121 |
if hasattr(self, 'data') and self.data: |
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
122 |
clean_data.update(self.data) |
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
123 |
delattr(self, 'data') |
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
124 |
|
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
125 |
# pass data, so form is bound |
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
126 |
if data: |
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
127 |
self.kwargs['data'] = clean_data |
2539
dd0322a37e44
Ensure that long questions and comments are stored as db.Text.
Daniel Diniz <ajaksu@gmail.com>
parents:
2533
diff
changeset
|
128 |
|
2517
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
129 |
super(SurveyTakeForm, self).__init__(*args, **self.kwargs) |
2432
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
130 |
|
2542
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
131 |
self.fields = self.getFields(clean_data) |
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
132 |
|
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
133 |
def setCleaners(self, post_dict=None): |
2539
dd0322a37e44
Ensure that long questions and comments are stored as db.Text.
Daniel Diniz <ajaksu@gmail.com>
parents:
2533
diff
changeset
|
134 |
"""Set cleaner methods for dynamic fields. |
dd0322a37e44
Ensure that long questions and comments are stored as db.Text.
Daniel Diniz <ajaksu@gmail.com>
parents:
2533
diff
changeset
|
135 |
|
2542
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
136 |
Used for storing textual input as Text instead of StringProperty. If |
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
137 |
passed a dict of field names/values (as the kwarg 'data' to __init__), |
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
138 |
it's possible to set clean_[field_id] methods for validation. |
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
139 |
|
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
140 |
This method populates the 'data' dict used for generating form fields. |
2539
dd0322a37e44
Ensure that long questions and comments are stored as db.Text.
Daniel Diniz <ajaksu@gmail.com>
parents:
2533
diff
changeset
|
141 |
""" |
dd0322a37e44
Ensure that long questions and comments are stored as db.Text.
Daniel Diniz <ajaksu@gmail.com>
parents:
2533
diff
changeset
|
142 |
|
dd0322a37e44
Ensure that long questions and comments are stored as db.Text.
Daniel Diniz <ajaksu@gmail.com>
parents:
2533
diff
changeset
|
143 |
# prefix for method names |
dd0322a37e44
Ensure that long questions and comments are stored as db.Text.
Daniel Diniz <ajaksu@gmail.com>
parents:
2533
diff
changeset
|
144 |
clean = 'clean_' |
dd0322a37e44
Ensure that long questions and comments are stored as db.Text.
Daniel Diniz <ajaksu@gmail.com>
parents:
2533
diff
changeset
|
145 |
|
dd0322a37e44
Ensure that long questions and comments are stored as db.Text.
Daniel Diniz <ajaksu@gmail.com>
parents:
2533
diff
changeset
|
146 |
# data is passed to super's __init__ as the 'data' kwarg |
dd0322a37e44
Ensure that long questions and comments are stored as db.Text.
Daniel Diniz <ajaksu@gmail.com>
parents:
2533
diff
changeset
|
147 |
data = {} |
dd0322a37e44
Ensure that long questions and comments are stored as db.Text.
Daniel Diniz <ajaksu@gmail.com>
parents:
2533
diff
changeset
|
148 |
|
2542
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
149 |
# flag whether we can use getlist to retrieve multiple values |
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
150 |
is_post = hasattr(post_dict, 'getlist') |
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
151 |
|
2539
dd0322a37e44
Ensure that long questions and comments are stored as db.Text.
Daniel Diniz <ajaksu@gmail.com>
parents:
2533
diff
changeset
|
152 |
schema = {} |
dd0322a37e44
Ensure that long questions and comments are stored as db.Text.
Daniel Diniz <ajaksu@gmail.com>
parents:
2533
diff
changeset
|
153 |
if self.survey_content: |
dd0322a37e44
Ensure that long questions and comments are stored as db.Text.
Daniel Diniz <ajaksu@gmail.com>
parents:
2533
diff
changeset
|
154 |
schema = eval(self.survey_content.schema) |
dd0322a37e44
Ensure that long questions and comments are stored as db.Text.
Daniel Diniz <ajaksu@gmail.com>
parents:
2533
diff
changeset
|
155 |
|
dd0322a37e44
Ensure that long questions and comments are stored as db.Text.
Daniel Diniz <ajaksu@gmail.com>
parents:
2533
diff
changeset
|
156 |
for key, val in schema.items(): |
dd0322a37e44
Ensure that long questions and comments are stored as db.Text.
Daniel Diniz <ajaksu@gmail.com>
parents:
2533
diff
changeset
|
157 |
if val['type'] == 'long_answer': |
dd0322a37e44
Ensure that long questions and comments are stored as db.Text.
Daniel Diniz <ajaksu@gmail.com>
parents:
2533
diff
changeset
|
158 |
|
dd0322a37e44
Ensure that long questions and comments are stored as db.Text.
Daniel Diniz <ajaksu@gmail.com>
parents:
2533
diff
changeset
|
159 |
# store > 500 chars per long answer |
dd0322a37e44
Ensure that long questions and comments are stored as db.Text.
Daniel Diniz <ajaksu@gmail.com>
parents:
2533
diff
changeset
|
160 |
setattr(self, clean + key, |
dd0322a37e44
Ensure that long questions and comments are stored as db.Text.
Daniel Diniz <ajaksu@gmail.com>
parents:
2533
diff
changeset
|
161 |
lambda key=key: db.Text(self.cleaned_data.get(key)) |
dd0322a37e44
Ensure that long questions and comments are stored as db.Text.
Daniel Diniz <ajaksu@gmail.com>
parents:
2533
diff
changeset
|
162 |
) |
dd0322a37e44
Ensure that long questions and comments are stored as db.Text.
Daniel Diniz <ajaksu@gmail.com>
parents:
2533
diff
changeset
|
163 |
|
dd0322a37e44
Ensure that long questions and comments are stored as db.Text.
Daniel Diniz <ajaksu@gmail.com>
parents:
2533
diff
changeset
|
164 |
if val['has_comment']: |
dd0322a37e44
Ensure that long questions and comments are stored as db.Text.
Daniel Diniz <ajaksu@gmail.com>
parents:
2533
diff
changeset
|
165 |
comment = COMMENT_PREFIX + key |
dd0322a37e44
Ensure that long questions and comments are stored as db.Text.
Daniel Diniz <ajaksu@gmail.com>
parents:
2533
diff
changeset
|
166 |
|
dd0322a37e44
Ensure that long questions and comments are stored as db.Text.
Daniel Diniz <ajaksu@gmail.com>
parents:
2533
diff
changeset
|
167 |
#store > 500 chars per comment field |
dd0322a37e44
Ensure that long questions and comments are stored as db.Text.
Daniel Diniz <ajaksu@gmail.com>
parents:
2533
diff
changeset
|
168 |
setattr(self, clean + comment, |
dd0322a37e44
Ensure that long questions and comments are stored as db.Text.
Daniel Diniz <ajaksu@gmail.com>
parents:
2533
diff
changeset
|
169 |
lambda comment=comment: db.Text(self.cleaned_data.get(comment)) |
dd0322a37e44
Ensure that long questions and comments are stored as db.Text.
Daniel Diniz <ajaksu@gmail.com>
parents:
2533
diff
changeset
|
170 |
) |
dd0322a37e44
Ensure that long questions and comments are stored as db.Text.
Daniel Diniz <ajaksu@gmail.com>
parents:
2533
diff
changeset
|
171 |
|
dd0322a37e44
Ensure that long questions and comments are stored as db.Text.
Daniel Diniz <ajaksu@gmail.com>
parents:
2533
diff
changeset
|
172 |
# put comment in self.data |
2542
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
173 |
if post_dict: |
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
174 |
comment_val = post_dict.get(comment) or None |
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
175 |
else: |
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
176 |
comment_val = getattr(self.survey_record, comment, None) |
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
177 |
data[comment] = comment_val |
2539
dd0322a37e44
Ensure that long questions and comments are stored as db.Text.
Daniel Diniz <ajaksu@gmail.com>
parents:
2533
diff
changeset
|
178 |
|
2542
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
179 |
# put POST or record value for field in self.data |
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
180 |
is_multi = val['type'] == 'pick_multi' |
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
181 |
if post_dict: |
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
182 |
if is_multi and is_post: |
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
183 |
key_val = post_dict.getlist(key) |
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
184 |
else: |
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
185 |
key_val = post_dict.get(key) |
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
186 |
else: |
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
187 |
key_val = getattr(self.survey_record, key, None) |
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
188 |
if is_multi and isinstance(key_val, basestring): |
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
189 |
key_val = key_val.split(',') |
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
190 |
|
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
191 |
data[key] = key_val |
2539
dd0322a37e44
Ensure that long questions and comments are stored as db.Text.
Daniel Diniz <ajaksu@gmail.com>
parents:
2533
diff
changeset
|
192 |
|
dd0322a37e44
Ensure that long questions and comments are stored as db.Text.
Daniel Diniz <ajaksu@gmail.com>
parents:
2533
diff
changeset
|
193 |
return data |
dd0322a37e44
Ensure that long questions and comments are stored as db.Text.
Daniel Diniz <ajaksu@gmail.com>
parents:
2533
diff
changeset
|
194 |
|
2501
d612b48e6e12
Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents:
2492
diff
changeset
|
195 |
def getFields(self, post_dict=None): |
2432
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
196 |
"""Build the SurveyContent (questions) form fields. |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
197 |
|
2501
d612b48e6e12
Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents:
2492
diff
changeset
|
198 |
params: |
d612b48e6e12
Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents:
2492
diff
changeset
|
199 |
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
|
200 |
|
2542
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
201 |
Populates self.survey_fields, which will be ordered in self.insertFields. |
2432
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
202 |
""" |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
203 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
204 |
if not self.survey_content: |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
205 |
return |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
206 |
|
2501
d612b48e6e12
Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents:
2492
diff
changeset
|
207 |
post_dict = post_dict or {} |
2432
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
208 |
self.survey_fields = {} |
2442
dd1f94c3594c
Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents:
2439
diff
changeset
|
209 |
schema = SurveyContentSchema(self.survey_content.schema) |
2432
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
210 |
extra_attrs = {} |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
211 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
212 |
# figure out whether we want a read-only view |
2517
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
213 |
read_only = self.read_only |
2432
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
214 |
|
2517
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
215 |
if not read_only: |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
216 |
survey_content = self.survey_content |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
217 |
survey_entity = self.survey_logic.getSurveyForContent(survey_content) |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
218 |
deadline = survey_entity.survey_end |
2518
66405056baf8
Only initialize a SurveyTakeForm in take instead of in takeGet and takePost.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2517
diff
changeset
|
219 |
read_only = deadline and (datetime.datetime.now() > deadline) |
2517
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
220 |
else: |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
221 |
extra_attrs['disabled'] = 'disabled' |
2432
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
222 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
223 |
# add unordered fields to self.survey_fields |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
224 |
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
|
225 |
|
2542
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
226 |
value = post_dict.get(field) |
2501
d612b48e6e12
Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents:
2492
diff
changeset
|
227 |
|
2542
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
228 |
# skip comments, as they should go below their corresponding questions |
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
229 |
if field.startswith(COMMENT_PREFIX): |
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
230 |
continue |
2432
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
231 |
|
2451
c58a7ea6c126
Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents:
2442
diff
changeset
|
232 |
label = schema.getLabel(field) |
c58a7ea6c126
Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents:
2442
diff
changeset
|
233 |
if label is None: |
2542
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
234 |
# we log this error in getLabel |
2451
c58a7ea6c126
Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents:
2442
diff
changeset
|
235 |
continue |
2432
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
236 |
|
2501
d612b48e6e12
Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents:
2492
diff
changeset
|
237 |
# find correct field type |
2451
c58a7ea6c126
Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents:
2442
diff
changeset
|
238 |
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
|
239 |
|
2e096acc8720
Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents:
2501
diff
changeset
|
240 |
# check if question is required, it's never required when editing |
2517
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
241 |
required = schema.getRequired(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
|
242 |
kwargs = dict(label=label, req=required) |
2432
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
243 |
|
2502
2e096acc8720
Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents:
2501
diff
changeset
|
244 |
# 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
|
245 |
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
|
246 |
|
2e096acc8720
Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents:
2501
diff
changeset
|
247 |
# 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
|
248 |
if schema.getHasComment(field): |
2542
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
249 |
comment = post_dict.get(COMMENT_PREFIX + field) |
2501
d612b48e6e12
Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents:
2492
diff
changeset
|
250 |
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
|
251 |
|
2432
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
252 |
return self.insertFields() |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
253 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
254 |
def insertFields(self): |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
255 |
"""Add ordered fields to self.fields. |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
256 |
""" |
2542
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
257 |
fields = SortedDict() |
2432
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
258 |
survey_order = self.survey_content.getSurveyOrder() |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
259 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
260 |
# first, insert dynamic survey fields |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
261 |
for position, property in survey_order.items(): |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
262 |
position = position * 2 |
2542
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
263 |
fields.insert(position, property, self.survey_fields[property]) |
2517
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
264 |
|
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
265 |
# add comment if field has one and this isn't an edit view |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
266 |
property = COMMENT_PREFIX + property |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
267 |
if property in self.survey_fields: |
2542
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
268 |
fields.insert(position - 1, property, |
2517
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
269 |
self.survey_fields[property]) |
2542
a9dec4763c6b
POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents:
2539
diff
changeset
|
270 |
return fields |
2432
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
271 |
|
2502
2e096acc8720
Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents:
2501
diff
changeset
|
272 |
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
|
273 |
tip='', comment=''): |
2432
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
274 |
"""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
|
275 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
276 |
params: |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
277 |
field: the current field |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
278 |
value: the initial value for this field |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
279 |
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
|
280 |
schema: schema for survey |
2432
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
281 |
req: required bool |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
282 |
label: label for field |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
283 |
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
|
284 |
comment: initial comment value for field |
2432
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
285 |
""" |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
286 |
|
2517
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
287 |
widget = widgets.Textarea(attrs=attrs) |
2432
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
288 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
289 |
if not tip: |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
290 |
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
|
291 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
292 |
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
|
293 |
widget=widget, initial=value) |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
294 |
|
2451
c58a7ea6c126
Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents:
2442
diff
changeset
|
295 |
self.survey_fields[field] = question |
2432
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
296 |
|
2468
0273c1a8d708
Fixed too long line in Surveys helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2465
diff
changeset
|
297 |
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
|
298 |
tip='', comment=''): |
2432
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
299 |
"""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
|
300 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
301 |
params: |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
302 |
field: the current field |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
303 |
value: the initial value for this field |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
304 |
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
|
305 |
schema: schema for survey |
2432
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
306 |
req: required bool |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
307 |
label: label for field |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
308 |
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
|
309 |
comment: initial comment value for field |
2432
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
310 |
""" |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
311 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
312 |
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
|
313 |
|
2517
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
314 |
widget = widgets.TextInput(attrs=attrs) |
2432
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
315 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
316 |
if not tip: |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
317 |
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
|
318 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
319 |
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
|
320 |
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
|
321 |
|
2432
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
322 |
self.survey_fields[field] = question |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
323 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
324 |
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
|
325 |
tip='', comment=''): |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
326 |
"""Add a selection field to this form. |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
327 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
328 |
params: |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
329 |
field: the current field |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
330 |
value: the initial value for this field |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
331 |
attrs: additional attributes for field |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
332 |
schema: schema for survey |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
333 |
req: required bool |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
334 |
label: label for field |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
335 |
tip: tooltip text for field |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
336 |
comment: initial comment value for field |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
337 |
""" |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
338 |
|
2517
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
339 |
widget = PickOneSelect(attrs) |
2432
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
340 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
341 |
these_choices = [] |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
342 |
# add all properties, but select chosen one |
2517
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
343 |
# TODO(ajaksu): this breaks ordering and blocks merging choice methods |
2432
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
344 |
options = getattr(self.survey_content, field) |
2533
941732c52b67
Fixed broken selection questions from refactoring SurveyForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2530
diff
changeset
|
345 |
if self.survey_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
|
346 |
these_choices.append((value, value)) |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
347 |
if value in options: |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
348 |
options.remove(value) |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
349 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
350 |
for option in options: |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
351 |
these_choices.append((option, option)) |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
352 |
if not tip: |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
353 |
tip = 'Please select an answer this question.' |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
354 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
355 |
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
|
356 |
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
|
357 |
|
2432
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
358 |
self.survey_fields[field] = question |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
359 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
360 |
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
|
361 |
tip='', comment=''): |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
362 |
"""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
|
363 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
364 |
params: |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
365 |
field: the current field |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
366 |
value: the initial value for this field |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
367 |
attrs: additional attributes for field |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
368 |
schema: schema for survey |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
369 |
req: required bool |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
370 |
label: label for field |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
371 |
tip: tooltip text for field |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
372 |
comment: initial comment value for field |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
373 |
""" |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
374 |
|
2517
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
375 |
widget = PickManyCheckbox(attrs) |
2432
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
376 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
377 |
# 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
|
378 |
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
|
379 |
# 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
|
380 |
value = value.split(',') |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
381 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
382 |
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
|
383 |
if not tip: |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
384 |
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
|
385 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
386 |
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
|
387 |
choices=tuple(these_choices), widget=widget, |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
388 |
initial=value) |
2451
c58a7ea6c126
Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents:
2442
diff
changeset
|
389 |
|
2432
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
390 |
self.survey_fields[field] = question |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
391 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
392 |
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
|
393 |
tip='', comment=''): |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
394 |
"""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
|
395 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
396 |
params: |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
397 |
field: the current field |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
398 |
value: the initial value for this field |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
399 |
attrs: additional attributes for field |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
400 |
schema: schema for survey |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
401 |
req: required bool |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
402 |
label: label for field |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
403 |
tip: tooltip text for field |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
404 |
comment: initial comment value for field |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
405 |
""" |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
406 |
|
2517
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
407 |
widget = PickQuantRadio(attrs) |
2432
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
408 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
409 |
if self.survey_record: |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
410 |
value = value |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
411 |
else: |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
412 |
value = None |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
413 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
414 |
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
|
415 |
if not tip: |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
416 |
tip = 'Please select one of these choices.' |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
417 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
418 |
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
|
419 |
choices=tuple(these_choices), widget=widget, |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
420 |
initial=value) |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
421 |
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
|
422 |
|
c58a7ea6c126
Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents:
2442
diff
changeset
|
423 |
def addCommentField(self, field, comment, attrs, tip): |
2520
859ada69db69
Style fixes in Survey helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2518
diff
changeset
|
424 |
"""Add comment field to a question. |
859ada69db69
Style fixes in Survey helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2518
diff
changeset
|
425 |
|
859ada69db69
Style fixes in Survey helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2518
diff
changeset
|
426 |
params: |
859ada69db69
Style fixes in Survey helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2518
diff
changeset
|
427 |
field: the name of the field to add the comment field to |
859ada69db69
Style fixes in Survey helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2518
diff
changeset
|
428 |
comment: the initial value of this field. |
859ada69db69
Style fixes in Survey helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2518
diff
changeset
|
429 |
attrs: the attrs for the widget |
859ada69db69
Style fixes in Survey helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2518
diff
changeset
|
430 |
tip: tooltip text for this field |
859ada69db69
Style fixes in Survey helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2518
diff
changeset
|
431 |
""" |
2526
8f29bfb9eb52
Survey editing/taking Javascript and CSS fixes.
James Levy <jamesalexanderlevy@gmail.com>
parents:
2520
diff
changeset
|
432 |
attrs['class'] = 'comment' |
2517
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
433 |
widget = widgets.Textarea(attrs=attrs) |
2539
dd0322a37e44
Ensure that long questions and comments are stored as db.Text.
Daniel Diniz <ajaksu@gmail.com>
parents:
2533
diff
changeset
|
434 |
comment_field = CharField(help_text=tip, required=False, |
2526
8f29bfb9eb52
Survey editing/taking Javascript and CSS fixes.
James Levy <jamesalexanderlevy@gmail.com>
parents:
2520
diff
changeset
|
435 |
label='Add a Comment (optional)', widget=widget, initial=comment) |
2517
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
436 |
self.survey_fields[COMMENT_PREFIX + field] = comment_field |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
437 |
|
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
438 |
|
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
439 |
class Meta(object): |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
440 |
model = SurveyContent |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
441 |
exclude = ['schema'] |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
442 |
|
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
443 |
|
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
444 |
class SurveyEditForm(djangoforms.ModelForm): |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
445 |
"""SurveyContent form for editing a survey. |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
446 |
|
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
447 |
This class is used to produce survey forms for several circumstances: |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
448 |
- Admin creating survey from scratch |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
449 |
- Admin updating existing survey |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
450 |
|
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
451 |
Using dynamic properties of the survey model (if passed as an arg) the |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
452 |
survey form is dynamically formed. |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
453 |
""" |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
454 |
|
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
455 |
def __init__(self, *args, **kwargs): |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
456 |
"""Store special kwargs as attributes. |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
457 |
|
2520
859ada69db69
Style fixes in Survey helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2518
diff
changeset
|
458 |
params: |
2517
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
459 |
survey_content: a SurveyContent entity. |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
460 |
survey_logic: an instance of SurveyLogic. |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
461 |
""" |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
462 |
|
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
463 |
self.kwargs = kwargs |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
464 |
self.survey_content = self.kwargs.pop('survey_content', None) |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
465 |
self.survey_logic = self.kwargs.pop('survey_logic', None) |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
466 |
|
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
467 |
super(SurveyEditForm, self).__init__(*args, **self.kwargs) |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
468 |
|
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
469 |
def getFields(self): |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
470 |
"""Build the SurveyContent (questions) form fields. |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
471 |
|
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
472 |
params: |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
473 |
post_dict: dict with POST data that will be used for validation |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
474 |
|
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
475 |
Populates self.survey_fields, which will be ordered in self.insert_fields. |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
476 |
Also populates self.data, which will be used in form validation. |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
477 |
""" |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
478 |
|
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
479 |
if not self.survey_content: |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
480 |
return |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
481 |
|
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
482 |
self.survey_fields = {} |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
483 |
schema = SurveyContentSchema(self.survey_content.schema) |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
484 |
extra_attrs = {} |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
485 |
|
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
486 |
# add unordered fields to self.survey_fields |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
487 |
for field in self.survey_content.dynamic_properties(): |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
488 |
|
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
489 |
# use prompts set by survey creator |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
490 |
value = getattr(self.survey_content, field) |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
491 |
from_content = True |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
492 |
|
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
493 |
label = schema.getLabel(field) |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
494 |
if label is None: |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
495 |
continue |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
496 |
|
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
497 |
tip = 'Please provide an answer to this question.' |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
498 |
kwargs = schema.getEditFieldArgs(field, value, tip, label) |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
499 |
|
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
500 |
kwargs['widget'] = schema.getEditWidget(field, extra_attrs) |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
501 |
|
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
502 |
# add new field |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
503 |
self.survey_fields[field] = schema.getEditField(field)(**kwargs) |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
504 |
|
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
505 |
# TODO(ajaksu): find a new way to keep fields in order |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
506 |
return self.insertFields() |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
507 |
|
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
508 |
def insertFields(self): |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
509 |
"""Add ordered fields to self.fields. |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
510 |
""" |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
511 |
|
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
512 |
survey_order = self.survey_content.getSurveyOrder() |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
513 |
|
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
514 |
# insert dynamic survey fields |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
515 |
for position, property in survey_order.items(): |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
516 |
self.fields.insert(position, property, self.survey_fields[property]) |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
517 |
|
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
518 |
return self.fields |
2432
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 Meta(object): |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
521 |
model = SurveyContent |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
522 |
exclude = ['schema'] |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
523 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
524 |
|
2451
c58a7ea6c126
Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents:
2442
diff
changeset
|
525 |
class SurveyContentSchema(object): |
c58a7ea6c126
Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents:
2442
diff
changeset
|
526 |
"""Abstract question metadata handling. |
c58a7ea6c126
Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents:
2442
diff
changeset
|
527 |
""" |
c58a7ea6c126
Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents:
2442
diff
changeset
|
528 |
|
c58a7ea6c126
Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents:
2442
diff
changeset
|
529 |
def __init__(self, schema): |
2517
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
530 |
"""Set the dictionary that this class encapsulates. |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
531 |
|
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
532 |
Args: |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
533 |
schema: schema as stored in SurveyConent entity |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
534 |
""" |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
535 |
|
2451
c58a7ea6c126
Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents:
2442
diff
changeset
|
536 |
self.schema = eval(schema) |
c58a7ea6c126
Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents:
2442
diff
changeset
|
537 |
|
c58a7ea6c126
Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents:
2442
diff
changeset
|
538 |
def getType(self, field): |
2517
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
539 |
"""Fetch question type for field e.g. short_answer, pick_multi, etc. |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
540 |
|
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
541 |
Args: |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
542 |
field: name of the field to get the type from |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
543 |
""" |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
544 |
|
2451
c58a7ea6c126
Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents:
2442
diff
changeset
|
545 |
return self.schema[field]["type"] |
c58a7ea6c126
Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents:
2442
diff
changeset
|
546 |
|
2502
2e096acc8720
Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents:
2501
diff
changeset
|
547 |
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
|
548 |
"""Check whether survey question is required. |
2517
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
549 |
|
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
550 |
Args: |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
551 |
field: name of the field to check the required property for |
2502
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 |
|
2e096acc8720
Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents:
2501
diff
changeset
|
554 |
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
|
555 |
|
2e096acc8720
Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents:
2501
diff
changeset
|
556 |
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
|
557 |
"""Check whether survey question allows adding a comment. |
2517
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
558 |
|
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
559 |
Args: |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
560 |
field: name of the field to get the hasComment property for |
2502
2e096acc8720
Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents:
2501
diff
changeset
|
561 |
""" |
2e096acc8720
Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents:
2501
diff
changeset
|
562 |
|
2e096acc8720
Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents:
2501
diff
changeset
|
563 |
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
|
564 |
|
2451
c58a7ea6c126
Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents:
2442
diff
changeset
|
565 |
def getRender(self, field): |
2517
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
566 |
"""Get rendering options for choice questions. |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
567 |
|
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
568 |
Args: |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
569 |
field: name of the field to get the rendering option for |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
570 |
""" |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
571 |
|
2451
c58a7ea6c126
Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents:
2442
diff
changeset
|
572 |
return self.schema[field]["render"] |
c58a7ea6c126
Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents:
2442
diff
changeset
|
573 |
|
2517
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
574 |
def getEditField(self, field): |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
575 |
"""For a given question kind, get the correct edit view field. |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
576 |
""" |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
577 |
|
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
578 |
kind = self.getType(field) |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
579 |
if kind in CHOICE_TYPES: |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
580 |
Field = PickOneField |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
581 |
else: |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
582 |
Field = CharField |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
583 |
|
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
584 |
return Field |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
585 |
|
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
586 |
def getEditFieldArgs(self, field, value, tip, label): |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
587 |
"""Build edit view field arguments. |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
588 |
|
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
589 |
params: |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
590 |
field: field name |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
591 |
value: field value (text for text questions, list for choice questions) |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
592 |
tipe: help text, to be used in a tooltip |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
593 |
label: the field's question (or identifier if question is missing) |
2451
c58a7ea6c126
Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents:
2442
diff
changeset
|
594 |
""" |
c58a7ea6c126
Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents:
2442
diff
changeset
|
595 |
|
2517
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
596 |
kind = self.getType(field) |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
597 |
|
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
598 |
kwargs = dict(help_text=tip, required=False, label=label) |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
599 |
|
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
600 |
if kind in CHOICE_TYPES: |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
601 |
kwargs['choices'] = tuple([(val, val) for val in value]) |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
602 |
else: |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
603 |
kwargs['initial'] = value |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
604 |
|
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
605 |
return kwargs |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
606 |
|
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
607 |
def getEditWidget(self, field, attrs): |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
608 |
"""Get survey editing widget for questions. |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
609 |
""" |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
610 |
|
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
611 |
kind = self.getType(field) |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
612 |
is_required = self.getRequired(field) |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
613 |
has_comment = self.getHasComment(field) |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
614 |
|
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
615 |
if kind in CHOICE_TYPES: |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
616 |
widget = UniversalChoiceEditor |
2451
c58a7ea6c126
Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents:
2442
diff
changeset
|
617 |
render = self.getRender(field) |
2517
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
618 |
args = 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
|
619 |
else: |
2517
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
620 |
args = is_required, has_comment |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
621 |
if kind == 'long_answer': |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
622 |
attrs['class'] = "text_question" |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
623 |
widget = LongTextarea |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
624 |
elif kind == 'short_answer': |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
625 |
widget = ShortTextInput |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
626 |
|
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
627 |
kwargs = dict(attrs=attrs) |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
628 |
|
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
629 |
return widget(*args, **kwargs) |
2451
c58a7ea6c126
Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents:
2442
diff
changeset
|
630 |
|
c58a7ea6c126
Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents:
2442
diff
changeset
|
631 |
def getLabel(self, field): |
c58a7ea6c126
Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents:
2442
diff
changeset
|
632 |
"""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
|
633 |
""" |
c58a7ea6c126
Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents:
2442
diff
changeset
|
634 |
|
c58a7ea6c126
Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents:
2442
diff
changeset
|
635 |
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
|
636 |
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
|
637 |
(field, str(self.schema))) |
c58a7ea6c126
Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents:
2442
diff
changeset
|
638 |
return |
c58a7ea6c126
Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents:
2442
diff
changeset
|
639 |
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
|
640 |
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
|
641 |
else: |
c58a7ea6c126
Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents:
2442
diff
changeset
|
642 |
label = field |
c58a7ea6c126
Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents:
2442
diff
changeset
|
643 |
return label |
c58a7ea6c126
Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents:
2442
diff
changeset
|
644 |
|
c58a7ea6c126
Added SurveyContentSchema to improve readability for the survey helper.
Daniel Diniz <ajaksu@gmail.com>
parents:
2442
diff
changeset
|
645 |
|
2432
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
646 |
class UniversalChoiceEditor(widgets.Widget): |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
647 |
"""Edit interface for choice questions. |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
648 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
649 |
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
|
650 |
""" |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
651 |
|
2502
2e096acc8720
Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents:
2501
diff
changeset
|
652 |
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
|
653 |
choices=()): |
2e096acc8720
Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents:
2501
diff
changeset
|
654 |
""" |
2e096acc8720
Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents:
2501
diff
changeset
|
655 |
params: |
2e096acc8720
Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents:
2501
diff
changeset
|
656 |
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
|
657 |
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
|
658 |
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
|
659 |
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
|
660 |
""" |
2432
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
661 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
662 |
self.attrs = attrs or {} |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
663 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
664 |
# 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
|
665 |
# 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
|
666 |
# more than once. |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
667 |
self.choices = list(choices) |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
668 |
self.kind = kind |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
669 |
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
|
670 |
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
|
671 |
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
|
672 |
|
2432
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
673 |
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
|
674 |
"""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
|
675 |
|
2e096acc8720
Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents:
2501
diff
changeset
|
676 |
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
|
677 |
""" |
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 |
if value is None: |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
680 |
value = '' |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
681 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
682 |
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
|
683 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
684 |
# 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
|
685 |
selected = 'selected="selected"' |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
686 |
context = dict( |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
687 |
name=name, |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
688 |
is_selection=selected * (self.kind == 'selection'), |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
689 |
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
|
690 |
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
|
691 |
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
|
692 |
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
|
693 |
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
|
694 |
) |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
695 |
|
2502
2e096acc8720
Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents:
2501
diff
changeset
|
696 |
# 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
|
697 |
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
|
698 |
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
|
699 |
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
|
700 |
)) |
2e096acc8720
Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents:
2501
diff
changeset
|
701 |
|
2432
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
702 |
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
|
703 |
chained_choices = enumerate(chain(self.choices, choices)) |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
704 |
choices = {} |
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 |
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
|
707 |
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
|
708 |
choices[i] = option_value |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
709 |
context['choices'] = choices |
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 |
template = 'soc/survey/universal_choice_editor.html' |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
712 |
return loader.render_to_string(template, context) |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
713 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
714 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
715 |
class PickOneField(forms.ChoiceField): |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
716 |
"""Stub for customizing the single choice field. |
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 |
#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
|
719 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
720 |
def __init__(self, *args, **kwargs): |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
721 |
super(PickOneField, self).__init__(*args, **kwargs) |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
722 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
723 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
724 |
class PickManyField(forms.MultipleChoiceField): |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
725 |
"""Stub for customizing the multiple choice field. |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
726 |
""" |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
727 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
728 |
def __init__(self, *args, **kwargs): |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
729 |
super(PickManyField, self).__init__(*args, **kwargs) |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
730 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
731 |
|
2530
d7a0ab3f1965
Make sure that radio button questions validate correctly and allow only one choice.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2527
diff
changeset
|
732 |
class PickQuantField(forms.ChoiceField): |
d7a0ab3f1965
Make sure that radio button questions validate correctly and allow only one choice.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2527
diff
changeset
|
733 |
"""Stub for customizing the choice field. |
2432
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
734 |
""" |
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 |
def __init__(self, *args, **kwargs): |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
737 |
super(PickQuantField, self).__init__(*args, **kwargs) |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
738 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
739 |
|
2502
2e096acc8720
Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents:
2501
diff
changeset
|
740 |
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
|
741 |
"""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
|
742 |
""" |
2e096acc8720
Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents:
2501
diff
changeset
|
743 |
|
2517
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
744 |
def __init__(self, is_required, has_comment, attrs=None): |
2502
2e096acc8720
Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents:
2501
diff
changeset
|
745 |
"""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
|
746 |
|
2e096acc8720
Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents:
2501
diff
changeset
|
747 |
params: |
2e096acc8720
Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents:
2501
diff
changeset
|
748 |
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
|
749 |
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
|
750 |
""" |
2e096acc8720
Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents:
2501
diff
changeset
|
751 |
|
2e096acc8720
Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents:
2501
diff
changeset
|
752 |
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
|
753 |
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
|
754 |
|
2e096acc8720
Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents:
2501
diff
changeset
|
755 |
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
|
756 |
|
2e096acc8720
Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents:
2501
diff
changeset
|
757 |
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
|
758 |
"""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
|
759 |
|
2e096acc8720
Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents:
2501
diff
changeset
|
760 |
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
|
761 |
""" |
2e096acc8720
Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents:
2501
diff
changeset
|
762 |
|
2e096acc8720
Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents:
2501
diff
changeset
|
763 |
# 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
|
764 |
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
|
765 |
|
2517
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
766 |
# add 'required' and 'has_comment' fields |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
767 |
context = dict(name=name, is_required=self.is_required, |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
768 |
has_comment=self.has_comment) |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
769 |
template = loader.get_template_from_string(REQUIRED_COMMENT_TPL) |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
770 |
rendered = template.render(context=loader.Context(dict_=context)) |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
771 |
output = rendered + output |
2502
2e096acc8720
Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents:
2501
diff
changeset
|
772 |
|
2517
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
773 |
output = '<fieldset>' + output + '</fieldset>' |
2502
2e096acc8720
Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents:
2501
diff
changeset
|
774 |
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
|
775 |
|
2e096acc8720
Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents:
2501
diff
changeset
|
776 |
|
2e096acc8720
Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents:
2501
diff
changeset
|
777 |
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
|
778 |
"""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
|
779 |
""" |
2e096acc8720
Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents:
2501
diff
changeset
|
780 |
|
2517
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
781 |
def __init__(self, is_required, has_comment, attrs=None): |
2502
2e096acc8720
Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents:
2501
diff
changeset
|
782 |
"""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
|
783 |
|
2e096acc8720
Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents:
2501
diff
changeset
|
784 |
params: |
2e096acc8720
Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents:
2501
diff
changeset
|
785 |
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
|
786 |
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
|
787 |
""" |
2e096acc8720
Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents:
2501
diff
changeset
|
788 |
|
2e096acc8720
Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents:
2501
diff
changeset
|
789 |
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
|
790 |
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
|
791 |
|
2e096acc8720
Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents:
2501
diff
changeset
|
792 |
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
|
793 |
|
2e096acc8720
Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents:
2501
diff
changeset
|
794 |
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
|
795 |
"""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
|
796 |
|
2e096acc8720
Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents:
2501
diff
changeset
|
797 |
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
|
798 |
""" |
2e096acc8720
Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents:
2501
diff
changeset
|
799 |
|
2e096acc8720
Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents:
2501
diff
changeset
|
800 |
# 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
|
801 |
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
|
802 |
|
2517
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
803 |
# add 'required' and 'has_comment' fields |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
804 |
context = dict(name=name, is_required=self.is_required, |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
805 |
has_comment=self.has_comment) |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
806 |
template = loader.get_template_from_string(REQUIRED_COMMENT_TPL) |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
807 |
rendered = template.render(context=loader.Context(dict_=context)) |
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
808 |
output = rendered + output |
2502
2e096acc8720
Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents:
2501
diff
changeset
|
809 |
|
2517
97117d341f62
Split SurveyForm into SurveyTakeForm and SurveyEditForm.
Daniel Diniz <ajaksu@gmail.com>
parents:
2507
diff
changeset
|
810 |
output = '<fieldset>' + output + '</fieldset>' |
2502
2e096acc8720
Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents:
2501
diff
changeset
|
811 |
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
|
812 |
|
2e096acc8720
Surveys can now have required questions and comments can be turned on/off.
Daniel Diniz <ajaksu@gmail.com>
parents:
2501
diff
changeset
|
813 |
|
2432
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
814 |
class PickOneSelect(forms.Select): |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
815 |
"""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
|
816 |
""" |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
817 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
818 |
def __init__(self, *args, **kwargs): |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
819 |
super(PickOneSelect, self).__init__(*args, **kwargs) |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
820 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
821 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
822 |
class PickManyCheckbox(forms.CheckboxSelectMultiple): |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
823 |
"""Customized multiple choice checkbox widget. |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
824 |
""" |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
825 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
826 |
def __init__(self, *args, **kwargs): |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
827 |
super(PickManyCheckbox, self).__init__(*args, **kwargs) |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
828 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
829 |
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
|
830 |
"""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
|
831 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
832 |
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
|
833 |
""" |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
834 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
835 |
if value is None: |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
836 |
value = [] |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
837 |
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
|
838 |
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
|
839 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
840 |
# normalize to strings. |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
841 |
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
|
842 |
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
|
843 |
smart_unicode = forms.util.smart_unicode |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
844 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
845 |
# set container fieldset and list |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
846 |
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
|
847 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
848 |
# add numbered checkboxes wrapped in list items |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
849 |
chained_choices = enumerate(chain(self.choices, choices)) |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
850 |
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
|
851 |
option_label = escape(smart_unicode(option_label)) |
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 |
# 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
|
854 |
# 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
|
855 |
if has_id: |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
856 |
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
|
857 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
858 |
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
|
859 |
rendered_cb = cb.render(name, option_value) |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
860 |
cb_label = (rendered_cb, option_label) |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
861 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
862 |
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
|
863 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
864 |
output.append(u' </ul>\n</fieldset>') |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
865 |
return u'\n'.join(output) |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
866 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
867 |
def id_for_label(self, id_): |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
868 |
# 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
|
869 |
if id_: |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
870 |
id_ += '_fieldset' |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
871 |
return id_ |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
872 |
id_for_label = classmethod(id_for_label) |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
873 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
874 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
875 |
class PickQuantRadioRenderer(widgets.RadioFieldRenderer): |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
876 |
"""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
|
877 |
""" |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
878 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
879 |
def __init__(self, *args, **kwargs): |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
880 |
super(PickQuantRadioRenderer, self).__init__(*args, **kwargs) |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
881 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
882 |
def render(self): |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
883 |
"""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
|
884 |
""" |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
885 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
886 |
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
|
887 |
% 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
|
888 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
889 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
890 |
class PickQuantRadio(forms.RadioSelect): |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
891 |
"""TODO(James,Ajaksu) Fix Docstring |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
892 |
""" |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
893 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
894 |
renderer = PickQuantRadioRenderer |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
895 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
896 |
def __init__(self, *args, **kwargs): |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
897 |
super(PickQuantRadio, self).__init__(*args, **kwargs) |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
898 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
899 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
900 |
# 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
|
901 |
WIDGETS = {'multi_checkbox': PickManyCheckbox, |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
902 |
'single_select': PickOneSelect, |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
903 |
'quant_radio': PickQuantRadio} |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
904 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
905 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
906 |
class SurveyResults(widgets.Widget): |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
907 |
"""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
|
908 |
""" |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
909 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
910 |
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
|
911 |
order=[], idx=0, context={}): |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
912 |
""" renders list of survey results |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
913 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
914 |
params: |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
915 |
survey: current survey |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
916 |
params: dict of params for rendering list |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
917 |
filter: filter for list results |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
918 |
limit: limit for list results |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
919 |
offset: offset for list results |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
920 |
order: order for list results |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
921 |
idx: index for list results |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
922 |
context: context dict for template |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
923 |
""" |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
924 |
|
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
|
925 |
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
|
926 |
record_logic = survey_logic.getRecordLogic() |
2432
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
927 |
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
|
928 |
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
|
929 |
order=order) |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
930 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
931 |
params['name'] = "Survey Results" |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
932 |
content = { |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
933 |
'idx': idx, |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
934 |
'data': data, |
2465
037a7e79a635
Use an existing logic for showing Survey Results.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2463
diff
changeset
|
935 |
'logic': record_logic, |
2432
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
936 |
'limit': limit, |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
937 |
} |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
938 |
updates = dicts.rename(params, params['list_params']) |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
939 |
content.update(updates) |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
940 |
contents = [content] |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
941 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
942 |
if len(content) == 1: |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
943 |
content = content[0] |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
944 |
key_order = content.get('key_order') |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
945 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
946 |
context['list'] = Lists(contents) |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
947 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
948 |
# 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
|
949 |
for list_ in context['list']._contents: |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
950 |
if len(list_['data']) < 1: |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
951 |
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
|
952 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
953 |
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
|
954 |
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
|
955 |
list_['description'] = 'Survey Results:' |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
956 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
957 |
context['properties'] = survey.survey_content.orderedProperties() |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
958 |
context['entity_type'] = "Survey Results" |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
959 |
context['entity_type_plural'] = "Results" |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
960 |
context['no_lists_msg'] = "No Survey Results" |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
961 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
962 |
path = (survey.entity_type().lower(), survey.prefix, |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
963 |
survey.scope_path, survey.link_id) |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
964 |
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
|
965 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
966 |
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
|
967 |
dictionary=context).strip('\n') |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
968 |
return markup |
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
969 |
|
636dfd5381c2
Added Survey Views Helper for rendering several widgets.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
970 |
|
2478
985fd974e095
Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents:
2470
diff
changeset
|
971 |
class HelperForm(object): |
985fd974e095
Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents:
2470
diff
changeset
|
972 |
"""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
|
973 |
""" |
985fd974e095
Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents:
2470
diff
changeset
|
974 |
|
985fd974e095
Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents:
2470
diff
changeset
|
975 |
def __init__(self, form=None): |
985fd974e095
Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents:
2470
diff
changeset
|
976 |
"""Store the edit_form. |
985fd974e095
Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents:
2470
diff
changeset
|
977 |
""" |
985fd974e095
Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents:
2470
diff
changeset
|
978 |
|
985fd974e095
Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents:
2470
diff
changeset
|
979 |
self.form = form |
985fd974e095
Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents:
2470
diff
changeset
|
980 |
|
985fd974e095
Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents:
2470
diff
changeset
|
981 |
def __call__(self, instance=None): |
985fd974e095
Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents:
2470
diff
changeset
|
982 |
"""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
|
983 |
""" |
985fd974e095
Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents:
2470
diff
changeset
|
984 |
|
985fd974e095
Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents:
2470
diff
changeset
|
985 |
form = self.form(instance=instance) |
985fd974e095
Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents:
2470
diff
changeset
|
986 |
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
|
987 |
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
|
988 |
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
|
989 |
return form |
985fd974e095
Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents:
2470
diff
changeset
|
990 |
|
985fd974e095
Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents:
2470
diff
changeset
|
991 |
|
985fd974e095
Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents:
2470
diff
changeset
|
992 |
def _get_csv_header(sur): |
985fd974e095
Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents:
2470
diff
changeset
|
993 |
"""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
|
994 |
|
985fd974e095
Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents:
2470
diff
changeset
|
995 |
Args: |
985fd974e095
Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents:
2470
diff
changeset
|
996 |
sur: Survey entity |
985fd974e095
Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents:
2470
diff
changeset
|
997 |
""" |
985fd974e095
Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents:
2470
diff
changeset
|
998 |
|
985fd974e095
Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents:
2470
diff
changeset
|
999 |
tpl = '# %s: %s\n' |
985fd974e095
Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents:
2470
diff
changeset
|
1000 |
|
985fd974e095
Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents:
2470
diff
changeset
|
1001 |
# add static properties |
985fd974e095
Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents:
2470
diff
changeset
|
1002 |
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
|
1003 |
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
|
1004 |
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
|
1005 |
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
|
1006 |
fields.sort() |
985fd974e095
Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents:
2470
diff
changeset
|
1007 |
|
985fd974e095
Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents:
2470
diff
changeset
|
1008 |
# add dynamic properties |
985fd974e095
Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents:
2470
diff
changeset
|
1009 |
fields += ['#\n#---\n#\n'] |
985fd974e095
Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents:
2470
diff
changeset
|
1010 |
dynamic = sur.survey_content.dynamic_properties() |
985fd974e095
Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents:
2470
diff
changeset
|
1011 |
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
|
1012 |
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
|
1013 |
|
985fd974e095
Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents:
2470
diff
changeset
|
1014 |
# add schema |
985fd974e095
Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents:
2470
diff
changeset
|
1015 |
fields += ['#\n#---\n#\n'] |
985fd974e095
Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents:
2470
diff
changeset
|
1016 |
schema = sur.survey_content.schema |
985fd974e095
Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents:
2470
diff
changeset
|
1017 |
indent = '},\n#' + ' ' * 9 |
985fd974e095
Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents:
2470
diff
changeset
|
1018 |
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
|
1019 |
|
985fd974e095
Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents:
2470
diff
changeset
|
1020 |
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
|
1021 |
|
985fd974e095
Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents:
2470
diff
changeset
|
1022 |
|
985fd974e095
Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents:
2470
diff
changeset
|
1023 |
def _get_records(recs, props): |
985fd974e095
Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents:
2470
diff
changeset
|
1024 |
"""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
|
1025 |
""" |
985fd974e095
Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents:
2470
diff
changeset
|
1026 |
|
985fd974e095
Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents:
2470
diff
changeset
|
1027 |
records = [] |
985fd974e095
Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents:
2470
diff
changeset
|
1028 |
props = props[1:] |
985fd974e095
Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents:
2470
diff
changeset
|
1029 |
for rec in recs: |
985fd974e095
Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents:
2470
diff
changeset
|
1030 |
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
|
1031 |
leading = (rec.user.link_id,) |
985fd974e095
Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents:
2470
diff
changeset
|
1032 |
records.append(leading + values) |
985fd974e095
Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents:
2470
diff
changeset
|
1033 |
return records |
985fd974e095
Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents:
2470
diff
changeset
|
1034 |
|
985fd974e095
Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents:
2470
diff
changeset
|
1035 |
|
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
|
1036 |
def to_csv(survey_view): |
2478
985fd974e095
Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents:
2470
diff
changeset
|
1037 |
"""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
|
1038 |
|
6eac6cd88dad
Made the CSV exporter for Survey not rely on the BackReferenceProperty in Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2489
diff
changeset
|
1039 |
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
|
1040 |
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
|
1041 |
""" |
985fd974e095
Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents:
2470
diff
changeset
|
1042 |
|
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
|
1043 |
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
|
1044 |
"""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
|
1045 |
""" |
6eac6cd88dad
Made the CSV exporter for Survey not rely on the BackReferenceProperty in Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2489
diff
changeset
|
1046 |
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
|
1047 |
record_logic = survey_logic.getRecordLogic() |
2478
985fd974e095
Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents:
2470
diff
changeset
|
1048 |
|
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
|
1049 |
# 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
|
1050 |
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
|
1051 |
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
|
1052 |
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
|
1053 |
|
6eac6cd88dad
Made the CSV exporter for Survey not rely on the BackReferenceProperty in Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2489
diff
changeset
|
1054 |
# 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
|
1055 |
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
|
1056 |
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
|
1057 |
|
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
|
1058 |
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
|
1059 |
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
|
1060 |
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
|
1061 |
# 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
|
1062 |
return header, survey.link_id |
2501
d612b48e6e12
Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents:
2492
diff
changeset
|
1063 |
|
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
|
1064 |
# 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
|
1065 |
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
|
1066 |
recs = _get_records(recs, properties) |
2501
d612b48e6e12
Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents:
2492
diff
changeset
|
1067 |
|
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
|
1068 |
# 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
|
1069 |
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
|
1070 |
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
|
1071 |
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
|
1072 |
writer.writerows(recs) |
2501
d612b48e6e12
Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents:
2492
diff
changeset
|
1073 |
|
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
|
1074 |
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
|
1075 |
return wrapper |