app/soc/views/models/survey.py
author Daniel Diniz <ajaksu@gmail.com>
Fri, 03 Jul 2009 14:19:23 +0200
changeset 2501 d612b48e6e12
parent 2497 996f381d458c
child 2502 2e096acc8720
permissions -rw-r--r--
Added Survey From Field validation. Also added the COMMENT_PREFIX in Survey model to ensure that comments are stored in the same way everwhere. Reviewed by: Lennard de Rijk
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
     2
#
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
     3
# Copyright 2009 the Melange authors.
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
     4
#
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
     8
#
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    10
#
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    15
# limitations under the License.
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    16
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    17
"""Views for Surveys.
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    18
"""
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    19
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    20
__authors__ = [
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    21
  '"Daniel Diniz" <ajaksu@gmail.com>',
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    22
  '"James Levy" <jamesalexanderlevy@gmail.com>',
2463
05af53239799 Add more flexibility to the Survey helper for use with the new Survey types.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2455
diff changeset
    23
  '"Lennard de Rijk" <ljvderijk@gmail.com>',
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    24
  ]
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    25
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    26
import datetime
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    27
import re
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    28
import string
2475
dfb9249d91b6 Added skeleton for the take view and its template.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2474
diff changeset
    29
dfb9249d91b6 Added skeleton for the take view and its template.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2474
diff changeset
    30
from google.appengine.ext import db
dfb9249d91b6 Added skeleton for the take view and its template.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2474
diff changeset
    31
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    32
from django import forms
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    33
from django import http
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    34
from django.utils import simplejson
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    35
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    36
from soc.logic import cleaning
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    37
from soc.logic import dicts
2455
a023b71ce125 fixed critical errors related to survey subclassing and renaming of deadline and opening properties, and surveys in sidebar are now marked as such and have a red color if untaken
james@jamess-macbook-air.local
parents: 2448
diff changeset
    38
from soc.logic.models.survey import logic as survey_logic
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    39
from soc.logic.models.user import logic as user_logic
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    40
from soc.models.survey import Survey
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    41
from soc.models.survey_record import SurveyRecord
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    42
from soc.models.user import User
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    43
from soc.views import out_of_band
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    44
from soc.views.helper import access
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    45
from soc.views.helper import decorators
2501
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2497
diff changeset
    46
from soc.views.helper import forms as forms_helper
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    47
from soc.views.helper import redirects
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    48
from soc.views.helper import responses
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    49
from soc.views.helper import surveys
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    50
from soc.views.helper import widgets
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    51
from soc.views.models import base
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    52
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    53
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    54
CHOICE_TYPES = set(('selection', 'pick_multi', 'choice', 'pick_quant'))
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    55
TEXT_TYPES = set(('long_answer', 'short_answer'))
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    56
PROPERTY_TYPES = tuple(CHOICE_TYPES) + tuple(TEXT_TYPES)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    57
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    58
_short_answer = ("Short Answer",
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    59
                "Less than 40 characters. Rendered as a text input. "
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    60
                "It's possible to add a free form question (Content) "
2476
c27c21beba35 Fixed multiple typos in Survey tooltips strings.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2475
diff changeset
    61
                "and a in-input prompt/example text.")
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    62
_choice = ("Selection",
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    63
           "Can be set as a single choice (selection) or multiple choice "
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    64
           "(pick_multi) question. Rendered as a select (single choice) "
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    65
           "or a group of checkboxes (multiple choice). It's possible to "
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    66
           "add a free form question (Content) and as many free form options "
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    67
           "as wanted. Each option can be edited (double-click), deleted "
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    68
           "(click on (-) button) or reordered (drag and drop).")
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    69
_long_answer = ("Long Answer",
2476
c27c21beba35 Fixed multiple typos in Survey tooltips strings.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2475
diff changeset
    70
                "Unlimited length, auto-growing field. Rendered as a textarea. "
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    71
                 "It's possible to add a free form question (Content) and "
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    72
                 "an in-input prompt/example text.")
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    73
QUESTION_TYPES = dict(short_answer=_short_answer, long_answer=_long_answer,
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    74
                      choice=_choice)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    75
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    76
# for to_csv and View.exportSerialized
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    77
FIELDS = 'author modified_by'
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    78
PLAIN = 'is_featured content created modified'
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    79
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    80
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    81
class View(base.View):
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    82
  """View methods for the Survey model.
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    83
  """
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    84
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    85
  def __init__(self, params=None):
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    86
    """Defines the fields and methods required for the base View class
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    87
    to provide the user with list, public, create, edit and delete views.
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    88
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    89
    Params:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    90
      params: a dict with params for this View
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    91
    """
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    92
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    93
    # TODO: read/write access needs to match survey
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    94
    # TODO: usage requirements
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    95
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    96
    rights = access.Checker(params)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    97
    rights['any_access'] = ['allow']
2473
5cf0625dfa73 Changed the Survey public view to give a preview of the Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2472
diff changeset
    98
    rights['show'] = [('checkIsSurveyWritable', survey_logic)]
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    99
    rights['create'] = ['checkIsUser']
2471
04f13163c1e8 Brown paper bag "without tea" morning typo fix in Survey View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2469
diff changeset
   100
    rights['edit'] = [('checkIsSurveyWritable', survey_logic)]
2473
5cf0625dfa73 Changed the Survey public view to give a preview of the Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2472
diff changeset
   101
    rights['delete'] = ['checkIsDeveloper'] # TODO: fix deletion of Surveys
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   102
    rights['list'] = ['checkDocumentList']
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   103
    rights['pick'] = ['checkDocumentPick']
2475
dfb9249d91b6 Added skeleton for the take view and its template.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2474
diff changeset
   104
    rights['take'] = ['checkIsDeveloper'] # TODO(ljvderijk) test proper check
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   105
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   106
    new_params = {}
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   107
    new_params['logic'] = survey_logic
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   108
    new_params['rights'] = rights
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   109
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   110
    new_params['name'] = "Survey"
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   111
    new_params['pickable'] = True
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   112
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   113
    new_params['extra_django_patterns'] = [
2475
dfb9249d91b6 Added skeleton for the take view and its template.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2474
diff changeset
   114
         (r'^%(url_name)s/(?P<access_type>take)/%(key_fields)s$',
dfb9249d91b6 Added skeleton for the take view and its template.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2474
diff changeset
   115
         'soc.views.models.%(module_name)s.take',
2485
4f4e4fdc283d Fixed typo in title of Survey Take page.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2484
diff changeset
   116
         'Take %(name)s'),
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   117
         (r'^%(url_name)s/(?P<access_type>json)/%(scope)s$',
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   118
         'soc.views.models.%(module_name)s.json',
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   119
         'Export %(name)s as JSON'),
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   120
        (r'^%(url_name)s/(?P<access_type>results)/%(scope)s$',
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   121
         'soc.views.models.%(module_name)s.results',
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   122
         'View survey results for %(name)s'),
2455
a023b71ce125 fixed critical errors related to survey subclassing and renaming of deadline and opening properties, and surveys in sidebar are now marked as such and have a red color if untaken
james@jamess-macbook-air.local
parents: 2448
diff changeset
   123
        (r'^%(url_name)s/(?P<access_type>show)/user/(?P<link_id>)\w+$',
a023b71ce125 fixed critical errors related to survey subclassing and renaming of deadline and opening properties, and surveys in sidebar are now marked as such and have a red color if untaken
james@jamess-macbook-air.local
parents: 2448
diff changeset
   124
         'soc.views.models.%(module_name)s.results',
a023b71ce125 fixed critical errors related to survey subclassing and renaming of deadline and opening properties, and surveys in sidebar are now marked as such and have a red color if untaken
james@jamess-macbook-air.local
parents: 2448
diff changeset
   125
         'View survey results for user'),
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   126
        ]
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   127
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   128
    new_params['export_content_type'] = 'text/text'
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   129
    new_params['export_extension'] = '.csv'
