Skip to content

Commit

Permalink
finished the basic implementation of forward collection in the Refere…
Browse files Browse the repository at this point in the history
…nceListProperty
  • Loading branch information
bendavieshe3 committed Mar 9, 2011
1 parent 7c5df86 commit ef1403e
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions src/he3/db/properties/reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from google.appengine.api import datastore_errors
from google.appengine.api import datastore

import logging

BadValueError = datastore_errors.BadValueError #pylint:disable=C0103

class ReferenceListProperty(db.ListProperty):
Expand All @@ -20,7 +22,6 @@ class ReferenceListProperty(db.ListProperty):
CURRENT STATUS
At time of writing
- Reverse Collection Property is not being created
- Referenced entities are always retrieved from the datastore. This should
be replaced by model-instance caching and possibly even lazy loading so
individual list members can be instanced as required.
Expand All @@ -37,7 +38,7 @@ def __init__(self, reference_class=None, verbose_name=None,
reference_class is a db.model class to create a list of references
of. As per ReferenceProperty
verbose_name is a human readable name
collection_name is the name of the query exposed on the 1st part
collection_name is the name of the query exposed on the 1st party
model object
reverse_collection_name is the name of query attributes on the
reference class model object.
Expand Down Expand Up @@ -99,7 +100,7 @@ def __property_config__(self, model_class, property_name):
self.collection_name))
#create and attach forward collection
setattr(self.model_class, self.collection_name,
db._ReverseReferenceProperty(self.reference_class, property_name))
_ForwardReferenceProperty(self.reference_class, property_name))



Expand Down Expand Up @@ -185,3 +186,23 @@ def make_value_from_datastore(self, value):
'''
return db.get(value);

class _ForwardReferenceProperty(db._ReverseReferenceProperty):
'''
A variation on the _reverseReferenceProperty to change the returned
query to be effective for 1st party collections
'''

def __get__(self, model_instance, model_class):
'''
Return a query which will fetch the members specified in the
ReferenceListProperty
'''
if model_instance is not None:
logging.info(self._ReverseReferenceProperty__model)
query = db.Query(self._ReverseReferenceProperty__model)
logging.info('here')
keys = [reference_item.key() for reference_item in
getattr(model_instance, self._ReverseReferenceProperty__property)]
return query.filter('__key__ IN ', keys)
else:
return self

0 comments on commit ef1403e

Please sign in to comment.