Skip to content

Commit

Permalink
Added doctests
Browse files Browse the repository at this point in the history
  • Loading branch information
Arachnid committed Sep 14, 2009
1 parent df5dfb7 commit 4ffb266
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions __init__.py
Expand Up @@ -135,6 +135,19 @@ def __init__(self, property, *args, **kwargs):

class KeyProperty(db.Property):
"""A property that stores a key, without automatically dereferencing it.
Example usage:
>>> class SampleModel(db.Model):
... sample_key = KeyProperty()
>>> model = SampleModel()
>>> model.sample_key = db.Key.from_path("Foo", "bar")
>>> model.put() # doctest: +ELLIPSIS
datastore_types.Key.from_path(u'SampleModel', ...)
>>> model.sample_key # doctest: +ELLIPSIS
datastore_types.Key.from_path(u'Foo', u'bar', ...)
"""
def validate(self, value):
"""Validate the value.
Expand All @@ -146,6 +159,8 @@ def validate(self, value):
"""
if isinstance(value, basestring):
value = db.Key(value)
if value is not None and not isinstance(value, db.Key):
raise TypeError("Property %s must be an instance of db.Key" % self.name)
if value is not None:
if not isinstance(value, db.Key):
raise TypeError("Property %s must be an instance of db.Key"
% (self.name,))
return super(KeyProperty, self).validate(value)

0 comments on commit 4ffb266

Please sign in to comment.