app/reflistprop/__init__.py
author Todd Larsen <tlarsen@google.com>
Tue, 14 Oct 2008 21:02:28 +0000
changeset 326 4a4474944dee
child 328 275a47dd0ac8
permissions -rw-r--r--
Add a third-party ReferenceListProperty class. This is going to be used to contain the references to Questions that make up a Quiz. Patch by: Todd Larsen Review by: to-be-reviewed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
326
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     2
#
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     3
# Copyright 2007 Ken Tidwell 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     4
#
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License. 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     8
#
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     9
#     http://www.apache.org/licenses/LICENSE-2.0 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    10
#
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    15
# limitations under the License.
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    16
# 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    17
"""Simple property for storing ordered lists of Model objects.
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    18
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    19
It should be noted that larger lists are going to be VERY inefficient
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    20
to load (one get per object).
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    21
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    22
Currently I have no idea where that upper bound might lie, though.
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    23
 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    24
A quick usage example:
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    25
        class Bit(db.Model):
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    26
                name = db.StringProperty(required=True)\ 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    27
        class Holder(db.Model):
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    28
                bits = reflistprop.ReferenceListProperty(Bit, default=None) 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    29
        b1 = Bit(name="first") 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    30
        b1.put() # need to put it so that it is a valid reference object 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    31
        h1 = holder() 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    32
        h1.bits.append(b1) 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    33
        h1.put()
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    34
         
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    35
These also work: 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    36
        h1.bits = [b1]
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    37
         
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    38
This throws a db.BadValueError because a string is not an instance of 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    39
Bit: 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    40
        h1.bits = ["nasty evil string"]
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    41
         
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    42
This is not good but gets no complaint at assignment time (same 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    43
behaviour as ListProperty)
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    44
but we will throw a db.BadValueError if you try to put it into the 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    45
datastore. (Maybe ListProperty
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    46
wants to do the same? Or should I be waiting for the datastore 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    47
internal entity construction to
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    48
notice the problem and throw?):
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    49
        h1.bits.append("nasty evil string")
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    50
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    51
Yes, of course you can query them. The important bit to understand is 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    52
that the list is stored as a list of keys in the datastore. So you use 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    53
the key of the entity in question in your query. (Seems like it would be 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    54
nice if the property could get involved and do that coercion for you but 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    55
I don't think it can right now...).
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    56
 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    57
Here's a little example:
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    58
        class Thang(db.Model): 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    59
            name = db.StringProperty(required=True) 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    60
        class Holder(db.Model): 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    61
            thangs = langutils.ReferenceListProperty(Thang, default=None) 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    62
        holder1 = Holder() 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    63
        holder1.put() 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    64
        foo = Thang(name="foo") 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    65
        foo.put() 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    66
        holder1.thangs.append(foo) 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    67
        holder1.put() 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    68
        hq = db.GqlQuery("SELECT * FROM Holder where thangs = :1", foo.key()) 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    69
        holders = hq.fetch(10) 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    70
        print "Holders =", holders
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    71
        
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    72
Obtained from:
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    73
  http://groups.google.com/group/google-appengine/msg/d203cc1b93ee22d7 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    74
"""
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    75
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    76
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    77
from google.appengine.ext import db
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    78
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    79
 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    80
class ReferenceListProperty(db.Property):
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    81
  """A property that stores a list of models. 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    82
  This is a parameterized property; the parameter must be a valid 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    83
  Model type, and all items must conform to this type.
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    84
  """
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    85
  def __init__(self, item_type, verbose_name=None, default=None, **kwds): 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    86
    """Construct ReferenceListProperty.
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    87
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    88
    Args:
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    89
      item_type: Type for the list items; must be a subclass of Model. 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    90
      verbose_name: Optional verbose name.
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    91
      default: Optional default value; if omitted, an empty list is used. 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    92
      **kwds: Optional additional keyword arguments, passed to base class. 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    93
    """
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    94
    if not issubclass(item_type, db.Model): 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    95
      raise TypeError('Item type should be a subclass of Model') 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    96
    if default is None:
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    97
      default = []
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    98
    self.item_type = item_type 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    99
    super(ReferenceListProperty, self).__init__(verbose_name, 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   100
                                                default=default,
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   101
                                                **kwds)
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   102
  def validate(self, value):
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   103
    """Validate list.
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   104
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   105
    Note that validation here is just as broken as for ListProperty. 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   106
    The values in the list are only validated if the entire list is
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   107
    swapped out.
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   108
    If the list is directly modified, there is no attempt to validate 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   109
    the new items.
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   110
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   111
    Returns:
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   112
      A valid value. 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   113
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   114
    Raises:
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   115
      BadValueError if property is not a list whose items are 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   116
      instances of the item_type given to the constructor.
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   117
    """ 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   118
    value = super(ReferenceListProperty, self).validate(value) 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   119
    if value is not None: 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   120
      if not isinstance(value, list): 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   121
        raise db.BadValueError('Property %s must be a list' % 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   122
                               self.name)
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   123
      for item in value:
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   124
        if not isinstance(item, self.item_type): 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   125
            raise db.BadValueError(
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   126
                'Items in the %s list must all be %s instances' % 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   127
                (self.name, self.item_type.__name__))
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   128
    return value
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   129
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   130
  def empty(self, value): 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   131
    """Is list property empty.
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   132
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   133
    [] is not an empty value.
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   134
 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   135
    Returns:
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   136
      True if value is None, else False. 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   137
    """ 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   138
    return value is None
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   139
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   140
  data_type = list
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   141
 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   142
  def default_value(self): 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   143
    """Default value for list.
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   144
 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   145
    Because the property supplied to 'default' is a static value, 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   146
    that value must be shallow copied to prevent all fields with 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   147
    default values from sharing the same instance.
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   148
 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   149
    Returns: 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   150
      Copy of the default value. 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   151
    """ 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   152
    return list(super(ReferenceListProperty, self).default_value())
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   153
 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   154
  def get_value_for_datastore(self, model_instance): 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   155
    """A list of key values is stored.
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   156
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   157
    Prior to storage, we validate the items in the list. 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   158
    This check seems to be missing from ListProperty.
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   159
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   160
    Args: 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   161
      model_instance: Instance to fetch datastore value from.
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   162
 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   163
    Returns: 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   164
      A list of the keys for all Models in the value list. 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   165
    """ 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   166
    value = self.__get__(model_instance, model_instance.__class__) 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   167
    self.validate(value) 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   168
    if value is None: 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   169
        return None 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   170
    else: 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   171
        return [v.key() for v in value]
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   172
 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   173
  def make_value_from_datastore(self, value): 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   174
    """Recreates the list of Models from the list of keys.
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   175
 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   176
    Args:
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   177
      value: value retrieved from the datastore entity.
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   178
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   179
    Returns: 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   180
      None or a list of Models. 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   181
    """ 
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   182
    if value is None:
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   183
        return None
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   184
    else:
4a4474944dee Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   185
        return [db.get(v) for v in value]