2492
6eac6cd88dad Made the CSV exporter for Survey not rely on the BackReferenceProperty in Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2486
diff changeset
   130
    new_params['export_function'] = surveys.to_csv(self)
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   131
    new_params['delete_redirect'] = '/'
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   132
    new_params['list_key_order'] = [
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   133
        'link_id', 'scope_path', 'name', 'short_name', 'title',
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   134
        'content', 'prefix','read_access','write_access']
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   135
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   136
    new_params['edit_template'] = 'soc/survey/edit.html'
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   137
    new_params['create_template'] = 'soc/survey/edit.html'
2478
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2477
diff changeset
   138
    new_params['public_template'] = 'soc/survey/public.html'
2475
dfb9249d91b6 Added skeleton for the take view and its template.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2474
diff changeset
   139
    new_params['take_template'] = 'soc/survey/take.html'
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   140
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   141
    # TODO which one of these are leftovers from Document?
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   142
    new_params['no_create_raw'] = True
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   143
    new_params['no_create_with_scope'] = True
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   144
    new_params['no_create_with_key_fields'] = True
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   145
    new_params['no_list_raw'] = True
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   146
    new_params['sans_link_id_create'] = True
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   147
    new_params['sans_link_id_list'] = True
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   148
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   149
    new_params['create_dynafields'] = [
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   150
        {'name': 'link_id',
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   151
         'base': forms.fields.CharField,
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   152
         'label': 'Survey Link ID',
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   153
         },
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   154
        ]
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   155
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   156
    # survey_html: save form content if POST fails, so fields remain in UI
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   157
    new_params['create_extra_dynaproperties'] = {
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   158
        #'survey_content': forms.fields.CharField(widget=surveys.EditSurvey(),
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   159
                                                 #required=False),
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   160
        'survey_html': forms.fields.CharField(widget=forms.HiddenInput,
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   161
                                              required=False),
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   162
        'scope_path': forms.fields.CharField(widget=forms.HiddenInput,
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   163
                                             required=True),
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   164
        'prefix': forms.fields.CharField(widget=widgets.ReadOnlyInput(),
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   165
                                        required=True),
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   166
        'clean_content': cleaning.clean_html_content('content'),
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   167
        'clean_link_id': cleaning.clean_link_id('link_id'),
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   168
        'clean_scope_path': cleaning.clean_scope_path('scope_path'),
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   169
        'clean': cleaning.validate_document_acl(self, True),
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   170
        }
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   171
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   172
    new_params['extra_dynaexclude'] = ['author', 'created', 'content',
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   173
                                       'home_for', 'modified_by', 'modified',
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   174
                                       'take_survey', 'survey_content']
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   175
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   176
    new_params['edit_extra_dynaproperties'] = {
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   177
        'doc_key_name': forms.fields.CharField(widget=forms.HiddenInput),
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   178
        'created_by': forms.fields.CharField(widget=widgets.ReadOnlyInput(),
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   179
                                             required=False),
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   180
        'last_modified_by': forms.fields.CharField(
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   181
                                widget=widgets.ReadOnlyInput(), required=False),
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   182
        'clean': cleaning.validate_document_acl(self),
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   183
        }
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   184
2464
64b241a237b2 Merge params from inheriting Survey views so functionality can be reused.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2463
diff changeset
   185
    params = dicts.merge(params, new_params, sub_merge=True)
2463
05af53239799 Add more flexibility to the Survey helper for use with the new Survey types.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2455
diff changeset
   186
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   187
    super(View, self).__init__(params=params)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   188
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   189
  def list(self, request, access_type, page_name=None, params=None,
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   190
           filter=None, order=None, **kwargs):
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   191
    """See base.View.list.
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   192
    """
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   193
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   194
    return super(View, self).list(request, access_type, page_name=page_name,
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   195
                                  params=params, filter=kwargs)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   196
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   197
  def _public(self, request, entity, context):
2473
5cf0625dfa73 Changed the Survey public view to give a preview of the Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2472
diff changeset
   198
    """Add a preview version of the Survey to the page's context.
2455
a023b71ce125 fixed critical errors related to survey subclassing and renaming of deadline and opening properties, and surveys in sidebar are now marked as such and have a red color if untaken
james@jamess-macbook-air.local
parents: 2448
diff changeset
   199
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   200
    Args:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   201
      request: the django request object
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   202
      entity: the entity to make public
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   203
      context: the context object
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   204
    """
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   205
2473
5cf0625dfa73 Changed the Survey public view to give a preview of the Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2472
diff changeset
   206
    # construct the form to be shown on the page
5cf0625dfa73 Changed the Survey public view to give a preview of the Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2472
diff changeset
   207
    # TODO(ljvderijk) Generate SurveyForm without passing along the logic
5cf0625dfa73 Changed the Survey public view to give a preview of the Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2472
diff changeset
   208
    survey_form = surveys.SurveyForm(survey_content=entity.survey_content,
5cf0625dfa73 Changed the Survey public view to give a preview of the Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2472
diff changeset
   209
                                     survey_logic=self._params['logic'])
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   210
2473
5cf0625dfa73 Changed the Survey public view to give a preview of the Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2472
diff changeset
   211
    # TOOD(ljvderijk) pose question about the getFields method name and working
5cf0625dfa73 Changed the Survey public view to give a preview of the Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2472
diff changeset
   212
    survey_form.getFields()
2455
a023b71ce125 fixed critical errors related to survey subclassing and renaming of deadline and opening properties, and surveys in sidebar are now marked as such and have a red color if untaken
james@jamess-macbook-air.local
parents: 2448
diff changeset
   213
2473
5cf0625dfa73 Changed the Survey public view to give a preview of the Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2472
diff changeset
   214
    context['survey_form'] = survey_form
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   215
2473
5cf0625dfa73 Changed the Survey public view to give a preview of the Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2472
diff changeset
   216
    # return True to signal that the page may be displayed
5cf0625dfa73 Changed the Survey public view to give a preview of the Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2472
diff changeset
   217
    return True
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   218
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   219
  def _editContext(self, request, context):
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   220
    """Performs any required processing on the context for edit pages.
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   221
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   222
    Args:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   223
      request: the django request object
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   224
      context: the context dictionary that will be used
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   225
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   226
      Adds list of SurveyRecord results as supplement to view.
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   227
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   228
      See surveys.SurveyResults for details.
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   229
    """
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   230
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   231
    if not getattr(self, '_entity', None):
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   232
      return
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   233
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   234
    results = surveys.SurveyResults()
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   235
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   236
    context['survey_records'] = results.render(self._entity, self._params,
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   237
                                               filter={})
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   238
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   239
    super(View, self)._editContext(request, context)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   240
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   241
  def _editPost(self, request, entity, fields):
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   242
    """See base.View._editPost().
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   243
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   244
    Processes POST request items to add new dynamic field names,
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   245
    question types, and default prompt values to SurveyContent model.
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   246
    """
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   247
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   248
    user = user_logic.getForCurrentAccount()
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   249
    schema = {}
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   250
    survey_fields = {}
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   251
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   252
    if not entity:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   253
      # new Survey
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   254
      if 'serialized' in request.POST:
2478
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2477
diff changeset
   255
        fields, schema, survey_fields = self.importSerialized(request, fields,
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2477
diff changeset
   256
                                                              user)
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   257
      fields['author'] = user
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   258
    else:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   259
      fields['author'] = entity.author
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   260
      schema = self.loadSurveyContent(schema, survey_fields, entity)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   261
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   262
    # remove deleted properties from the model
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   263
    self.deleteQuestions(schema, survey_fields, request.POST)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   264
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   265
    # add new text questions and re-build choice questions
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   266
    self.getRequestQuestions(schema, survey_fields, request.POST)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   267
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   268
    # get schema options for choice questions
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   269
    self.getSchemaOptions(schema, survey_fields, request.POST)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   270
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   271
    survey_content = getattr(entity,'survey_content', None)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   272
    # create or update a SurveyContent for this Survey
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   273
    survey_content = survey_logic.createSurvey(survey_fields, schema,
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   274
                                                survey_content=survey_content)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   275
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   276
    # save survey_content for existent survey or pass for creating a new one
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   277
    if entity:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   278
      entity.modified_by = user
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   279
      entity.survey_content = survey_content
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   280
      db.put(entity)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   281
    else:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   282
      fields['survey_content'] = survey_content
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   283
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   284
    fields['modified_by'] = user
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   285
    super(View, self)._editPost(request, entity, fields)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   286
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   287
  def loadSurveyContent(self, schema, survey_fields, entity):
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   288
    """Populate the schema dict and get text survey questions.
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   289
    """
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   290
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   291
    if hasattr(entity, 'survey_content'):
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   292
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   293
      # there is a SurveyContent already
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   294
      survey_content = entity.survey_content
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   295
      schema = eval(survey_content.schema)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   296
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   297
      for question_name in survey_content.dynamic_properties():
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   298
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   299
        # get the current questions from the SurveyContent
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   300
        if question_name not in schema:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   301
          continue
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   302
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   303
        if schema[question_name]['type'] not in CHOICE_TYPES:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   304
          # Choice questions are always regenerated from request, see
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   305
          # self.get_request_questions()
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   306
          question = getattr(survey_content, question_name)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   307
          survey_fields[question_name] = question
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   308
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   309
    return schema
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   310
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   311
  def deleteQuestions(self, schema, survey_fields, POST):
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   312
    """Process the list of questions to delete, from a hidden input.
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   313
    """
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   314
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   315
    deleted = POST.get('__deleted__', '')
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   316
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   317
    if deleted:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   318
      deleted = deleted.split(',')
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   319
      for field in deleted:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   320
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   321
        if field in schema:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   322
          del schema[field]
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   323
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   324
        if field in survey_fields:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   325
          del survey_fields[field]
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   326
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   327
  def getRequestQuestions(self, schema, survey_fields, POST):
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   328
    """Get fields from request.
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   329
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   330
    We use two field/question naming and processing schemes:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   331
      - Choice questions consist of <input/>s with a common name, being rebuilt
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   332
        anew on every edit POST so we can gather ordering, text changes,
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   333
        deletions and additions.
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   334
      - Text questions only have special survey__* names on creation, afterwards
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   335
        they are loaded from the SurveyContent dynamic properties.
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   336
    """
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   337
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   338
    for key, value in POST.items():
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   339
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   340
      if key.startswith('id_'):
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   341
        # Choice question fields, they are always generated from POST contents,
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   342
        # as their 'content' is editable and they're reorderable. Also get
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   343
        # its field index for handling reordering fields later.
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   344
        name, number = key[3:].replace('__field', '').rsplit('_', 1)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   345
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   346
        if name not in schema:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   347
          if 'NEW_' + name in POST:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   348
            # new Choice question, set generic type and get its index
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   349
            schema[name] = {'type': 'choice'}
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   350
            schema[name]['index'] = int(POST['index_for_' + name])
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   351
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   352
        if name in schema and schema[name]['type'] in CHOICE_TYPES:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   353
          # build an index:content dictionary
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   354
          if name in survey_fields:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   355
            if value not in survey_fields[name]:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   356
              survey_fields[name][int(number)] = value
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   357
          else:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   358
            survey_fields[name] = {int(number): value}
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   359
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   360
      elif key.startswith('survey__'): # new Text question
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   361
        # this is super ugly but unless data is serialized the regex is needed
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   362
        prefix = re.compile('survey__([0-9]{1,3})__')
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   363
        prefix_match = re.match(prefix, key)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   364
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   365
        index = prefix_match.group(0).replace('survey', '').replace('__','')
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   366
        index = int(index)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   367
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   368
        field_name = prefix.sub('', key)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   369
        field = 'id_' + key
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   370
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   371
        for ptype in PROPERTY_TYPES:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   372
          # should only match one
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   373
          if ptype + "__" in field_name:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   374
            field_name = field_name.replace(ptype + "__", "")
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   375
            schema[field_name] = {}
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   376
            schema[field_name]["index"] = index
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   377
            schema[field_name]["type"] = ptype
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   378
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   379
        survey_fields[field_name] = value
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   380
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   381
  def getSchemaOptions(self, schema, survey_fields, POST):
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   382
    """Get question, type, rendering and option order for choice questions.
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   383
    """
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   384
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   385
    RENDER = {'checkboxes': 'multi_checkbox', 'select': 'single_select',
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   386
              'radio_buttons': 'quant_radio'}
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   387
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   388
    RENDER_TYPES = {'select': 'selection',
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   389
                    'checkboxes': 'pick_multi',
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   390
                    'radio_buttons': 'pick_quant' }
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   391
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   392
    for key in schema:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   393
      if schema[key]['type'] in CHOICE_TYPES and key in survey_fields:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   394
        render_for = 'render_for_' + key
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   395
        if render_for in POST:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   396
          schema[key]['render'] = RENDER[POST[render_for]]
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   397
          schema[key]['type'] = RENDER_TYPES[POST[render_for]]
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   398
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   399
        # handle reordering fields
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   400
        ordered = False
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   401
        order = 'order_for_' + key
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   402
        if order in POST and isinstance(survey_fields[key], dict):
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   403
          order = POST[order]
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   404
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   405
          # 'order_for_name' is jquery serialized from a sortable, so it's in
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   406
          # a 'name[]=1&name[]=2&name[]=0' format ('id-li-' is set in our JS)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   407
          order = order.replace('id-li-%s[]=' % key, '')
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   408
          order = order.split('&')
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   409
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   410
          if len(order) == len(survey_fields[key]) and order[0]:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   411
            order = [int(number) for number in order]
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   412
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   413
            if set(order) == set(survey_fields[key]):
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   414
              survey_fields[key] = [survey_fields[key][i] for i in order]
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   415
              ordered = True
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   416
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   417
          if not ordered:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   418
            # we don't have a good ordering to use
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   419
            ordered = sorted(survey_fields[key].items())
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   420
            survey_fields[key] = [value for index, value in ordered]
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   421
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   422
      # set 'question' entry (free text label for question) in schema
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   423
      question_for = 'NEW_' + key
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   424
      if question_for in POST:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   425
        schema[key]["question"] = POST[question_for]
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   426
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   427
  def createGet(self, request, context, params, seed):
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   428
    """Pass the question types for the survey creation template.
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   429
    """
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   430
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   431
    context['question_types'] = QUESTION_TYPES
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   432
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   433
    # avoid spurious results from showing on creation
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   434
    context['new_survey'] = True
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   435
    return super(View, self).createGet(request, context, params, seed)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   436
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   437
  def editGet(self, request, entity, context, params=None):
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   438
    """Process GET requests for the specified entity.
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   439
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   440
    Builds the SurveyForm that represents the Survey question contents.
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   441
    """
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   442
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   443
    self._entity = entity
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   444
    survey_content = entity.survey_content
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   445
    user = user_logic.getForCurrentAccount()
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   446
    # no project or survey_record needed for survey prototype
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   447
    project = None
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   448
    survey_record = None
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   449
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   450
    survey_form = surveys.SurveyForm(survey_content=survey_content,
2463
05af53239799 Add more flexibility to the Survey helper for use with the new Survey types.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2455
diff changeset
   451
                                     this_user=user, project=project,
05af53239799 Add more flexibility to the Survey helper for use with the new Survey types.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2455
diff changeset
   452
                                     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: 2455
diff changeset
   453
                                     survey_record=survey_record,
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   454
                                     editing=True, read_only=False)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   455
    survey_form.getFields()
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   456
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   457
    local = dict(survey_form=survey_form, question_types=QUESTION_TYPES,
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   458
                survey_h=entity.survey_content)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   459
    context.update(local)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   460
