app/soc/views/models/survey.py
author Lennard de Rijk <ljvderijk@gmail.com>
Wed, 01 Jul 2009 08:29:49 +0200
changeset 2469 ac14b088f89e
parent 2467 f46cff8d676b
child 2471 04f13163c1e8
permissions -rw-r--r--
Returned access checks in Survey View to the right state. This got bugged in James's commt.
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 csv
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    27
import datetime
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    28
import re
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    29
import StringIO
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    30
import string
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    31
from django import forms
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    32
from django import http
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    33
from django.utils import simplejson
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    34
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
    35
from google.appengine.ext import db
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
    36
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    37
from soc.cache import home
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    38
from soc.logic import cleaning
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    39
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
    40
from soc.logic.models.survey import logic as survey_logic
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    41
from soc.logic.models.survey import GRADES
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    42
from soc.logic.models.user import logic as user_logic
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    43
from soc.models.survey import Survey
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    44
from soc.models.survey_record import SurveyRecord
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    45
from soc.models.user import User
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    46
from soc.views import out_of_band
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    47
from soc.views.helper import access
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    48
from soc.views.helper import decorators
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    49
from soc.views.helper import redirects
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    50
from soc.views.helper import responses
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    51
from soc.views.helper import surveys
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    52
from soc.views.helper import widgets
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    53
from soc.views.models import base
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    54
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    55
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    56
CHOICE_TYPES = set(('selection', 'pick_multi', 'choice', 'pick_quant'))
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    57
TEXT_TYPES = set(('long_answer', 'short_answer'))
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    58
PROPERTY_TYPES = tuple(CHOICE_TYPES) + tuple(TEXT_TYPES)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    59
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    60
_short_answer = ("Short Answer",
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    61
                "Less than 40 characters. Rendered as a text input. "
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    62
                "It's possible to add a free form question (Content) "
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    63
                "and a in-input propmt/example text.")
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    64
_choice = ("Selection",
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    65
           "Can be set as a single choice (selection) or multiple choice "
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    66
           "(pick_multi) question. Rendered as a select (single choice) "
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    67
           "or a group of checkboxes (multiple choice). It's possible to "
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    68
           "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
    69
           "as wanted. Each option can be edited (double-click), deleted "
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    70
           "(click on (-) button) or reordered (drag and drop).")
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    71
_long_answer = ("Long Answer",
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    72
                "Unlimited length, auto-growing field. endered as a textarea. "
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    73
                 "It's possible to add a free form question (Content) and "
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    74
                 "an in-input prompt/example text.")
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    75
QUESTION_TYPES = dict(short_answer=_short_answer, long_answer=_long_answer,
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    76
                      choice=_choice)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    77
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    78
# for to_csv and View.exportSerialized
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    79
FIELDS = 'author modified_by'
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    80
PLAIN = 'is_featured content created modified'
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    81
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    82
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    83
class View(base.View):
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    84
  """View methods for the Survey model.
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    85
  """
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    86
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    87
  def __init__(self, params=None):
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    88
    """Defines the fields and methods required for the base View class
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    89
    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
    90
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    91
    Params:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    92
      params: a dict with params for this View
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    93
    """
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    94
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    95
    # TODO: read/write access needs to match survey
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    96
    # TODO: usage requirements
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    97
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    98
    rights = access.Checker(params)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    99
    rights['any_access'] = ['allow']
2469
ac14b088f89e Returned access checks in Survey View to the right state.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2467
diff changeset
   100
    rights['show'] = [('checkIsSurveyReadable', survey_logic)]
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   101
    rights['create'] = ['checkIsUser']
2469
ac14b088f89e Returned access checks in Survey View to the right state.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2467
diff changeset
   102
    rights['edit'] = [('checkIsSurveyWritable', suvery_logic)]
