author | Sverre Rabbelier <srabbelier@gmail.com> |
Sun, 01 Mar 2009 15:33:52 +0000 | |
changeset 1578 | 0ffb9a3dfbdf |
parent 386 | 33942ff6e71b |
permissions | -rw-r--r-- |
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 |
# |
328
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
3 |
# Copyright 2007 Ken Tidwell |
326
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"); |
328
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
6 |
# you may not use this file except in compliance with the License. |
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
7 |
# You may obtain a copy of the License at |
326
4a4474944dee
Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
8 |
# |
328
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
9 |
# http://www.apache.org/licenses/LICENSE-2.0 |
326
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, |
328
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
13 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
14 |
# See the License for the specific language governing permissions and |
326
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): |
328
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
26 |
name = db.StringProperty(required=True) |
326
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): |
328
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
28 |
bits = reflistprop.ReferenceListProperty(Bit, default=None) |
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
29 |
b1 = Bit(name="first") |
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
30 |
b1.put() # need to put it so that it is a valid reference object |
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
31 |
h1 = holder() |
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
32 |
h1.bits.append(b1) |
326
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 |
|
328
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
35 |
These also work: |
326
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 |
|
328
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
38 |
This throws a db.BadValueError because a string is not an instance of |
326
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 |
|
328
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
42 |
This is not good but gets no complaint at assignment time (same behaviour |
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
43 |
as ListProperty) but we will throw a db.BadValueError if you try to put it |
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
44 |
into the datastore. (Maybe ListProperty wants to do the same? Or should I be |
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
45 |
waiting for the datastore internal entity construction to notice the problem |
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
46 |
and throw?): |
326
4a4474944dee
Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
47 |
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
|
48 |
|
328
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
49 |
Yes, of course you can query them. The important bit to understand is |
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
50 |
that the list is stored as a list of keys in the datastore. So you use |
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
51 |
the key of the entity in question in your query. (Seems like it would be |
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
52 |
nice if the property could get involved and do that coercion for you but |
326
4a4474944dee
Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
53 |
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
|
54 |
|
4a4474944dee
Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
55 |
Here's a little example: |
328
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
56 |
class Thang(db.Model): |
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
57 |
name = db.StringProperty(required=True) |
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
58 |
class Holder(db.Model): |
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
59 |
thangs = langutils.ReferenceListProperty(Thang, default=None) |
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
60 |
holder1 = Holder() |
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
61 |
holder1.put() |
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
62 |
foo = Thang(name="foo") |
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
63 |
foo.put() |
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
64 |
holder1.thangs.append(foo) |
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
65 |
holder1.put() |
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
66 |
hq = db.GqlQuery("SELECT * FROM Holder where thangs = :1", foo.key()) |
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
67 |
holders = hq.fetch(10) |
326
4a4474944dee
Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
68 |
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
|
69 |
|
4a4474944dee
Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
70 |
Obtained from: |
328
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
71 |
http://groups.google.com/group/google-appengine/msg/d203cc1b93ee22d7 |
326
4a4474944dee
Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
72 |
""" |
4a4474944dee
Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
73 |
|
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 |
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
|
76 |
|
4a4474944dee
Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
77 |
|
4a4474944dee
Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
78 |
class ReferenceListProperty(db.Property): |
328
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
79 |
"""A property that stores a list of models. |
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
80 |
|
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
81 |
This is a parameterized property; the parameter must be a valid |
326
4a4474944dee
Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
82 |
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
|
83 |
""" |
328
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
84 |
def __init__(self, item_type, verbose_name=None, default=None, **kwds): |
326
4a4474944dee
Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
85 |
"""Construct ReferenceListProperty. |
4a4474944dee
Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
86 |
|
4a4474944dee
Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
87 |
Args: |
328
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
88 |
item_type: Type for the list items; must be a subclass of Model. |
326
4a4474944dee
Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
89 |
verbose_name: Optional verbose name. |
328
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
90 |
default: Optional default value; if omitted, an empty list is used. |
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
91 |
**kwds: Optional additional keyword arguments, passed to base class. |
326
4a4474944dee
Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
92 |
""" |
328
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
93 |
if not issubclass(item_type, db.Model): |
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
94 |
raise TypeError('Item type should be a subclass of Model') |
326
4a4474944dee
Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
95 |
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
|
96 |
default = [] |
328
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
97 |
self.item_type = item_type |
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
98 |
super(ReferenceListProperty, self).__init__(verbose_name, |
326
4a4474944dee
Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
99 |
default=default, |
4a4474944dee
Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
100 |
**kwds) |
328
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
101 |
|
326
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 |
|
328
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
105 |
Note that validation here is just as broken as for ListProperty. |
326
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 |
328
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
107 |
swapped out. If the list is directly modified, there is no attempt |
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
108 |
to validate the new items. |
326
4a4474944dee
Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
109 |
|
4a4474944dee
Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
110 |
Returns: |
328
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
111 |
A valid value. |
326
4a4474944dee
Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
112 |
|
4a4474944dee
Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
113 |
Raises: |
328
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
114 |
BadValueError if property is not a list whose items are |
326
4a4474944dee
Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
115 |
instances of the item_type given to the constructor. |
328
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
116 |
""" |
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
117 |
value = super(ReferenceListProperty, self).validate(value) |
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
118 |
if value is not None: |
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
119 |
if not isinstance(value, list): |
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
120 |
raise db.BadValueError('Property %s must be a list' % |
326
4a4474944dee
Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
121 |
self.name) |
4a4474944dee
Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
122 |
for item in value: |
328
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
123 |
if not isinstance(item, self.item_type): |
386
33942ff6e71b
Fix identions in reflistprop module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
328
diff
changeset
|
124 |
raise db.BadValueError( |
33942ff6e71b
Fix identions in reflistprop module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
328
diff
changeset
|
125 |
'Items in the %s list must all be %s instances' % |
33942ff6e71b
Fix identions in reflistprop module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
328
diff
changeset
|
126 |
(self.name, self.item_type.__name__)) |
326
4a4474944dee
Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
127 |
return value |
4a4474944dee
Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
128 |
|
328
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
129 |
def empty(self, value): |
326
4a4474944dee
Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
130 |
"""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
|
131 |
|
4a4474944dee
Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
132 |
[] 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
|
133 |
|
4a4474944dee
Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
134 |
Returns: |
328
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
135 |
True if value is None, else False. |
326
4a4474944dee
Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
136 |
""" |
4a4474944dee
Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
137 |
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
|
138 |
|
4a4474944dee
Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
139 |
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
|
140 |
|
328
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
141 |
def default_value(self): |
326
4a4474944dee
Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
142 |
"""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
|
143 |
|
328
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
144 |
Because the property supplied to 'default' is a static value, |
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
145 |
that value must be shallow copied to prevent all fields with |
326
4a4474944dee
Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
146 |
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
|
147 |
|
328
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
148 |
Returns: |
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
149 |
Copy of the default value. |
326
4a4474944dee
Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
150 |
""" |
4a4474944dee
Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
151 |
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
|
152 |
|
328
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
153 |
def get_value_for_datastore(self, model_instance): |
326
4a4474944dee
Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
154 |
"""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
|
155 |
|
328
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
156 |
Prior to storage, we validate the items in the list. |
326
4a4474944dee
Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
157 |
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
|
158 |
|
328
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
159 |
Args: |
326
4a4474944dee
Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
160 |
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
|
161 |
|
328
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
162 |
Returns: |
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
163 |
A list of the keys for all Models in the value list. |
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
164 |
""" |
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
165 |
value = self.__get__(model_instance, model_instance.__class__) |
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
166 |
self.validate(value) |
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
167 |
if value is None: |
386
33942ff6e71b
Fix identions in reflistprop module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
328
diff
changeset
|
168 |
return None |
328
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
169 |
else: |
386
33942ff6e71b
Fix identions in reflistprop module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
328
diff
changeset
|
170 |
return [v.key() for v in value] |
326
4a4474944dee
Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
171 |
|
328
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
172 |
def make_value_from_datastore(self, value): |
326
4a4474944dee
Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
173 |
"""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
|
174 |
|
4a4474944dee
Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
175 |
Args: |
4a4474944dee
Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
176 |
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
|
177 |
|
328
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
178 |
Returns: |
275a47dd0ac8
Add missing blank lines, clean up the doc string and remove white spaces at the end of the lines in reflistprop.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
326
diff
changeset
|
179 |
None or a list of Models. |
326
4a4474944dee
Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
180 |
""" |
4a4474944dee
Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
181 |
if value is None: |
386
33942ff6e71b
Fix identions in reflistprop module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
328
diff
changeset
|
182 |
return None |
326
4a4474944dee
Add a third-party ReferenceListProperty class. This is going to be used to
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
183 |
else: |
386
33942ff6e71b
Fix identions in reflistprop module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
328
diff
changeset
|
184 |
return [db.get(v) for v in value] |