Permalink
Browse files

Closes #46, `NodeModel` pickling.

... with the pleasant side-effect of removing the whole ugly `LazyModel`
mechanism.
  • Loading branch information...
mhluongo committed Oct 18, 2012
1 parent 5491792 commit 9d4b0d69539ca45285158038de53b84ff8ffab86
Showing with 15 additions and 45 deletions.
  1. +8 −42 neo4django/db/models/relationships.py
  2. +4 −1 neo4django/tests/model_tests.py
  3. +1 −1 neo4django/tests/models.py
  4. +2 −1 reg_settings.py
@@ -19,45 +19,6 @@
from collections import defaultdict
-class LazyModel(object):
- """
- A proxy class that enables relationships to have str targets, eg
-
- actors = Relationship('Actor', ...)
- """
- def __init__(self, cls, field, name, setup_reversed):
- self.__cls = cls
- self.__field = field
- self.__name = name
- self.__setup_reversed = setup_reversed
- add_lazy_relation(cls, field, name, self.__setup)
-
- def __setup(self, field, target, source):
- if not issubclass(target, NodeModel):
- raise TypeError("Relationships may only extend from Nodes.")
- self.__target = target
- self.__setup_reversed(target)
- __target = None
-
- @property
- def _model(self):
- model = self.__target
- if model is None:
- raise ValueError("Lazy model not initialized!")
- else:
- return model
-
- def __getinitargs__(self):
- return (self.__cls, self.__field, self.__name, self.__setup_reversed)
-
- def __getattr__(self, attr):
- if attr in ('__deepcopy__',) and self.__target is None:
- raise AttributeError
- return getattr(self._model, attr)
-
- def __call__(self, *args, **kwargs):
- return self._model(*args, **kwargs)
-
class RelationshipBase(type):
"""
Metaclass for Relationships. Creates a RelationshipModel for each Relationship
@@ -227,8 +188,13 @@ def contribute_to_class(self, source, name):
source._meta._relationships[name] = bound
setattr(source, name, bound)
if isinstance(self.__target, basestring):
- self.__target = LazyModel(source, self, self.__target,
- lambda target: bound._setup_reversed(target))
+ def setup(field, target, source):
+ if not issubclass(target, NodeModel):
+ raise TypeError("Relationships may only extend from Nodes.")
+ # replace the string target with the real target
+ self.__target = target
+ bound._setup_reversed(target)
+ add_lazy_relation(source, self, self.__target, setup)
target = self.__target
if not self.__is_reversed:
bound._setup_reversed(target)
@@ -268,7 +234,7 @@ def __init__(self, rel, source, relname, attname, serialize=True):
def _setup_reversed(self, target):
self.__target = target
- if not isinstance(target, LazyModel):
+ if not isinstance(target, basestring):
self.__rel.reverse(self.__source,
self.__attname).contribute_to_class(
target, self.reversed_name(self.__source))
@@ -155,7 +155,10 @@ class NameOwner(models.NodeModel):
assert pete in list(pete3.confidantes.all()),\
"Copying isn't commuting relationships!"
-def test_model_pickle():
+def test_model_pickling():
+ """
+ Covers issue #46, pickling `NodeModel`s.
+ """
def pickle_and_restore(m):
import pickle
@@ -14,5 +14,5 @@ class RelatedDog(models.NodeModel):
class LazyCat(models.NodeModel):
name = models.StringProperty()
- chases = models.Relationship('IndexedMouse', rel_type = 'chases')
+ chases = models.Relationship('IndexedMouse', rel_type='chases_lazily')
View
@@ -111,7 +111,8 @@
'test_relationship_delete',
'test_rel_cache',
'test_conflicting_rel_types',
- 'test_syncdb']
+ 'test_syncdb',
+ 'test_model_pickling']
should_fail=[
'test_dates',
'test_filter_iexact',

0 comments on commit 9d4b0d6

Please sign in to comment.