2478
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2477
diff changeset
   461
    params['edit_form'] = surveys.HelperForm(params['edit_form'])
2440
05c430d1c147 Renamed opening to survey_start and deadline to survey_end.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2439
diff changeset
   462
    if entity.survey_end and datetime.datetime.now() > entity.survey_end:
2455
a023b71ce125 fixed critical errors related to survey subclassing and renaming of deadline and opening properties, and surveys in sidebar are now marked as such and have a red color if untaken
james@jamess-macbook-air.local
parents: 2448
diff changeset
   463
      # are we already passed the survey_end?
a023b71ce125 fixed critical errors related to survey subclassing and renaming of deadline and opening properties, and surveys in sidebar are now marked as such and have a red color if untaken
james@jamess-macbook-air.local
parents: 2448
diff changeset
   464
      context["passed_survey_end"] = True
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   465
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   466
    return super(View, self).editGet(request, entity, context, params=params)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   467
2475
dfb9249d91b6 Added skeleton for the take view and its template.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2474
diff changeset
   468
  @decorators.merge_params
dfb9249d91b6 Added skeleton for the take view and its template.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2474
diff changeset
   469
  @decorators.check_access
dfb9249d91b6 Added skeleton for the take view and its template.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2474
diff changeset
   470
  def take(self, request, access_type, page_name=None,
dfb9249d91b6 Added skeleton for the take view and its template.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2474
diff changeset
   471
           params=None, **kwargs):
dfb9249d91b6 Added skeleton for the take view and its template.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2474
diff changeset
   472
    """View for taking a Survey.
dfb9249d91b6 Added skeleton for the take view and its template.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2474
diff changeset
   473
dfb9249d91b6 Added skeleton for the take view and its template.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2474
diff changeset
   474
    For Args see base.View().public().
dfb9249d91b6 Added skeleton for the take view and its template.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2474
diff changeset
   475
    """
dfb9249d91b6 Added skeleton for the take view and its template.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2474
diff changeset
   476
2477
e5629995d118 Implemented the takeGet view for Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2476
diff changeset
   477
    survey_logic = params['logic']
e5629995d118 Implemented the takeGet view for Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2476
diff changeset
   478
2475
dfb9249d91b6 Added skeleton for the take view and its template.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2474
diff changeset
   479
    try:
2497
996f381d458c Added _getSurveyRecordFor method to Survey View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2493
diff changeset
   480
      entity = survey_logic.getFromKeyFieldsOr404(kwargs)
2475
dfb9249d91b6 Added skeleton for the take view and its template.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2474
diff changeset
   481
    except out_of_band.Error, error:
dfb9249d91b6 Added skeleton for the take view and its template.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2474
diff changeset
   482
      return responses.errorResponse(
dfb9249d91b6 Added skeleton for the take view and its template.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2474
diff changeset
   483
          error, request, template=params['error_public'])
dfb9249d91b6 Added skeleton for the take view and its template.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2474
diff changeset
   484
dfb9249d91b6 Added skeleton for the take view and its template.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2474
diff changeset
   485
    template = params['take_template']
dfb9249d91b6 Added skeleton for the take view and its template.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2474
diff changeset
   486
dfb9249d91b6 Added skeleton for the take view and its template.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2474
diff changeset
   487
    # get the context for this webpage
dfb9249d91b6 Added skeleton for the take view and its template.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2474
diff changeset
   488
    context = responses.getUniversalContext(request)
dfb9249d91b6 Added skeleton for the take view and its template.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2474
diff changeset
   489
    responses.useJavaScript(context, params['js_uses_all'])
dfb9249d91b6 Added skeleton for the take view and its template.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2474
diff changeset
   490
    context['page_name'] = "%s titled '%s'" % (page_name, entity.title)
dfb9249d91b6 Added skeleton for the take view and its template.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2474
diff changeset
   491
    context['entity'] = entity
dfb9249d91b6 Added skeleton for the take view and its template.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2474
diff changeset
   492
2477
e5629995d118 Implemented the takeGet view for Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2476
diff changeset
   493
    # try to get an existing SurveyRecord for the current user
2497
996f381d458c Added _getSurveyRecordFor method to Survey View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2493
diff changeset
   494
    survey_record = self._getSurveyRecordFor(entity, request, params)
2477
e5629995d118 Implemented the takeGet view for Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2476
diff changeset
   495
2475
dfb9249d91b6 Added skeleton for the take view and its template.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2474
diff changeset
   496
    if request.POST:
dfb9249d91b6 Added skeleton for the take view and its template.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2474
diff changeset
   497
      return self.takePost(request, template, context, params, entity,
2477
e5629995d118 Implemented the takeGet view for Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2476
diff changeset
   498
                           survey_record, **kwargs)
2475
dfb9249d91b6 Added skeleton for the take view and its template.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2474
diff changeset
   499
    else: #request.GET
dfb9249d91b6 Added skeleton for the take view and its template.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2474
diff changeset
   500
      return self.takeGet(request, template, context, params, entity,
2477
e5629995d118 Implemented the takeGet view for Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2476
diff changeset
   501
                          survey_record, **kwargs)
2475
dfb9249d91b6 Added skeleton for the take view and its template.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2474
diff changeset
   502
2497
996f381d458c Added _getSurveyRecordFor method to Survey View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2493
diff changeset
   503
  def _getSurveyRecordFor(self, survey, request, params):
996f381d458c Added _getSurveyRecordFor method to Survey View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2493
diff changeset
   504
    """Returns the SurveyRecord for the given Survey and request.
996f381d458c Added _getSurveyRecordFor method to Survey View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2493
diff changeset
   505
996f381d458c Added _getSurveyRecordFor method to Survey View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2493
diff changeset
   506
    Args:
996f381d458c Added _getSurveyRecordFor method to Survey View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2493
diff changeset
   507
        survey: a Survey entity
996f381d458c Added _getSurveyRecordFor method to Survey View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2493
diff changeset
   508
        request: a Django HTTPRequest object
996f381d458c Added _getSurveyRecordFor method to Survey View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2493
diff changeset
   509
        params: params for the requesting view
996f381d458c Added _getSurveyRecordFor method to Survey View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2493
diff changeset
   510
996f381d458c Added _getSurveyRecordFor method to Survey View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2493
diff changeset
   511
    Returns:
996f381d458c Added _getSurveyRecordFor method to Survey View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2493
diff changeset
   512
        An existing SurveyRecord iff any exists for the given Survey, request
996f381d458c Added _getSurveyRecordFor method to Survey View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2493
diff changeset
   513
        and any other conditions that must apply.
996f381d458c Added _getSurveyRecordFor method to Survey View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2493
diff changeset
   514
    """
996f381d458c Added _getSurveyRecordFor method to Survey View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2493
diff changeset
   515
996f381d458c Added _getSurveyRecordFor method to Survey View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2493
diff changeset
   516
    survey_logic = params['logic']
996f381d458c Added _getSurveyRecordFor method to Survey View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2493
diff changeset
   517
    record_logic = survey_logic.getRecordLogic()
