app/soc/models/answer.py
author Pawel Solyga <Pawel.Solyga@gmail.com>
Sat, 22 Nov 2008 22:44:02 +0000
changeset 568 6713617751b4
parent 512 aae25d2b4464
child 1307 091a21cf3627
permissions -rw-r--r--
Remove inheritance from PolyModel in Answer and Linkable model. Replace PolyModel inheritance with base.ModelWithFieldAttributes. A little explanation why we are trying to get rid of PolyModel. We decided to use PolyModel in order to get support for model inheritance in App Engine, however as we know this was a hacky workaround which added inheritance_line property to each model. Recent commits which added Linkable model caused our data viewer in admin console to show only one model "Linkable" since all of the classes inherited from it. Basically datastore viewer was useless plus we had a really big mess in datastore since everything was of one kind (Linkable). It's almost like creating one huge table in SQL database. Upcoming commits will eliminate all usage of PolyModel and finally remove PolyModel from our repository. We are still using inheritance however this doesn't modify how models are saved in data store so basically it's like copy and paste of properties from parent models. Patch by: Pawel Solyga, Sverre Rabbelier
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
20
7a901a99b349 Initial definition of the Answer Model.
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
7a901a99b349 Initial definition of the Answer Model.
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     2
#
7a901a99b349 Initial definition of the Answer Model.
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     3
# Copyright 2008 the Melange authors.
7a901a99b349 Initial definition of the Answer Model.
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     4
#
7a901a99b349 Initial definition of the Answer Model.
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
7a901a99b349 Initial definition of the Answer Model.
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
7a901a99b349 Initial definition of the Answer Model.
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
7a901a99b349 Initial definition of the Answer Model.
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     8
# 
7a901a99b349 Initial definition of the Answer Model.
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
7a901a99b349 Initial definition of the Answer Model.
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    10
# 
7a901a99b349 Initial definition of the Answer Model.
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
7a901a99b349 Initial definition of the Answer Model.
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
7a901a99b349 Initial definition of the Answer Model.
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
7a901a99b349 Initial definition of the Answer Model.
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
7a901a99b349 Initial definition of the Answer Model.
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    15
# limitations under the License.
7a901a99b349 Initial definition of the Answer Model.
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    16
339
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents: 316
diff changeset
    17
"""This module contains the Answer Model"""
20
7a901a99b349 Initial definition of the Answer Model.
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    18
7a901a99b349 Initial definition of the Answer Model.
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    19
__authors__ = [
7a901a99b349 Initial definition of the Answer Model.
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    20
  '"Todd Larsen" <tlarsen@google.com>',
7a901a99b349 Initial definition of the Answer Model.
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    21
  '"Sverre Rabbelier" <sverre@rabbelier.nl>',
7a901a99b349 Initial definition of the Answer Model.
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    22
]
7a901a99b349 Initial definition of the Answer Model.
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    23
316
9efdc7bc3565 Add missing blank lines between imports and sort all of the imports.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 244
diff changeset
    24
339
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents: 316
diff changeset
    25
from google.appengine.ext import db
316
9efdc7bc3565 Add missing blank lines between imports and sort all of the imports.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 244
diff changeset
    26
20
7a901a99b349 Initial definition of the Answer Model.
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    27
import soc.models.question
339
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents: 316
diff changeset
    28
import soc.models.quiz
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents: 316
diff changeset
    29
import soc.models.response
20
7a901a99b349 Initial definition of the Answer Model.
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    30
568
6713617751b4 Remove inheritance from PolyModel in Answer and Linkable model. Replace PolyModel inheritance with base.ModelWithFieldAttributes.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 512
diff changeset
    31
from soc.models import base
20
7a901a99b349 Initial definition of the Answer Model.
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    32
568
6713617751b4 Remove inheritance from PolyModel in Answer and Linkable model. Replace PolyModel inheritance with base.ModelWithFieldAttributes.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 512
diff changeset
    33
6713617751b4 Remove inheritance from PolyModel in Answer and Linkable model. Replace PolyModel inheritance with base.ModelWithFieldAttributes.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 512
diff changeset
    34
class Answer(base.ModelWithFieldAttributes):
339
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents: 316
diff changeset
    35
  """Model of a specific Answer to a Question in a specific Response.
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents: 316
diff changeset
    36
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents: 316
diff changeset
    37
  The properties in this Model do not have verbose_name or help_text,
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents: 316
diff changeset
    38
  because the dynamic nature of the forms required to create, edit, and
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents: 316
diff changeset
    39
  use entities of this Model make them pretty useless.
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents: 316
diff changeset
    40
  """
20
7a901a99b349 Initial definition of the Answer Model.
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    41
7a901a99b349 Initial definition of the Answer Model.
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    42
  #: A required many:1 relationship, where each of many Answers is