ac14b088f89e Returned access checks in Survey View to the right state.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2467
diff changeset
   103
    rights['delete'] = [('checkIsSurveyWritable', survey_logic)]
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   104
    rights['list'] = ['checkDocumentList']
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   105
    rights['pick'] = ['checkDocumentPick']
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   106
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   107
    new_params = {}
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   108
    new_params['logic'] = survey_logic
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   109
    new_params['rights'] = rights
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   110
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   111
    new_params['name'] = "Survey"
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   112
    new_params['pickable'] = True
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   113
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   114
    new_params['extra_django_patterns'] = [
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   115
        (r'^%(url_name)s/(?P<access_type>activate)/%(scope)s$',
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   116
         'soc.views.models.%(module_name)s.activate',
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   117
         'Activate grades for %(name)s'),
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   118
         (r'^%(url_name)s/(?P<access_type>json)/%(scope)s$',
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   119
         'soc.views.models.%(module_name)s.json',
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   120
         'Export %(name)s as JSON'),
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   121
        (r'^%(url_name)s/(?P<access_type>results)/%(scope)s$',
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   122
         'soc.views.models.%(module_name)s.results',
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   123
         '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
   124
        (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
   125
         '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
   126
         'View survey results for user'),
2435
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
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   129
    new_params['export_content_type'] = 'text/text'
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   130
    new_params['export_extension'] = '.csv'
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   131
    new_params['export_function'] = to_csv
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   132
    new_params['delete_redirect'] = '/'
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   133
    new_params['list_key_order'] = [
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   134
        'link_id', 'scope_path', 'name', 'short_name', 'title',
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   135
        'content', 'prefix','read_access','write_access']
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   136
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   137
    new_params['edit_template'] = 'soc/survey/edit.html'
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   138
    new_params['create_template'] = 'soc/survey/edit.html'
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   139
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   140
    # TODO which one of these are leftovers from Document?
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   141
    new_params['no_create_raw'] = True
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   142
    new_params['no_create_with_scope'] = True
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   143
    new_params['no_create_with_key_fields'] = True
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   144
    new_params['no_list_raw'] = True
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   145
    new_params['sans_link_id_create'] = True
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   146
    new_params['sans_link_id_list'] = True
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   147
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   148
    new_params['create_dynafields'] = [
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   149
        {'name': 'link_id',
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   150
         'base': forms.fields.CharField,
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   151
         'label': 'Survey Link ID',
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   152
         },
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
    # 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
   156
    new_params['create_extra_dynaproperties'] = {
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   157
        #'survey_content': forms.fields.CharField(widget=surveys.EditSurvey(),
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   158
                                                 #required=False),
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   159
        'survey_html': forms.fields.CharField(widget=forms.HiddenInput,
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   160
                                              required=False),
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   161
        'scope_path': forms.fields.CharField(widget=forms.HiddenInput,
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   162
                                             required=True),
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   163
        'prefix': forms.fields.CharField(widget=widgets.ReadOnlyInput(),
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   164
                                        required=True),
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   165
        'clean_content': cleaning.clean_html_content('content'),
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   166
        'clean_link_id': cleaning.clean_link_id('link_id'),
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   167
        'clean_scope_path': cleaning.clean_scope_path('scope_path'),
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   168
        'clean': cleaning.validate_document_acl(self, True),
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   169
        }
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
    new_params['extra_dynaexclude'] = ['author', 'created', 'content',
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   172
                                       'home_for', 'modified_by', 'modified',
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   173
                                       'take_survey', 'survey_content']
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   174
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   175
    new_params['edit_extra_dynaproperties'] = {
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   176
        'doc_key_name': forms.fields.CharField(widget=forms.HiddenInput),
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   177
        'created_by': forms.fields.CharField(widget=widgets.ReadOnlyInput(),
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   178
                                             required=False),
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   179
        'last_modified_by': forms.fields.CharField(
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   180
                                widget=widgets.ReadOnlyInput(), required=False),
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   181
        'clean': cleaning.validate_document_acl(self),
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   182
        }
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   183
2464
64b241a237b2 Merge params from inheriting Survey views so functionality can be reused.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2463
diff changeset
   184
    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
   185
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   186
    super(View, self).__init__(params=params)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   187
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   188
  def list(self, request, access_type, page_name=None, params=None,
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   189
           filter=None, order=None, **kwargs):
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   190
    """See base.View.list.
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   191
    """
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
    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
   194
                                  params=params, filter=kwargs)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   195
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   196
  def _public(self, request, entity, context):
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   197
    """Survey taking and result display handler.
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   198
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
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
   205
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   206
    -- Taking Survey Pages Are Not 'Public' --
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   207
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   208
    For surveys, the "public" page is actually the access-protected
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   209
    survey-taking page.
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   210
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   211
    -- SurveyProjectGroups --
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   212
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   213
    Each survey can be taken once per user per project.
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   214
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   215
    This means that MidtermGSOC2009 can be taken once for a student
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   216
    for a project, and once for a mentor for each project they are
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   217
    mentoring.
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
    The project selected while taking a survey determines how this_user
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   220
    SurveyRecord will be linked to other SurveyRecords.
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
    --- Deadlines ---
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   223
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
   224
    A survey_end can also be used as a conditional for updating values,
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   225
    we have a special read_only UI and a check on the POST handler for this.
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   226
    Passing read_only=True here allows one to fetch the read_only 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
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   229
    # check ACL
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   230
    rights = self._params['rights']
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   231
    rights.checkIsSurveyReadable({'key_name': entity.key().name(),
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   232
                                  'prefix': entity.prefix,
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   233
                                  'scope_path': entity.scope_path,
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   234
                                  'link_id': entity.link_id,},
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   235
                                 'key_name')
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   236
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   237
    survey = entity
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   238
    user = user_logic.getForCurrentAccount()
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   239
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   240
    status = self.getStatus(request, context, user, survey)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   241
    read_only, can_write, not_ready = status
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   242
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   243
    # If user can edit this survey and is requesting someone else's results,
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   244
    # in a read-only request, we fetch them.
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   245
    if can_write and read_only and 'user_results' in request.GET:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   246
      user = user_logic.getFromKeyNameOr404(request.GET['user_results'])
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
    if not_ready and not can_write:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   249
      context['notice'] = "No survey available."
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   250
      return False
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   251
    elif not_ready:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   252
      return False
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   253
    else:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   254
      # check for existing survey_record
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   255
      record_query = SurveyRecord.all(
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   256
      ).filter("user =", user
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   257
      ).filter("survey =", survey)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   258
      # get project from GET arg
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   259
      if request._get.get('project'):
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   260
        import soc.models.student_project
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   261
        project = soc.models.student_project.StudentProject.get(
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   262
        request._get.get('project'))
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   263
        record_query = record_query.filter("project =", project)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   264
      else:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   265
        project = None
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   266
      survey_record = record_query.get()
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
      if len(request.POST) < 1 or read_only or not_ready:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   269
         # not submitting completed survey OR we're ignoring late submission
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   270
        pass
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   271
      else:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   272
        # save/update the submitted survey
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   273
        context['notice'] = "Survey Submission Saved"
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
   274
        survey_record = survey_logic.updateSurveyRecord(user, survey,
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
   275
        survey_record, request.POST)
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   276
    survey_content = survey.survey_content
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   277
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   278
    if not survey_record and read_only:
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
   279
      # no recorded answers, we're either past survey_end or want to see answers
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   280
      is_same_user = user.key() == user_logic.getForCurrentAccount().key()
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   281
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   282
      if not can_write or not is_same_user:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   283
        # If user who can edit looks at her own taking page, show the default
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   284
        # form as readonly. Otherwise, below, show nothing.
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   285
        context["notice"] = "There are no records for this survey and user."
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   286
        return False
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   287
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   288
    survey_form = surveys.SurveyForm(survey_content=survey_content,
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   289
                                     this_user=user,
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   290
                                     project=project,
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
   291
                                     survey_logic=self._params['logic'],
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   292
                                     survey_record=survey_record,
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   293
                                     read_only=read_only,
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   294
                                     editing=False)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   295
    survey_form.getFields()
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   296
    if 'evaluation' in survey.taking_access:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   297
      survey_form = surveys.getRoleSpecificFields(survey, user,
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   298
                                  project, survey_form, survey_record)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   299
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   300
    # set help and status text
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   301
    self.setHelpStatus(context, read_only,
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   302
    survey_record, survey_form, survey)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   303
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   304
    if not context['survey_form']:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   305
      access_tpl = "Access Error: This Survey Is Limited To %s"
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   306
      context["notice"] = access_tpl % string.capwords(survey.taking_access)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   307
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   308
    context['read_only'] = read_only
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   309
    context['project'] = project
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   310
    return True
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   311
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   312
  def getStatus(self, request, context, user, survey):
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
   313
    """Determine if we're past survey_end or before survey_start, check user rights.
2435
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
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   316
    read_only = (context.get("read_only", False) or
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   317
                 request.GET.get("read_only", False) or
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   318
                 request.POST.get("read_only", False)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   319
                 )
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   320
    now = datetime.datetime.now()
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   321
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
   322
    # check survey_end, see check for survey_start below
2440
05c430d1c147 Renamed opening to survey_start and deadline to survey_end.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2439
diff changeset
   323
    if survey.survey_end and now > survey.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
   324
      # are we already passed the survey_end?
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   325
      context["notice"] = "The Deadline For This Survey Has Passed"
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   326
      read_only = True
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   327
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   328
    # check if user can edit this survey
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   329
    params = dict(prefix=survey.prefix, scope_path=survey.scope_path)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   330
    checker = access.rights_logic.Checker(survey.prefix)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   331
    roles = checker.getMembership(survey.write_access)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   332
    rights = self._params['rights']
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   333
    can_write = access.Checker.hasMembership(rights, roles, params)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   334
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
   335
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   336
    not_ready = False
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
   337
    # check if we're past the survey_start date
2440
05c430d1c147 Renamed opening to survey_start and deadline to survey_end.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2439
diff changeset
   338
    if survey.survey_start and now < survey.survey_start:
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   339
      not_ready = True
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   340
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
   341
      # only users that can edit a survey should see it before survey_start
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   342
      if not can_write:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   343
        context["notice"] = "There is no such survey available."
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   344
        return False
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   345
      else:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   346
        context["notice"] = "This survey is not open for taking yet."
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   347
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   348
    return read_only, can_write, not_ready
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   349
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   350
  def setHelpStatus(self, context, read_only, survey_record, survey_form,
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   351
                    survey):
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   352
    """Set help_text and status for template use.
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   353
    """
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   354
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   355
    if not read_only:
2440
05c430d1c147 Renamed opening to survey_start and deadline to survey_end.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2439
diff changeset
   356
      if not survey.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
   357
        survey_end_text = ""
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   358
      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
   359
        survey_end_text = " by " + str(
2440
05c430d1c147 Renamed opening to survey_start and deadline to survey_end.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2439
diff changeset
   360
      survey.survey_end.strftime("%A, %d. %B %Y %I:%M%p"))
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   361
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   362
      if survey_record:
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
   363
        help_text = "Edit and re-submit this survey" + survey_end_text + "."
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   364
        status = "edit"
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   365
      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
   366
        help_text = "Please complete this survey" + survey_end_text + "."
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   367
        status = "create"
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   368
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   369
    else:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   370
      help_text = "Read-only view."
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   371
      status = "view"
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   372
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   373
    survey_data = dict(survey_form=survey_form, status=status,
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   374
                                     help_text=help_text)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   375
    context.update(survey_data)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   376
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   377
  def _editContext(self, request, context):
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   378
    """Performs any required processing on the context for edit pages.
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   379
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   380
    Args:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   381
      request: the django request object
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   382
      context: the context dictionary that will be used
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
      Adds list of SurveyRecord results as supplement to view.
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   385
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   386
      See surveys.SurveyResults for details.
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
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   389
    if not getattr(self, '_entity', None):
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   390
      return
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
    results = surveys.SurveyResults()
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   393
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   394
    context['survey_records'] = results.render(self._entity, self._params,
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   395
                                               filter={})
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   396
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   397
    super(View, self)._editContext(request, context)
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
  def _editPost(self, request, entity, fields):
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   400
    """See base.View._editPost().
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   401
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   402
    Processes POST request items to add new dynamic field names,
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   403
    question types, and default prompt values to SurveyContent model.
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
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   406
    user = user_logic.getForCurrentAccount()
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   407
    schema = {}
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   408
    survey_fields = {}
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 not entity:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   411
      # new Survey
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   412
      if 'serialized' in request.POST:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   413
        fields, schema, survey_fields = self.importSerialized(request, fields, user)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   414
      fields['author'] = user
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   415
    else:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   416
      fields['author'] = entity.author
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   417
      schema = self.loadSurveyContent(schema, survey_fields, entity)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   418
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   419
    # remove deleted properties from the model
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   420
    self.deleteQuestions(schema, survey_fields, request.POST)
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
    # add new text questions and re-build choice questions
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   423
    self.getRequestQuestions(schema, survey_fields, request.POST)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   424
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   425
    # get schema options for choice questions
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   426
    self.getSchemaOptions(schema, survey_fields, request.POST)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   427
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   428
    survey_content = getattr(entity,'survey_content', None)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   429
    # create or update a SurveyContent for this Survey
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   430
    survey_content = survey_logic.createSurvey(survey_fields, schema,
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   431
                                                survey_content=survey_content)
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
    # 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
   434
    if entity:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   435
      entity.modified_by = user
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   436
      entity.survey_content = survey_content
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   437
      db.put(entity)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   438
    else:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   439
      fields['survey_content'] = survey_content
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   440
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   441
    fields['modified_by'] = user
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   442
    super(View, self)._editPost(request, entity, fields)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   443
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   444
  def loadSurveyContent(self, schema, survey_fields, entity):
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   445
    """Populate the schema dict and get text survey questions.
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   446
    """
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   447
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   448
    if hasattr(entity, 'survey_content'):
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
      # there is a SurveyContent already
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   451
      survey_content = entity.survey_content
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   452
      schema = eval(survey_content.schema)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   453
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   454
      for question_name in survey_content.dynamic_properties():
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   455
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   456
        # get the current questions from the SurveyContent
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   457
        if question_name not in schema:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   458
          continue
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   459
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   460
        if schema[question_name]['type'] not in CHOICE_TYPES:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   461
          # Choice questions are always regenerated from request, see
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   462
          # self.get_request_questions()
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   463
          question = getattr(survey_content, question_name)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   464
          survey_fields[question_name] = question
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 schema
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   467
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   468
  def deleteQuestions(self, schema, survey_fields, POST):
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   469
    """Process the list of questions to delete, from a hidden input.
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   470
    """
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   471
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   472
    deleted = POST.get('__deleted__', '')
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   473
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   474
    if deleted:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   475
      deleted = deleted.split(',')
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   476
      for field in deleted:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   477
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   478
        if field in schema:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   479
          del schema[field]
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   480
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   481
        if field in survey_fields:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   482
          del survey_fields[field]
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   483
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   484
  def getRequestQuestions(self, schema, survey_fields, POST):
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   485
    """Get fields from request.
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   486
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   487
    We use two field/question naming and processing schemes:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   488
      - 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
   489
        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
   490
        deletions and additions.
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   491
      - Text questions only have special survey__* names on creation, afterwards
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   492
        they are loaded from the SurveyContent dynamic properties.
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   493
    """
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   494
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   495
    for key, value in POST.items():
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   496
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   497
      if key.startswith('id_'):
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   498
        # Choice question fields, they are always generated from POST contents,
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   499
        # as their 'content' is editable and they're reorderable. Also get
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   500
        # its field index for handling reordering fields later.
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   501
        name, number = key[3:].replace('__field', '').rsplit('_', 1)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   502
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   503
        if name not in schema:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   504
          if 'NEW_' + name in POST:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   505
            # new Choice question, set generic type and get its index
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   506
            schema[name] = {'type': 'choice'}
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   507
            schema[name]['index'] = int(POST['index_for_' + name])
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   508
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   509
        if name in schema and schema[name]['type'] in CHOICE_TYPES:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   510
          # build an index:content dictionary
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   511
          if name in survey_fields:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   512
            if value not in survey_fields[name]:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   513
              survey_fields[name][int(number)] = value
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   514
          else:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   515
            survey_fields[name] = {int(number): value}
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   516
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   517
      elif key.startswith('survey__'): # new Text question
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   518
        # 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
   519
        prefix = re.compile('survey__([0-9]{1,3})__')
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   520
        prefix_match = re.match(prefix, key)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   521
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   522
        index = prefix_match.group(0).replace('survey', '').replace('__','')
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   523
        index = int(index)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   524
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   525
        field_name = prefix.sub('', key)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   526
        field = 'id_' + key
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   527
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   528
        for ptype in PROPERTY_TYPES:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   529
          # should only match one
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   530
          if ptype + "__" in field_name:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   531
            field_name = field_name.replace(ptype + "__", "")
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   532
            schema[field_name] = {}
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   533
            schema[field_name]["index"] = index
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   534
            schema[field_name]["type"] = ptype
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   535
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   536
        survey_fields[field_name] = value
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   537
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   538
  def getSchemaOptions(self, schema, survey_fields, POST):
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   539
    """Get question, type, rendering and option order for choice questions.
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   540
    """
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   541
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   542
    RENDER = {'checkboxes': 'multi_checkbox', 'select': 'single_select',
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   543
              'radio_buttons': 'quant_radio'}
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   544
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   545
    RENDER_TYPES = {'select': 'selection',
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   546
                    'checkboxes': 'pick_multi',
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   547
                    'radio_buttons': 'pick_quant' }
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   548
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   549
    for key in schema:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   550
      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
   551
        render_for = 'render_for_' + key
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   552
        if render_for in POST:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   553
          schema[key]['render'] = RENDER[POST[render_for]]
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   554
          schema[key]['type'] = RENDER_TYPES[POST[render_for]]
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   555
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   556
        # handle reordering fields
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   557
        ordered = False
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   558
        order = 'order_for_' + key
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   559
        if order in POST and isinstance(survey_fields[key], dict):
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   560
          order = POST[order]
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   561
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   562
          # '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
   563
          # 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
   564
          order = order.replace('id-li-%s[]=' % key, '')
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   565
          order = order.split('&')
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   566
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   567
          if len(order) == len(survey_fields[key]) and order[0]:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   568
            order = [int(number) for number in order]
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   569
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   570
            if set(order) == set(survey_fields[key]):
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   571
              survey_fields[key] = [survey_fields[key][i] for i in order]
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   572
              ordered = True
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   573
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   574
          if not ordered:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   575
            # we don't have a good ordering to use
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   576
            ordered = sorted(survey_fields[key].items())
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   577
            survey_fields[key] = [value for index, value in ordered]
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   578
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   579
      # set 'question' entry (free text label for question) in schema
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   580
      question_for = 'NEW_' + key
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   581
      if question_for in POST:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   582
        schema[key]["question"] = POST[question_for]
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   583
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   584
  def createGet(self, request, context, params, seed):
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   585
    """Pass the question types for the survey creation template.
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   586
    """
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   587
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   588
    context['question_types'] = QUESTION_TYPES
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   589
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   590
    # avoid spurious results from showing on creation
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   591
    context['new_survey'] = True
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   592
    return super(View, self).createGet(request, context, params, seed)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   593
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   594
  def editGet(self, request, entity, context, params=None):
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   595
    """Process GET requests for the specified entity.
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   596
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   597
    Builds the SurveyForm that represents the Survey question contents.
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   598
    """
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   599
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   600
    # TODO(ajaksu) Move CHOOSE_A_PROJECT_FIELD and CHOOSE_A_GRADE_FIELD
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   601
    # to template.
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   602
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   603
    CHOOSE_A_PROJECT_FIELD = """<tr class="role-specific">
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   604
    <th><label>Choose Project:</label></th>
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   605
    <td>
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   606
      <select disabled="TRUE" id="id_survey__NA__selection__project"
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   607
        name="survey__1__selection__see">
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   608
          <option>Survey Taker's Projects For This Program</option></select>
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   609
     </td></tr>
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   610
     """
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   611
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   612
    CHOOSE_A_GRADE_FIELD = """<tr class="role-specific">
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   613
    <th><label>Assign Grade:</label></th>
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   614
    <td>
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   615
      <select disabled=TRUE id="id_survey__NA__selection__grade"
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   616
       name="survey__1__selection__see">
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   617
        <option>Pass/Fail</option>
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   618
      </select></td></tr>
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   619
    """
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   620
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   621
    self._entity = entity
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   622
    survey_content = entity.survey_content
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   623
    user = user_logic.getForCurrentAccount()
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   624
    # no project or survey_record needed for survey prototype
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   625
    project = None
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   626
    survey_record = None
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   627
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   628
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   629
    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
   630
                                     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
   631
                                     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
   632
                                     survey_record=survey_record,
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   633
                                     editing=True, read_only=False)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   634
    survey_form.getFields()
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   635
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   636
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   637
    # activate grades flag -- TODO: Can't configure notice on edit page
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   638
    if request._get.get('activate'):
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   639
      context['notice'] = "Evaluation Grades Have Been Activated"
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   640
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   641
    local = dict(survey_form=survey_form, question_types=QUESTION_TYPES,
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   642
                survey_h=entity.survey_content)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   643
    context.update(local)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   644
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   645
    params['edit_form'] = 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
   646
    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
   647
      # 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
   648
      context["passed_survey_end"] = True
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   649
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   650
    return super(View, self).editGet(request, entity, context, params=params)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   651
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   652
  def getMenusForScope(self, entity, params):
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
   653
    """List featured surveys if after the survey_start date and before survey_end.
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   654
    """
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   655
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   656
    # only list surveys for registered users
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   657
    user = user_logic.getForCurrentAccount()
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   658
    if not user:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   659
      return []
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   660
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   661
    filter = {
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   662
        'prefix' : params['url_name'],
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   663
        'scope_path': entity.key().id_or_name(),
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   664
        'is_featured': True,
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
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   667
    entities = self._logic.getForFields(filter)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   668
    submenus = []
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   669
    now = datetime.datetime.now()
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   670
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   671
    # cache ACL
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   672
    survey_rights = {}
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
    # add a link to all featured documents
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   675
    for entity in entities:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   676
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   677
      # only list those surveys the user can read
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   678
      if entity.read_access not in survey_rights:
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
        params = dict(prefix=entity.prefix, scope_path=entity.scope_path,
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   681
                      link_id=entity.link_id, user=user)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   682
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   683
        # TODO(ajaksu) use access.Checker.checkIsSurveyReadable
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   684
        checker = access.rights_logic.Checker(entity.prefix)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   685
        roles = checker.getMembership(entity.read_access)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   686
        rights = self._params['rights']
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   687
        can_read = access.Checker.hasMembership(rights, roles, params)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   688
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   689
        # cache ACL for a given entity.read_access
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   690
        survey_rights[entity.read_access] = can_read
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   691
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   692
        if not can_read:
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
          pass#continue
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   694
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   695
      elif not survey_rights[entity.read_access]:
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
        pass#continue
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   697
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
   698
      # omit if either before survey_start or after survey_end
2440
05c430d1c147 Renamed opening to survey_start and deadline to survey_end.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2439
diff changeset
   699
      if entity.survey_start and entity.survey_start > now:
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
   700
        pass#continue
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   701
2440
05c430d1c147 Renamed opening to survey_start and deadline to survey_end.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2439
diff changeset
   702
      if entity.survey_end and entity.survey_end < now:
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
   703
        pass#continue
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   704
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
   705
      taken_status = ""
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
      taken_status = "(new)"
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   707
      #TODO only if a document is readable it might be added
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   708
      submenu = (redirects.getPublicRedirect(entity, self._params),
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
   709
      'Survey ' +  taken_status + ': ' + entity.short_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
   710
      'show')
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   711
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   712
      submenus.append(submenu)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   713
    return submenus
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   714
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   715
  def activate(self, request, **kwargs):
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   716
    """This is a hack to support the 'Enable grades' button.
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   717
    """
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   718
    self.activateGrades(request)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   719
    redirect_path = request.path.replace('/activate/', '/edit/') + '?activate=1'
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   720
    return http.HttpResponseRedirect(redirect_path)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   721
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   722
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   723
  def activateGrades(self, request, **kwargs):
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   724
    """Updates SurveyRecord's grades for a given Survey.
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   725
    """
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   726
    survey_key_name = survey_logic.getKeyNameFromPath(request.path)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   727
    survey = Survey.get_by_key_name(survey_key_name)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   728
    survey_logic.activateGrades(survey)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   729
    return
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
  @decorators.merge_params
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   732
  @decorators.check_access
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   733
  def viewResults(self, request, access_type, page_name=None,
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   734
                  params=None, **kwargs):
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   735
    """View for SurveyRecord and SurveyRecordGroup.
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   736
    """
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   737
2467
f46cff8d676b Solved some merging problems and use the getRecordLogic where needed.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2464
diff changeset
   738
    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
   739
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   740
    user = user_logic.getForCurrentAccount()
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   741
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
   742
    # 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
   743
    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
   744
      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
   745
      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
   746
      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
   747
      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
   748
      context['page_name'] = u'Your survey records.'
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   749
    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
   750
      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
   751
                                              params, kwargs)
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   752
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
   753
      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
   754
        # 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
   755
        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
   756
      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
   757
      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
   758
      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
   759
      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
   760
        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
   761
                                      '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
   762
                                      '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
   763
                                      '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
   764
                                     '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
   765
        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
   766
      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
   767
        pass
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   768
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
   769
      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
   770
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
   771
      # 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
   772
      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
   773
        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
   774
      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
   775
        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
   776
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
   777
      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
   778
      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
   779
      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
   780
      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
   781
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
   782
      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
   783
                                        offset=offset, order=order)
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   784
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   785
    updates = dicts.rename(params, params['list_params'])
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   786
    context.update(updates)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   787
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   788
    context['results'] = records, records
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   789
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   790
    template = 'soc/survey/results_page.html'
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   791
    return responses.respond(request, template, context=context)
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
  @decorators.merge_params
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   794
  @decorators.check_access
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   795
  def exportSerialized(self, request, access_type, page_name=None,
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   796
                       params=None, **kwargs):
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   797
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   798
    sur, context = self.getContextEntity(request, page_name, params, kwargs)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   799
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   800
    if context is None:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   801
      # user cannot see this page, return error response
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   802
      return sur
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
    json = sur.toDict()
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   805
    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
   806
    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
   807
    json.update(dict(static))
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   808
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   809
    dynamic = sur.survey_content.dynamic_properties()
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   810
    content = ((prop, getattr(sur.survey_content, prop)) for prop in dynamic)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   811
    json['survey_content'] = dict(content)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   812
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   813
    schema =  sur.survey_content.schema
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   814
    json['survey_content']['schema'] = eval(sur.survey_content.schema)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   815
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   816
    data = simplejson.dumps(json, indent=2)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   817
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   818
    return self.json(request, data=json)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   819
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   820
  def importSerialized(self, request, fields, user):
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   821
    json = request.POST['serialized']
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   822
    json = simplejson.loads(json)['data']
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   823
    survey_content = json.pop('survey_content')
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   824
    schema = survey_content.pop('schema')
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   825
    del json['author']
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   826
    del json['created']
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   827
    del json['modified']
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   828
    #del json['is_featured']
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   829
    # keywords can't be unicode
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   830
    keywords = {}
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   831
    for key, val in json.items():
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   832
      keywords[str(key)] = val
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   833
    if 'is_featured' in keywords:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   834
      keywords['is_featured'] = eval(keywords['is_featured'])
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   835
    return keywords, schema, survey_content
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   836
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   837
  def getContextEntity(self, request, page_name, params, kwargs):
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   838
    context = responses.getUniversalContext(request)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   839
    responses.useJavaScript(context, params['js_uses_all'])
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   840
    context['page_name'] = page_name
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   841
    entity = None
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   842
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   843
    # 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
   844
    kwargs['prefix'] = 'program'
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   845
    kwargs['link_id'] = request.path.split('/')[-1]
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   846
    kwargs['scope_path'] = '/'.join(request.path.split('/')[4:-1])
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   847
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   848
    entity = survey_logic.getFromKeyFieldsOr404(kwargs)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   849
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   850
    if not self._public(request, entity, context):
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   851
      error = out_of_band.Error('')
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   852
      error = responses.errorResponse(
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   853
          error, request, template=params['error_public'], context=context)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   854
      return error, None
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   855
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   856
    return entity, context
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   857
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   858
class HelperForm(object):
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   859
  """Thin wrapper for adding values to params['edit_form'].fields.
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   860
  """
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   861
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   862
  def __init__(self, form=None):
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   863
    """Store the edit_form.
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   864
    """
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   865
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   866
    self.form = form
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   867
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   868
  def __call__(self, instance=None):
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   869
    """Transparently instantiate and add initial values to the edit_form.
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   870
    """
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   871
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   872
    form = self.form(instance=instance)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   873
    form.fields['created_by'].initial = instance.author.name
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   874
    form.fields['last_modified_by'].initial = instance.modified_by.name
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   875
    form.fields['doc_key_name'].initial = instance.key().id_or_name()
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   876
    return form
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   877
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   878
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   879
def _get_csv_header(sur):
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   880
  """CSV header helper, needs support for comment lines in CSV.
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   881
  """
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   882
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   883
  tpl = '# %s: %s\n'
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   884
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   885
  # add static properties
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   886
  fields = ['# Melange Survey export for \n#  %s\n#\n' % sur.title]
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   887
  fields += [tpl % (k,v) for k,v in sur.toDict().items()]
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   888
  fields += [tpl % (f, str(getattr(sur, f))) for f in PLAIN.split()]
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   889
  fields += [tpl % (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
   890
  fields.sort()
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   891
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   892
  # add dynamic properties
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   893
  fields += ['#\n#---\n#\n']
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   894
  dynamic = sur.survey_content.dynamic_properties()
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   895
  dynamic = [(prop, getattr(sur.survey_content, prop)) for prop in dynamic]
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   896
  fields += [tpl % (k,v) for k,v in sorted(dynamic)]
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   897
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   898
  # add schema
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   899
  fields += ['#\n#---\n#\n']
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   900
  schema =  sur.survey_content.schema
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   901
  indent = '},\n#' + ' ' * 9
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   902
  fields += [tpl % ('Schema', schema.replace('},', indent)) + '#\n']
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   903
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   904
  return ''.join(fields).replace('\n', '\r\n')
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   905
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   906
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   907
def _get_records(recs, props):
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   908
  """Fetch properties from SurveyRecords for CSV export.
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   909
  """
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   910
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   911
  records = []
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   912
  props = props[1:]
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   913
  for rec in recs:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   914
    values = tuple(getattr(rec, prop, None) for prop in props)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   915
    leading = (rec.user.link_id,)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   916
    records.append(leading + values)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   917
  return records
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   918
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   919
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   920
def to_csv(survey):
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   921
  """CSV exporter.
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   922
  """
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   923
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   924
  # get header and properties
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   925
  header = _get_csv_header(survey)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   926
  leading = ['user', 'created', 'modified']
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   927
  properties = leading + survey.survey_content.orderedProperties()
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   928
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   929
  try:
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
   930
    first = survey.survey_records.run().next()
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   931
  except StopIteration:
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   932
    # bail out early if survey_records.run() is empty
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   933
    return header, survey.link_id
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   934
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   935
  # generate results list
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
   936
  recs = survey.survey_records.run()
2435
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   937
  recs = _get_records(recs, properties)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   938
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   939
  # write results to CSV
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   940
  output = StringIO.StringIO()
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   941
  writer = csv.writer(output)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   942
  writer.writerow(properties)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   943
  writer.writerows(recs)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   944
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   945
  return header + output.getvalue(), survey.link_id
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   946
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   947
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   948
view = View()
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   949
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   950
admin = decorators.view(view.admin)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   951
create = decorators.view(view.create)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   952
edit = decorators.view(view.edit)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   953
delete = decorators.view(view.delete)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   954
list = decorators.view(view.list)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   955
public = decorators.view(view.public)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   956
export = decorators.view(view.export)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   957
pick = decorators.view(view.pick)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   958
activate = decorators.view(view.activate)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   959
results = decorators.view(view.viewResults)
dd16e9b3c2d0 Added View for Surveys.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   960
json = decorators.view(view.exportSerialized)