996f381d458c Added _getSurveyRecordFor method to Survey View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2493
diff changeset
   518
996f381d458c Added _getSurveyRecordFor method to Survey View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2493
diff changeset
   519
    user_entity = user_logic.getForCurrentAccount()
996f381d458c Added _getSurveyRecordFor method to Survey View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2493
diff changeset
   520
996f381d458c Added _getSurveyRecordFor method to Survey View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2493
diff changeset
   521
    filter = {'survey': survey,
996f381d458c Added _getSurveyRecordFor method to Survey View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2493
diff changeset
   522
              'user': user_entity}
996f381d458c Added _getSurveyRecordFor method to Survey View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2493
diff changeset
   523
996f381d458c Added _getSurveyRecordFor method to Survey View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2493
diff changeset
   524
    return record_logic.getForFields(filter, unique=True)
996f381d458c Added _getSurveyRecordFor method to Survey View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2493
diff changeset
   525
2477
e5629995d118 Implemented the takeGet view for Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2476
diff changeset
   526
  def takeGet(self, request, template, context, params, entity, record,
2501
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2497
diff changeset
   527
              form_data=None, **kwargs):
2475
dfb9249d91b6 Added skeleton for the take view and its template.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2474
diff changeset
   528
    """Handles the GET request for the Survey's take page.
dfb9249d91b6 Added skeleton for the take view and its template.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2474
diff changeset
   529
dfb9249d91b6 Added skeleton for the take view and its template.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2474
diff changeset
   530
    Args:
dfb9249d91b6 Added skeleton for the take view and its template.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2474
diff changeset
   531
        template: the template used for this view
2477
e5629995d118 Implemented the takeGet view for Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2476
diff changeset
   532
        entity: the Survey entity
2486
78c87d9f4af7 Added hook methods to takeGet and takePost.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2485
diff changeset
   533
        record: a SurveyRecord entity
2501
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2497
diff changeset
   534
        form_data: dict with form data that will be used for validation
2475
dfb9249d91b6 Added skeleton for the take view and its template.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2474
diff changeset
   535
        rest: see base.View.public()
dfb9249d91b6 Added skeleton for the take view and its template.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2474
diff changeset
   536
    """
dfb9249d91b6 Added skeleton for the take view and its template.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2474
diff changeset
   537
2477
e5629995d118 Implemented the takeGet view for Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2476
diff changeset
   538
    survey_form = surveys.SurveyForm(survey_content=entity.survey_content,
e5629995d118 Implemented the takeGet view for Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2476
diff changeset
   539
                                     survey_record=record,
e5629995d118 Implemented the takeGet view for Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2476
diff changeset
   540
                                     survey_logic=self._params['logic'])
e5629995d118 Implemented the takeGet view for Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2476
diff changeset
   541
2501
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2497
diff changeset
   542
    # fetch field contents and pass request data, if any
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2497
diff changeset
   543
    survey_form.getFields(post_dict=form_data)
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2497
diff changeset
   544
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2497
diff changeset
   545
    # validate request data
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2497
diff changeset
   546
    if form_data and not survey_form.is_valid():
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2497
diff changeset
   547
      survey_form.full_clean()
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2497
diff changeset
   548
      context['survey_form'] = survey_form
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2497
diff changeset
   549
      return self._constructResponse(request, entity=None, context=context,
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2497
diff changeset
   550
          form=survey_form, params=params, template=template)
2475
dfb9249d91b6 Added skeleton for the take view and its template.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2474
diff changeset
   551
2477
e5629995d118 Implemented the takeGet view for Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2476
diff changeset
   552
    # fill context with the survey and additional information
e5629995d118 Implemented the takeGet view for Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2476
diff changeset
   553
    context['survey_form'] = survey_form
e5629995d118 Implemented the takeGet view for Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2476
diff changeset
   554
    self.setHelpAndStatus(context, entity, record)
2475
dfb9249d91b6 Added skeleton for the take view and its template.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2474
diff changeset
   555
2486
78c87d9f4af7 Added hook methods to takeGet and takePost.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2485
diff changeset
   556
    # call the hook method
78c87d9f4af7 Added hook methods to takeGet and takePost.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2485
diff changeset
   557
    self._takeGet(request, template, context, params, entity, record, **kwargs)
78c87d9f4af7 Added hook methods to takeGet and takePost.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2485
diff changeset
   558
2477
e5629995d118 Implemented the takeGet view for Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2476
diff changeset
   559
    return responses.respond(request, template, context)
e5629995d118 Implemented the takeGet view for Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2476
diff changeset
   560
2486
78c87d9f4af7 Added hook methods to takeGet and takePost.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2485
diff changeset
   561
  def _takeGet(self, request, template, context, params, entity, record,
78c87d9f4af7 Added hook methods to takeGet and takePost.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2485
diff changeset
   562
              **kwargs):
78c87d9f4af7 Added hook methods to takeGet and takePost.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2485
diff changeset
   563
    """Hook for the GET request for the Survey's take page.
78c87d9f4af7 Added hook methods to takeGet and takePost.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2485
diff changeset
   564
78c87d9f4af7 Added hook methods to takeGet and takePost.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2485
diff changeset
   565
    This method is called just before the GET page is shown.
78c87d9f4af7 Added hook methods to takeGet and takePost.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2485
diff changeset
   566
78c87d9f4af7 Added hook methods to takeGet and takePost.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2485
diff changeset
   567
    Args:
78c87d9f4af7 Added hook methods to takeGet and takePost.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2485
diff changeset
   568
        template: the template used for this view
78c87d9f4af7 Added hook methods to takeGet and takePost.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2485
diff changeset
   569
        entity: the Survey entity
78c87d9f4af7 Added hook methods to takeGet and takePost.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2485
diff changeset
   570
        record: a SurveyRecord entity
78c87d9f4af7 Added hook methods to takeGet and takePost.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2485
diff changeset
   571
        rest: see base.View.public()
78c87d9f4af7 Added hook methods to takeGet and takePost.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2485
diff changeset
   572
    """
78c87d9f4af7 Added hook methods to takeGet and takePost.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2485
diff changeset
   573
    pass
78c87d9f4af7 Added hook methods to takeGet and takePost.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2485
diff changeset
   574
2477
e5629995d118 Implemented the takeGet view for Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2476
diff changeset
   575
  def takePost(self, request, template, context, params, entity, record,
e5629995d118 Implemented the takeGet view for Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2476
diff changeset
   576
               **kwargs):
2475
dfb9249d91b6 Added skeleton for the take view and its template.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2474
diff changeset
   577
    """Handles the POST request for the Survey's take page.
dfb9249d91b6 Added skeleton for the take view and its template.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2474
diff changeset
   578
dfb9249d91b6 Added skeleton for the take view and its template.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2474
diff changeset
   579
    Args:
dfb9249d91b6 Added skeleton for the take view and its template.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2474
diff changeset
   580
        template: the template used for this view
2477
e5629995d118 Implemented the takeGet view for Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2476
diff changeset
   581
        entity: the Survey entity
2486
78c87d9f4af7 Added hook methods to takeGet and takePost.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2485
diff changeset
   582
        record: a SurveyRecord entity
2475
dfb9249d91b6 Added skeleton for the take view and its template.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2474
diff changeset
   583
        rest: see base.View.public()
dfb9249d91b6 Added skeleton for the take view and its template.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2474
diff changeset
   584
    """
dfb9249d91b6 Added skeleton for the take view and its template.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2474
diff changeset
   585
2484
f41a9e1eeeea Add a base implementation of takePost.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2478
diff changeset
   586
    survey_logic = params['logic']
f41a9e1eeeea Add a base implementation of takePost.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2478
diff changeset
   587
    record_logic = survey_logic.getRecordLogic()