7a901a99b349 Initial definition of the Answer Model.
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    43
  #: a specific answer to a single Question.  An Answer must always
7a901a99b349 Initial definition of the Answer Model.
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    44
  #: be associated with a Question in order to be interpreted.
7a901a99b349 Initial definition of the Answer Model.
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    45
  #: It is currently unclear how useful this back-reference will be,
339
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents: 316
diff changeset
    46
  #: since the same Question could be used in multiple different
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents: 316
diff changeset
    47
  #: Quizzes. Given this, 'answers' currently only exists for
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents: 316
diff changeset
    48
  #: completeness.
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents: 316
diff changeset
    49
  question = db.ReferenceProperty(
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents: 316
diff changeset
    50
    reference_class=soc.models.question.Question, required=True,
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents: 316
diff changeset
    51
    collection_name="answers")
20
7a901a99b349 Initial definition of the Answer Model.
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    52
339
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents: 316
diff changeset
    53
  #: A many:1 relationship, where each of many Answers to different
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents: 316
diff changeset
    54
  #: Questions represents the answer set of a specific Response to a Quiz.
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents: 316
diff changeset
    55
  #: The back-reference in the Response model is a Query named 'answers'
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents: 316
diff changeset
    56
  #: which represents all of the specific Answers to Questions in that
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents: 316
diff changeset
    57
  #: Response.
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents: 316
diff changeset
    58
  #:
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents: 316
diff changeset
    59
  #: One and only one of the response or quiz ReferenceProperties *must*
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents: 316
diff changeset
    60
  #: be defined for an Answer entity.
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents: 316
diff changeset
    61
  response = db.ReferenceProperty(
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents: 316
diff changeset
    62
      reference_class=soc.models.response.Response, required=False,
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents: 316
diff changeset
    63
      collection_name="answers")
20
7a901a99b349 Initial definition of the Answer Model.
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    64
339
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents: 316
diff changeset
    65
  #: A many:1 relationship, where each of many Answers to different
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents: 316
diff changeset
    66
  #: Questions represents the solution set for the Questions in a Quiz.
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents: 316
diff changeset
    67
  #: The back-reference in the Quiz model is a Query named 'solutions'
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents: 316
diff changeset
    68
  #: which represents all of the solutions to Questions in that Quiz.
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents: 316
diff changeset
    69
  #:
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents: 316
diff changeset
    70
  #: One and only one of the response or quiz ReferenceProperties *must*
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents: 316
diff changeset
    71
  #: be defined for an Answer entity.
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents: 316
diff changeset
    72
  quiz = db.ReferenceProperty(
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents: 316
diff changeset
    73
      reference_class=soc.models.quiz.Quiz, required=False,
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents: 316
diff changeset
    74
      collection_name="solutions")
20
7a901a99b349 Initial definition of the Answer Model.
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    75
339
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents: 316
diff changeset
    76
  #: db.ListProperty of strings representing the answer value or values.
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents: 316
diff changeset
    77
  #:
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents: 316
diff changeset
    78
  #: For Questions that are not multiple-choice (see the choice_ids and
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents: 316
diff changeset
    79
  #: choices properties of soc.models.question.Question), this list will
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents: 316
diff changeset
    80
  #: contain a single string that is a free-form text answer.
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents: 316
diff changeset
    81
  #:
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents: 316
diff changeset
    82
  #: For Questions that *are* multiple-choice, this list will contain one
512
aae25d2b4464 Rename link_name to link_id everywhere, regardless of case (so LINK_NAME
Todd Larsen <tlarsen@google.com>
parents: 339
diff changeset
    83
  #: or more short, plain-text, "link_id-like" strings representing the
339
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents: 316
diff changeset
    84
  #: "encoded" answer choices (see the choice_ids property in
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents: 316
diff changeset
    85
  #: soc.models.question.Question).  For such multiple-choice Questions,    
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents: 316
diff changeset
    86
  #: how many strings are stored depends on the max_answers property of
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents: 316
diff changeset
    87
  #: the soc.models.question.Question entity for which this is an Answer.
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents: 316
diff changeset
    88
  #:
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents: 316
diff changeset
    89
  #: If question.is_optional is True, 'answers' may even be None or an
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents: 316
diff changeset
    90
  #: empty list if no answers were provided.
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents: 316
diff changeset
    91
  #:
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents: 316
diff changeset
    92
  #: Answers can be indexed, filtered, and sorted by this list, but only in
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents: 316
diff changeset
    93
  #: the way that query operators work with a db.ListProperty.
b9be44e09530 Define the Models for implementing Quizzes (collections of Questions) and their
Todd Larsen <tlarsen@google.com>
parents: 316
diff changeset
    94
  answers = db.ListProperty(item_type=str)