app/soc/models/quiz.py
author Sverre Rabbelier <srabbelier@gmail.com>
Thu, 16 Oct 2008 23:23:16 +0000
changeset 363 d35ffa6ca643
parent 351 b37fc4c1e189
child 385 6d410bf49a82
permissions -rw-r--r--
Add a framework for generic views This commit does not enable the new code, as such there should be no change in functionality. In order to use the new code the maps.py file should be updated to point at the new views/models/sponsor.py module. Patch by: Sverre Rabbelier Reviewed by: to-be-reviewed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
339
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     2
#
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     3
# Copyright 2008 the Melange authors.
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     4
#
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     8
#
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    10
#
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    15
# limitations under the License.
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    16
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    17
"""This module contains the Quiz Model."""
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    18
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    19
__authors__ = [
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    20
  '"Todd Larsen" <tlarsen@google.com>',
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    21
]
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    22
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    23
import reflistprop
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    24
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    25
from google.appengine.ext import db
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    26
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    27
from django.utils.translation import ugettext_lazy
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    28
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    29
import soc.models.answer
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    30
import soc.models.question
351
b37fc4c1e189 Eliminate the Work.abstract property and move the Document.content property
Todd Larsen <tlarsen@google.com>
parents: 342
diff changeset
    31
import soc.models.work
339
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    32
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    33
351
b37fc4c1e189 Eliminate the Work.abstract property and move the Document.content property
Todd Larsen <tlarsen@google.com>
parents: 342
diff changeset
    34
class Quiz(soc.models.work.Work):
339
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    35
  """Model of a Quiz, a collection of Questions to be asked.
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    36
  
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    37
  (named Quiz because Questionnaire was too much to type...)
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    38
  
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    39
  A Quiz collects a set of Questions to which Answers are given in the
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    40
  form of a separate Model called a Response.
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    41
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    42
  Quizzes can even be used as templates for comments and scoring
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    43
  annotations to various Works, such as Documents and Proposals.  A
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    44
  separate Review Model is derived from Quiz for these purposes.
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    45
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    46
  The specific way that the properties and relations inherited from
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    47
  Document, and also indirectly from Work, are used with a Quiz are
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    48
  described below.
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    49
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    50
    work.title:  the title of the Quiz
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    51
342
72482d8e5b34 Remove the proposed WorksAuthors many:many relation and promote the "founder"
Todd Larsen <tlarsen@google.com>
parents: 339
diff changeset
    52
    work.author:  the author of the Work referred to by this relation
72482d8e5b34 Remove the proposed WorksAuthors many:many relation and promote the "founder"
Todd Larsen <tlarsen@google.com>
parents: 339
diff changeset
    53
      is the author of the Quiz (but not necessarily the individual
339
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    54
      Questions themselves, see the Question Model)
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    55
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    56
    work.reviews:  even Quizzes can be "reviewed" (possibly commented
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    57
      on during creation or annotated once put into use).
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    58
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    59
    work.partial_path/work.link_name: used to scope and uniquely identify
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    60
      a Quiz in the same way these properties are used with Documents, etc.
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    61
351
b37fc4c1e189 Eliminate the Work.abstract property and move the Document.content property
Todd Larsen <tlarsen@google.com>
parents: 342
diff changeset
    62
    work.content:  the "preface" of the Quiz, displayed before any
339
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    63
      of the Questions, usually containing instructions for the Quiz
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    64
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    65
  In addition to any explicit ReferenceProperties in the Quiz Model and
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    66
  those inherited as described above, a Quiz entity participates in these
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    67
  relationships:
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    68
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    69
    responses)  a 1:many relationship where each Quiz can produce all of
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    70
      its many Response entities that indicate they contain specific
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    71
      Answers to each of the Questions contained in that Quiz. This relation
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    72
      is implemented as the 'responses' back-reference Query of the Response
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    73
      Model 'quiz' reference.
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    74
      
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    75
    solutions)  a 1:many relationship where some (or none, or all) of the
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    76
      Questions in the Quiz have "solutions" or "correct Answers".  The
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    77
      'solutions' back-reference Query of the Answer Model 'quiz' reference
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    78
      is used to point these "correct Answers" at the Quiz to which they
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    79
      apply.  One example of a Quiz having a "correct Answer" is a GSoC
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    80
      mentor survey that has a "pass" Question that gates if the student
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    81
      gets paid.  The desired Answer for this Question would be
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    82
      associated with the Quiz via the 'quiz' property and some controller
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    83
      logic could check if a survey "passed" by querying for these
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    84
      "solution" Answers and seeing if the survey Response had the "right"
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    85
      Answers (to the one Question that matters in this case...).
342
72482d8e5b34 Remove the proposed WorksAuthors many:many relation and promote the "founder"
Todd Larsen <tlarsen@google.com>
parents: 339
diff changeset
    86
72482d8e5b34 Remove the proposed WorksAuthors many:many relation and promote the "founder"
Todd Larsen <tlarsen@google.com>
parents: 339
diff changeset
    87
    proposals)  a 1:many relationship where each Quiz can produce all of
72482d8e5b34 Remove the proposed WorksAuthors many:many relation and promote the "founder"
Todd Larsen <tlarsen@google.com>
parents: 339
diff changeset
    88
      the Proposals that make use of the Quiz as part of the Proposal.
72482d8e5b34 Remove the proposed WorksAuthors many:many relation and promote the "founder"
Todd Larsen <tlarsen@google.com>
parents: 339
diff changeset
    89
      This relation is implemented as the 'proposals' back-reference Query
72482d8e5b34 Remove the proposed WorksAuthors many:many relation and promote the "founder"
Todd Larsen <tlarsen@google.com>
parents: 339
diff changeset
    90
      of the Proposal Model 'quiz' reference.
339
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    91
  """
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    92
  
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    93
  #: a many:many relationship (many:many because a given Question can be
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    94
  #: reused in more than one Quiz, and each Quiz is made up of one or more
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    95
  #: Questions) between Question entities and, when combined, the Quiz they
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    96
  #: form.
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    97
  #:
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    98
  #: A ReferenceListProperty is used instead of a special many:many
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    99
  #; relation Model for a number of reasons:
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   100
  #:   1) the Questions in a Quiz need to be ordered
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   101
  #:   2) Quizzes will have relatively few Questions, so the performance
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   102
  #:      ReferenceListProperty is not a major concern
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   103
  #:   3) querying a Question for all of the Quizzes that contain it is
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   104
  #:      a rare occurrence, so the expense of a ListProperty query is
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   105
  #:      not a real concern
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   106
  questions = reflistprop.ReferenceListProperty(
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   107
    soc.models.question.Question, default=None)