f41a9e1eeeea Add a base implementation of takePost.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2478
diff changeset
   588
2501
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2497
diff changeset
   589
    # create a form to validate
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2497
diff changeset
   590
    survey_form = surveys.SurveyForm(survey_content=entity.survey_content,
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2497
diff changeset
   591
                                     survey_record=None,
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2497
diff changeset
   592
                                     survey_logic=self._params['logic'])
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2497
diff changeset
   593
    # fill form with request data
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2497
diff changeset
   594
    survey_form.getFields(post_dict=request.POST)
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2497
diff changeset
   595
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2497
diff changeset
   596
    if not survey_form.is_valid():
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2497
diff changeset
   597
      # redirect to takeGet so we can handle errors
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2497
diff changeset
   598
      return self.takeGet(request, template, context, params, entity, record,
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2497
diff changeset
   599
                          form_data=properties)
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2497
diff changeset
   600
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2497
diff changeset
   601
    # retrieve the data from the form
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2497
diff changeset
   602
    _, properties = forms_helper.collectCleanedFields(survey_form)
2475
dfb9249d91b6 Added skeleton for the take view and its template.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2474
diff changeset
   603
2484
f41a9e1eeeea Add a base implementation of takePost.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2478
diff changeset
   604
    # add the required SurveyRecord properties
f41a9e1eeeea Add a base implementation of takePost.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2478
diff changeset
   605
    properties['user'] = user_logic.getForCurrentAccount()
f41a9e1eeeea Add a base implementation of takePost.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2478
diff changeset
   606
    properties['survey'] = entity
f41a9e1eeeea Add a base implementation of takePost.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2478
diff changeset
   607
2486
78c87d9f4af7 Added hook methods to takeGet and takePost.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2485
diff changeset
   608
    # call the hook method before updating the SurveyRecord
78c87d9f4af7 Added hook methods to takeGet and takePost.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2485
diff changeset
   609
    self._takePost(request, params, entity, record, properties)
78c87d9f4af7 Added hook methods to takeGet and takePost.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2485
diff changeset
   610
2484
f41a9e1eeeea Add a base implementation of takePost.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2478
diff changeset
   611
    # update the record entity if any and clear all dynamic properties
f41a9e1eeeea Add a base implementation of takePost.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2478
diff changeset
   612
    record_logic.updateOrCreateFromFields(record, properties, clear_dynamic=True)
f41a9e1eeeea Add a base implementation of takePost.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2478
diff changeset
   613
f41a9e1eeeea Add a base implementation of takePost.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2478
diff changeset
   614
    # TODO: add notice to page that the response has been saved successfully
f41a9e1eeeea Add a base implementation of takePost.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2478
diff changeset
   615
    # redirect to the same page for now
f41a9e1eeeea Add a base implementation of takePost.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2478
diff changeset
   616
    redirect = request.path
f41a9e1eeeea Add a base implementation of takePost.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2478
diff changeset
   617
    return http.HttpResponseRedirect(redirect)
2477
e5629995d118 Implemented the takeGet view for Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2476
diff changeset
   618
2486
78c87d9f4af7 Added hook methods to takeGet and takePost.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2485
diff changeset
   619
  def _takePost(self, request, params, entity, record, properties):
78c87d9f4af7 Added hook methods to takeGet and takePost.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2485
diff changeset
   620
    """Hook for the POST request for the Survey's take page.
78c87d9f4af7 Added hook methods to takeGet and takePost.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2485
diff changeset
   621
78c87d9f4af7 Added hook methods to takeGet and takePost.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2485
diff changeset
   622
    This method is called just before the SurveyRecord is stored.
78c87d9f4af7 Added hook methods to takeGet and takePost.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2485
diff changeset
   623
78c87d9f4af7 Added hook methods to takeGet and takePost.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2485
diff changeset
   624
    Args:
78c87d9f4af7 Added hook methods to takeGet and takePost.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2485
diff changeset
   625
        request: Django Request object
78c87d9f4af7 Added hook methods to takeGet and takePost.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2485
diff changeset
   626
        params: the params for the current view
78c87d9f4af7 Added hook methods to takeGet and takePost.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2485
diff changeset
   627
        entity: a Survey entity
78c87d9f4af7 Added hook methods to takeGet and takePost.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2485
diff changeset
   628
        record: a SurveyRecord entity
78c87d9f4af7 Added hook methods to takeGet and takePost.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2485
diff changeset
   629
        properties: properties to be stored in the SurveyRecord entity
78c87d9f4af7 Added hook methods to takeGet and takePost.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2485
diff changeset
   630
    """
78c87d9f4af7 Added hook methods to takeGet and takePost.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2485
diff changeset
   631
    pass
78c87d9f4af7 Added hook methods to takeGet and takePost.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2485
diff changeset
   632
2477
e5629995d118 Implemented the takeGet view for Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2476
diff changeset
   633
  def setHelpAndStatus(self, context, survey, survey_record):
e5629995d118 Implemented the takeGet view for Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2476
diff changeset
   634
    """Get help_text and status for template use.
e5629995d118 Implemented the takeGet view for Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2476
diff changeset
   635
e5629995d118 Implemented the takeGet view for Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2476
diff changeset
   636
    Args:
e5629995d118 Implemented the takeGet view for Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2476
diff changeset
   637
        context: the context for the view to update
e5629995d118 Implemented the takeGet view for Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2476
diff changeset
   638
        survey: a Survey entity
2501
d612b48e6e12 Added Survey From Field validation.
Daniel Diniz <ajaksu@gmail.com>
parents: 2497
diff changeset
   639
        survey_record: a SurveyRecordEntity
2477
e5629995d118 Implemented the takeGet view for Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2476
diff changeset
   640
    """
e5629995d118 Implemented the takeGet view for Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2476
diff changeset
   641
e5629995d118 Implemented the takeGet view for Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2476
diff changeset
   642
    if not survey.survey_end:
e5629995d118 Implemented the takeGet view for Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2476
diff changeset
   643
      survey_end_text = ""
e5629995d118 Implemented the takeGet view for Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2476
diff changeset
   644
    else:
e5629995d118 Implemented the takeGet view for Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2476
diff changeset
   645
      survey_end_text = " by " + str(
e5629995d118 Implemented the takeGet view for Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2476
diff changeset
   646
          survey.survey_end.strftime("%A, %d. %B %Y %I:%M%p"))
e5629995d118 Implemented the takeGet view for Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2476
diff changeset
   647
e5629995d118 Implemented the takeGet view for Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2476
diff changeset
   648
    if survey_record:
e5629995d118 Implemented the takeGet view for Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2476
diff changeset
   649
      help_text = "Edit and re-submit this survey" + survey_end_text + "."
e5629995d118 Implemented the takeGet view for Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2476
diff changeset
   650
      status = "edit"
e5629995d118 Implemented the takeGet view for Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2476
diff changeset
   651
    else:
e5629995d118 Implemented the takeGet view for Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2476
diff changeset
   652
      help_text = "Please complete this survey" + survey_end_text + "."
e5629995d118 Implemented the takeGet view for Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2476
diff changeset
   653
      status = "create"
e5629995d118 Implemented the takeGet view for Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2476
diff changeset
   654
e5629995d118 Implemented the takeGet view for Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2476
diff changeset
   655
    # update the context with the help_text and status
e5629995d118 Implemented the takeGet view for Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2476
diff changeset
   656
    context_update = dict(status=status, help_text=help_text)
e5629995d118 Implemented the takeGet view for Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2476
diff changeset
   657
    context.update(context_update)
