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