app/soc/models/survey.py
changeset 2428 8ca9f32d3fc4
child 2440 05c430d1c147
equal deleted inserted replaced
2427:0dee663d6582 2428:8ca9f32d3fc4
       
     1 #!/usr/bin/python2.5
       
     2 #
       
     3 # Copyright 2009 the Melange authors.
       
     4 #
       
     5 # Licensed under the Apache License, Version 2.0 (the "License");
       
     6 # you may not use this file except in compliance with the License.
       
     7 # You may obtain a copy of the License at
       
     8 #
       
     9 #   http://www.apache.org/licenses/LICENSE-2.0
       
    10 #
       
    11 # Unless required by applicable law or agreed to in writing, software
       
    12 # distributed under the License is distributed on an "AS IS" BASIS,
       
    13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    14 # See the License for the specific language governing permissions and
       
    15 # limitations under the License.
       
    16 
       
    17 """This module contains the Survey models.
       
    18 
       
    19 Survey describes meta-information and permissions.
       
    20 SurveyContent contains the fields (questions) and their metadata.
       
    21 """
       
    22 
       
    23 __authors__ = [
       
    24   '"Daniel Diniz" <ajaksu@gmail.com>',
       
    25   '"James Levy" <jamesalexanderlevy@gmail.com>',
       
    26   '"Lennard de Rijk" <ljvderijk@gmail.com>',
       
    27 ]
       
    28 
       
    29 
       
    30 from google.appengine.ext import db
       
    31 
       
    32 from django.utils.translation import ugettext
       
    33 
       
    34 import soc.models.work
       
    35 
       
    36 
       
    37 class SurveyContent(db.Expando):
       
    38   """Fields (questions) and schema representation of a Survey.
       
    39 
       
    40   Each survey content entity consists of properties where names and default
       
    41   values are set by the survey creator as survey fields.
       
    42 
       
    43     schema: A dictionary (as text) storing, for each field:
       
    44       - type
       
    45       - index
       
    46       - order (for choice questions)
       
    47       - render (for choice questions)
       
    48       - question (free form text question, used as label)
       
    49   """
       
    50 
       
    51   #:Field storing the content of the survey in the form of a dictionary.
       
    52   schema = db.TextProperty()
       
    53 
       
    54   #: Fields storing the created on and last modified on dates.
       
    55   created = db.DateTimeProperty(auto_now_add=True)
       
    56   modified = db.DateTimeProperty(auto_now=True)
       
    57 
       
    58   def getSurveyOrder(self):
       
    59     """Make survey questions always appear in the same (creation) order.
       
    60     """
       
    61     survey_order = {}
       
    62     schema = eval(self.schema)
       
    63     for property in self.dynamic_properties():
       
    64       # map out the order of the survey fields
       
    65       index = schema[property]["index"]
       
    66       if index not in survey_order:
       
    67         survey_order[index] = property
       
    68       else:
       
    69         # Handle duplicated indexes
       
    70         survey_order[max(survey_order) + 1] = property
       
    71     return survey_order
       
    72 
       
    73   def orderedProperties(self):
       
    74     """Helper for View.get_fields(), keep field order.
       
    75     """
       
    76     properties = []
       
    77     survey_order = self.getSurveyOrder().items()
       
    78     for position,key in survey_order:
       
    79       properties.insert(position, key)
       
    80     return properties
       
    81 
       
    82 
       
    83 class Survey(soc.models.work.Work):
       
    84   """Model of a Survey.
       
    85 
       
    86   This model describes meta-information and permissions.
       
    87   The actual questions of the survey are contained
       
    88   in the SurveyContent entity.
       
    89   """
       
    90 
       
    91   URL_NAME = 'survey'
       
    92   # euphemisms like "student" and "mentor" should be used if possible
       
    93   SURVEY_ACCESS = ['admin', 'restricted', 'member', 'user']
       
    94 
       
    95   # these are GSoC specific, so eventually we can subclass this
       
    96   SURVEY_TAKING_ACCESS = ['student', 
       
    97                           'mentor',
       
    98                           'student evaluation',
       
    99                           'mentor evaluation',
       
   100                           'org_admin',
       
   101                           'user',
       
   102                           'public']
       
   103   GRADE_OPTIONS = {'midterm':['mid_term_passed', 'mid_term_failed'],
       
   104                    'final':['final_passed', 'final_failed'],
       
   105                    'N/A':[] }
       
   106   prefix = db.StringProperty(default='program', required=True,
       
   107       choices=['site', 'club', 'sponsor', 'program', 'org', 'user'],
       
   108       verbose_name=ugettext('Prefix'))
       
   109   prefix.help_text = ugettext(
       
   110       'Indicates the prefix of the survey,'
       
   111       ' determines which access scheme is used.')
       
   112 
       
   113   #: Field storing the required access to read this survey.
       
   114   read_access = db.StringProperty(default='restricted', required=True,
       
   115       choices=SURVEY_ACCESS,
       
   116       verbose_name=ugettext('Survey Read Access'))
       
   117   read_access.help_text = ugettext(
       
   118       'Indicates who can read the results of this survey.')
       
   119 
       
   120   #: Field storing the required access to write to this survey.
       
   121   write_access = db.StringProperty(default='admin', required=True,
       
   122       choices=SURVEY_ACCESS,
       
   123       verbose_name=ugettext('Survey Write Access'))
       
   124   write_access.help_text = ugettext(
       
   125       'Indicates who can edit this survey.')
       
   126 
       
   127   #: Field storing the required access to write to this survey.
       
   128   taking_access = db.StringProperty(default='student', required=True,
       
   129       choices=SURVEY_TAKING_ACCESS,
       
   130       verbose_name=ugettext('Survey Taking Access'))
       
   131   taking_access.help_text = ugettext(
       
   132       'Indicates who can take this survey. '
       
   133       'Student/Mentor options are for Midterms and Finals.')
       
   134 
       
   135   #: Field storing whether a link to the survey should be featured in
       
   136   #: the sidebar menu (and possibly elsewhere); FAQs, Terms of Service,
       
   137   #: and the like are examples of "featured" survey.
       
   138   is_featured = db.BooleanProperty(
       
   139       verbose_name=ugettext('Is Featured'))
       
   140   is_featured.help_text = ugettext(
       
   141       'Field used to indicate if a Survey should be featured, for example,'
       
   142       ' in the sidebar menu.')
       
   143 
       
   144   #: Date at which the survey becomes available for taking.
       
   145   opening = db.DateTimeProperty(required=False)
       
   146   opening.help_text = ugettext(
       
   147       'Indicates a date before which this survey'
       
   148       ' cannot be taken or displayed.')
       
   149 
       
   150   #: Deadline for taking survey.
       
   151   deadline = db.DateTimeProperty(required=False)
       
   152   deadline.help_text = ugettext(
       
   153       'Indicates a date after which this survey'
       
   154       ' cannot be taken.')
       
   155 
       
   156   #: Referenceproperty that specifies the content of this survey.
       
   157   survey_content = db.ReferenceProperty(SurveyContent,
       
   158                                      collection_name="survey_parent")