2475
dfb9249d91b6 Added skeleton for the take view and its template.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2474
diff changeset
   658
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   659
  def activate(self, request, **kwargs):
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   660
    """This is a hack to support the 'Enable grades' button.
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   661
    """
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   662
    self.activateGrades(request)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   663
    redirect_path = request.path.replace('/activate/', '/edit/') + '?activate=1'
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   664
    return http.HttpResponseRedirect(redirect_path)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   665
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   666
  def activateGrades(self, request, **kwargs):
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   667
    """Updates SurveyRecord's grades for a given Survey.
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   668
    """
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   669
    survey_key_name = survey_logic.getKeyNameFromPath(request.path)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   670
    survey = Survey.get_by_key_name(survey_key_name)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   671
    survey_logic.activateGrades(survey)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   672
    return
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   673
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   674
  @decorators.merge_params
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   675
  @decorators.check_access
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   676
  def viewResults(self, request, access_type, page_name=None,
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   677
                  params=None, **kwargs):
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   678
    """View for SurveyRecord and SurveyRecordGroup.
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   679
    """
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   680
2467
f46cff8d676b Solved some merging problems and use the getRecordLogic where needed.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2464
diff changeset
   681
    results_logic = params['logic'].getRecordLogic()
f46cff8d676b Solved some merging problems and use the getRecordLogic where needed.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2464
diff changeset
   682
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   683
    user = user_logic.getForCurrentAccount()
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   684
2455
a023b71ce125 fixed critical errors related to survey subclassing and renaming of deadline and opening properties, and surveys in sidebar are now marked as such and have a red color if untaken
james@jamess-macbook-air.local
parents: 2448
diff changeset
   685
    # TODO(ajaksu) use the named parameter link_id from the re
a023b71ce125 fixed critical errors related to survey subclassing and renaming of deadline and opening properties, and surveys in sidebar are now marked as such and have a red color if untaken
james@jamess-macbook-air.local
parents: 2448
diff changeset
   686
    if request.path == '/survey/show/user/' + user.link_id:
a023b71ce125 fixed critical errors related to survey subclassing and renaming of deadline and opening properties, and surveys in sidebar are now marked as such and have a red color if untaken
james@jamess-macbook-air.local
parents: 2448
diff changeset
   687
      records = tuple(user.surveys_taken.run())
a023b71ce125 fixed critical errors related to survey subclassing and renaming of deadline and opening properties, and surveys in sidebar are now marked as such and have a red color if untaken
james@jamess-macbook-air.local
parents: 2448
diff changeset
   688
      context = responses.getUniversalContext(request)
a023b71ce125 fixed critical errors related to survey subclassing and renaming of deadline and opening properties, and surveys in sidebar are now marked as such and have a red color if untaken
james@jamess-macbook-air.local
parents: 2448
diff changeset
   689
      context['content'] = records[0].survey.survey_content
a023b71ce125 fixed critical errors related to survey subclassing and renaming of deadline and opening properties, and surveys in sidebar are now marked as such and have a red color if untaken
james@jamess-macbook-air.local
parents: 2448
diff changeset
   690
      responses.useJavaScript(context, params['js_uses_all'])
a023b71ce125 fixed critical errors related to survey subclassing and renaming of deadline and opening properties, and surveys in sidebar are now marked as such and have a red color if untaken
james@jamess-macbook-air.local
parents: 2448
diff changeset
   691
      context['page_name'] = u'Your survey records.'
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   692
    else:
2455
a023b71ce125 fixed critical errors related to survey subclassing and renaming of deadline and opening properties, and surveys in sidebar are now marked as such and have a red color if untaken
james@jamess-macbook-air.local
parents: 2448
diff changeset
   693
      entity, context = self.getContextEntity(request, page_name,
a023b71ce125 fixed critical errors related to survey subclassing and renaming of deadline and opening properties, and surveys in sidebar are now marked as such and have a red color if untaken
james@jamess-macbook-air.local
parents: 2448
diff changeset
   694
                                              params, kwargs)
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   695
2455
a023b71ce125 fixed critical errors related to survey subclassing and renaming of deadline and opening properties, and surveys in sidebar are now marked as such and have a red color if untaken
james@jamess-macbook-air.local
parents: 2448
diff changeset
   696
      if context is None:
a023b71ce125 fixed critical errors related to survey subclassing and renaming of deadline and opening properties, and surveys in sidebar are now marked as such and have a red color if untaken
james@jamess-macbook-air.local
parents: 2448
diff changeset
   697
        # user cannot see this page, return error response
a023b71ce125 fixed critical errors related to survey subclassing and renaming of deadline and opening properties, and surveys in sidebar are now marked as such and have a red color if untaken
james@jamess-macbook-air.local
parents: 2448
diff changeset
   698
        return entity
a023b71ce125 fixed critical errors related to survey subclassing and renaming of deadline and opening properties, and surveys in sidebar are now marked as such and have a red color if untaken
james@jamess-macbook-air.local
parents: 2448
diff changeset
   699
      context['content'] = entity.survey_content
a023b71ce125 fixed critical errors related to survey subclassing and renaming of deadline and opening properties, and surveys in sidebar are now marked as such and have a red color if untaken
james@jamess-macbook-air.local
parents: 2448
diff changeset
   700
      can_write = False
a023b71ce125 fixed critical errors related to survey subclassing and renaming of deadline and opening properties, and surveys in sidebar are now marked as such and have a red color if untaken
james@jamess-macbook-air.local
parents: 2448
diff changeset
   701
      rights = self._params['rights']
a023b71ce125 fixed critical errors related to survey subclassing and renaming of deadline and opening properties, and surveys in sidebar are now marked as such and have a red color if untaken
james@jamess-macbook-air.local
parents: 2448
diff changeset
   702
      try:
a023b71ce125 fixed critical errors related to survey subclassing and renaming of deadline and opening properties, and surveys in sidebar are now marked as such and have a red color if untaken
james@jamess-macbook-air.local
parents: 2448
diff changeset
   703
        rights.checkIsSurveyWritable({'key_name': entity.key().name(),
a023b71ce125 fixed critical errors related to survey subclassing and renaming of deadline and opening properties, and surveys in sidebar are now marked as such and have a red color if untaken
james@jamess-macbook-air.local
parents: 2448
diff changeset
   704
                                      'prefix': entity.prefix,
a023b71ce125 fixed critical errors related to survey subclassing and renaming of deadline and opening properties, and surveys in sidebar are now marked as such and have a red color if untaken
james@jamess-macbook-air.local
parents: 2448
diff changeset
   705
                                      'scope_path': entity.scope_path,
a023b71ce125 fixed critical errors related to survey subclassing and renaming of deadline and opening properties, and surveys in sidebar are now marked as such and have a red color if untaken
james@jamess-macbook-air.local
parents: 2448
diff changeset
   706
                                      'link_id': entity.link_id,},
a023b71ce125 fixed critical errors related to survey subclassing and renaming of deadline and opening properties, and surveys in sidebar are now marked as such and have a red color if untaken
james@jamess-macbook-air.local
parents: 2448
diff changeset
   707
                                     'key_name')
a023b71ce125 fixed critical errors related to survey subclassing and renaming of deadline and opening properties, and surveys in sidebar are now marked as such and have a red color if untaken
james@jamess-macbook-air.local
parents: 2448
diff changeset
   708
        can_write = True
a023b71ce125 fixed critical errors related to survey subclassing and renaming of deadline and opening properties, and surveys in sidebar are now marked as such and have a red color if untaken
james@jamess-macbook-air.local
parents: 2448
diff changeset
   709
      except out_of_band.AccessViolation:
a023b71ce125 fixed critical errors related to survey subclassing and renaming of deadline and opening properties, and surveys in sidebar are now marked as such and have a red color if untaken
james@jamess-macbook-air.local
parents: 2448
diff changeset
   710
        pass
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   711
2455
a023b71ce125 fixed critical errors related to survey subclassing and renaming of deadline and opening properties, and surveys in sidebar are now marked as such and have a red color if untaken
james@jamess-macbook-air.local
parents: 2448
diff changeset
   712
      filter = self._params.get('filter') or {}
a023b71ce125 fixed critical errors related to survey subclassing and renaming of deadline and opening properties, and surveys in sidebar are now marked as such and have a red color if untaken
james@jamess-macbook-air.local
parents: 2448
diff changeset
   713
a023b71ce125 fixed critical errors related to survey subclassing and renaming of deadline and opening properties, and surveys in sidebar are now marked as such and have a red color if untaken
james@jamess-macbook-air.local
parents: 2448
diff changeset
   714
      # if user can edit the survey, show everyone's results
a023b71ce125 fixed critical errors related to survey subclassing and renaming of deadline and opening properties, and surveys in sidebar are now marked as such and have a red color if untaken
james@jamess-macbook-air.local
parents: 2448
diff changeset
   715
      if can_write:
a023b71ce125 fixed critical errors related to survey subclassing and renaming of deadline and opening properties, and surveys in sidebar are now marked as such and have a red color if untaken
james@jamess-macbook-air.local
parents: 2448
diff changeset
   716
        filter['survey'] = entity
a023b71ce125 fixed critical errors related to survey subclassing and renaming of deadline and opening properties, and surveys in sidebar are now marked as such and have a red color if untaken
james@jamess-macbook-air.local
parents: 2448
diff changeset
   717
      else:
a023b71ce125 fixed critical errors related to survey subclassing and renaming of deadline and opening properties, and surveys in sidebar are now marked as such and have a red color if untaken
james@jamess-macbook-air.local
parents: 2448
diff changeset
   718
        filter.update({'user': user, 'survey': entity})
a023b71ce125 fixed critical errors related to survey subclassing and renaming of deadline and opening properties, and surveys in sidebar are now marked as such and have a red color if untaken
james@jamess-macbook-air.local
parents: 2448
diff changeset
   719
a023b71ce125 fixed critical errors related to survey subclassing and renaming of deadline and opening properties, and surveys in sidebar are now marked as such and have a red color if untaken
james@jamess-macbook-air.local
parents: 2448
diff changeset
   720
      limit = self._params.get('limit') or 1000
a023b71ce125 fixed critical errors related to survey subclassing and renaming of deadline and opening properties, and surveys in sidebar are now marked as such and have a red color if untaken
james@jamess-macbook-air.local
parents: 2448
diff changeset
   721
      offset = self._params.get('offset') or 0
a023b71ce125 fixed critical errors related to survey subclassing and renaming of deadline and opening properties, and surveys in sidebar are now marked as such and have a red color if untaken
james@jamess-macbook-air.local
parents: 2448
diff changeset
   722
      order = self._params.get('order') or []
a023b71ce125 fixed critical errors related to survey subclassing and renaming of deadline and opening properties, and surveys in sidebar are now marked as such and have a red color if untaken
james@jamess-macbook-air.local
parents: 2448
diff changeset
   723
      idx = self._params.get('idx') or 0
a023b71ce125 fixed critical errors related to survey subclassing and renaming of deadline and opening properties, and surveys in sidebar are now marked as such and have a red color if untaken
james@jamess-macbook-air.local
parents: 2448
diff changeset
   724
a023b71ce125 fixed critical errors related to survey subclassing and renaming of deadline and opening properties, and surveys in sidebar are now marked as such and have a red color if untaken
james@jamess-macbook-air.local
parents: 2448
diff changeset
   725
      records = results_logic.getForFields(filter=filter, limit=limit,
a023b71ce125 fixed critical errors related to survey subclassing and renaming of deadline and opening properties, and surveys in sidebar are now marked as such and have a red color if untaken
james@jamess-macbook-air.local
parents: 2448
diff changeset
   726
                                        offset=offset, order=order)
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   727
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   728
    updates = dicts.rename(params, params['list_params'])
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   729
    context.update(updates)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   730
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   731
    context['results'] = records, records
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   732
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   733
    template = 'soc/survey/results_page.html'
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   734
    return responses.respond(request, template, context=context)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   735
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   736
  @decorators.merge_params
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   737
  @decorators.check_access
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   738
  def exportSerialized(self, request, access_type, page_name=None,
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   739
                       params=None, **kwargs):
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   740
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   741
    sur, context = self.getContextEntity(request, page_name, params, kwargs)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   742
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   743
    if context is None:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   744
      # user cannot see this page, return error response
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   745
      return sur
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   746
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   747
    json = sur.toDict()
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   748
    json.update(dict((f, str(getattr(sur, f))) for f in PLAIN.split()))
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   749
    static = ((f, str(getattr(sur, f).link_id)) for f in FIELDS.split())
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   750
    json.update(dict(static))
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   751
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   752
    dynamic = sur.survey_content.dynamic_properties()
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   753
    content = ((prop, getattr(sur.survey_content, prop)) for prop in dynamic)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   754
    json['survey_content'] = dict(content)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   755
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   756
    schema =  sur.survey_content.schema
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   757
    json['survey_content']['schema'] = eval(sur.survey_content.schema)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   758
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   759
    data = simplejson.dumps(json, indent=2)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   760
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   761
    return self.json(request, data=json)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   762
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   763
  def importSerialized(self, request, fields, user):
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   764
    json = request.POST['serialized']
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   765
    json = simplejson.loads(json)['data']
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   766
    survey_content = json.pop('survey_content')
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   767
    schema = survey_content.pop('schema')
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   768
    del json['author']
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   769
    del json['created']
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   770
    del json['modified']
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   771
    #del json['is_featured']
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   772
    # keywords can't be unicode
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   773
    keywords = {}
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   774
    for key, val in json.items():
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   775
      keywords[str(key)] = val
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   776
    if 'is_featured' in keywords:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   777
      keywords['is_featured'] = eval(keywords['is_featured'])
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   778
    return keywords, schema, survey_content
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   779
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   780
  def getContextEntity(self, request, page_name, params, kwargs):
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   781
    context = responses.getUniversalContext(request)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   782
    responses.useJavaScript(context, params['js_uses_all'])
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   783
    context['page_name'] = page_name
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   784
    entity = None
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   785
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   786
    # TODO(ajaksu) there has to be a better way in this universe to get these
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   787
    kwargs['prefix'] = 'program'
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   788
    kwargs['link_id'] = request.path.split('/')[-1]
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   789
    kwargs['scope_path'] = '/'.join(request.path.split('/')[4:-1])
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   790
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   791
    entity = survey_logic.getFromKeyFieldsOr404(kwargs)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   792
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   793
    if not self._public(request, entity, context):
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   794
      error = out_of_band.Error('')
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   795
      error = responses.errorResponse(
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   796
          error, request, template=params['error_public'], context=context)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   797
      return error, None
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   798
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   799
    return entity, context
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   800
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   801
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   802
view = View()
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   803
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   804
admin = decorators.view(view.admin)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   805
create = decorators.view(view.create)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   806
edit = decorators.view(view.edit)
2475
dfb9249d91b6 Added skeleton for the take view and its template.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2474
diff changeset
   807
export = decorators.view(view.export)
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   808
delete = decorators.view(view.delete)
2475
dfb9249d91b6 Added skeleton for the take view and its template.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2474
diff changeset
   809
json = decorators.view(view.exportSerialized)
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   810
list = decorators.view(view.list)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   811
public = decorators.view(view.public)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   812
pick = decorators.view(view.pick)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   813
results = decorators.view(view.viewResults)
2475
dfb9249d91b6 Added skeleton for the take view and its template.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2474
diff changeset
   814
take = decorators.view(view.take)