Fetching contributors…
Cannot retrieve contributors at this time
864 lines (714 sloc) 29.7 KB
from django.db import models
from django.db.models.fields.related import add_lazy_relation
from django.db.models.query_utils import DeferredAttribute
from django.db.models.query import EmptyQuerySet
from django.db.models.signals import post_delete
from django.forms import ModelChoiceField, ModelMultipleChoiceField
from django.utils.text import capfirst
from django.dispatch import receiver
from neo4django import Incoming, Outgoing
from neo4django.db import DEFAULT_DB_ALIAS
from neo4django.decorators import not_implemented, transactional
from neo4django.utils import AssignableList, AttrRouter
from neo4django.constants import INTERNAL_ATTR, ORDER_ATTR
from .base import NodeModel
from .query import (NodeQuerySet, Query, cypher_rel_str)
from .cypher import (Clauses, Start, With, Match, Path, NodeComponent,
RelationshipComponent, OrderBy, OrderByTerm, ColumnExpression)
from neo4jrestclient.constants import RELATIONSHIPS_IN, RELATIONSHIPS_OUT
from collections import defaultdict
from functools import partial
class RelationshipModel(object):
Model backing all relationships. Intended for a single instance to
correspond to an edge in the graph.
__relationship = None
def __init__(self):
def relationship(self):
rel = self.__relationship
if rel is None:
# XXX: better exception
raise ValueError("Unsaved objects have no relationship.")
return rel
_neo4j_underlying = relationship
def new(RelationshipModel, module, name, bases):
return type(name, bases + (RelationshipModel,),
{'__module__': module})
def add_field(self, prop):
raise NotImplementedError("<RelationshipModel>.add_field()")
class Relationship(object):
def __init__(self, target, rel_type=None, direction=None, optional=True,
single=False, related_single=False, related_name=None,
editable=True, verbose_name=None, help_text=None,
preserve_ordering=False, null = True, metadata={},
if direction is Outgoing:
elif direction is Incoming:
elif direction is None:
if not isinstance(rel_type, basestring):
direction = rel_type.direction
if not isinstance(rel_type, basestring):
if rel_type.direction != direction:
raise ValueError("Incompatible direction!")
rel_type = rel_type.type
self._reverse_relationship_type = Relationship
self.__target = target = rel_type
self.__single = single
self.direction = direction
self.__related_single = related_single
self._related_name = related_name
self.__ordered = preserve_ordering
self.__meta = metadata
self.__related_meta = rel_metadata
self.editable = editable
self.optional = optional
self.verbose_name = verbose_name
self.help_text = help_text
self.null = null
target_model = property(lambda self: self.__target)
ordered = property(lambda self: self.__ordered)
meta = property(lambda self: self.__meta)
single = property(lambda self: self.__single)
__is_reversed = False
def reverse(self, target, name):
if self.direction is RELATIONSHIPS_IN:
elif self.direction is RELATIONSHIPS_OUT:
Type = self._reverse_relationship_type
relationship = Type(
target,, direction=direction,
single=self.__related_single, related_name=name,
metadata=self.__related_meta, preserve_ordering=self.__ordered)
relationship.__is_reversed = True
return relationship
def reversed_name(self, target=None):
if self._related_name:
return self._related_name
return self.get_name(target, self.__single)
def get_name(target, single=False):
suffix = '%s' if single else '%s_set'
if isinstance(target, basestring):
name = target.rsplit('.', 1)[-1]
name = target.__name__
return suffix % name.lower()
def get_internal_type(self):
return "Neo4jRelationship"
def has_default(self):
return False
def _get_bound_relationship_type(self):
# TODO this will change with relationship models (#1)
if self.__single:
return SingleNode
return MultipleNodes
def _get_new_bound_relationship(self, source, name):
return self._get_bound_relationship_type()(self, source, or name, name)
def contribute_to_class(self, source, name):
if not issubclass(source, NodeModel):
raise TypeError("Relationships may only extend from Nodes.")
self.creation_counter = source.creation_counter
# XXX this is to cover strange situations like accidental overriding
# of abstract models' reverse relationships like issue #190
if hasattr(source, name):
# make sure this relationship doesn't overlap with another of the same
# type and direction
if hasattr(source._meta, '_relationships'):
for r in source._meta._relationships.values():
if r.rel_type == and r.direction == self.direction:
import warnings
warnings.warn('`%s` and `%s` share a relationship type and '
'direction. Is this what you meant to do?'
% (, name))
bound = self._get_new_bound_relationship(source, name)
if not hasattr(source._meta, '_relationships'):
source._meta._relationships = {}
source._meta._relationships[name] = bound
setattr(source, name, bound)
if isinstance(self.__target, basestring):
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
add_lazy_relation(source, self, self.__target, setup)
target = self.__target
if not self.__is_reversed:
def formfield(self, **kwargs):
defaults = {
'required': not self.optional,
'label': capfirst(self.verbose_name),
'help_text': self.help_text,
'queryset': self.target_model.objects
if self.single:
defaults['form_class'] = ModelChoiceField
defaults['form_class'] = ModelMultipleChoiceField
form_class = defaults['form_class']
del defaults['form_class']
return form_class(**defaults)
def __getinitargs__(self):
return (self.__target,, self.direction, True, self.__single,
self.__related_single, self.reversed_name, self.__ordered,
self.__meta, self.__related_meta)
#subclasses DeferredAttribute to avoid being set to None in
class BoundRelationship(AttrRouter, DeferredAttribute):
indexed = False
rel = None
primary_key = False
choices = None
db_index = None
blank = True
unique = False
unique_for_date = False
unique_for_year = False
unique_for_month = False
def __init__(self, rel, source, relname, attname, serialize=True):
self.__rel = rel
self.__source = source
self._type = relname
self.__attname = attname
self.serialize = serialize
relationships = self._relationships_for(source)
relationships[self.__attname] = self # XXX weakref
# form handling
], self.__rel)
self.null = False
def clean(self, value, instance):
return value
def has_default(self):
return None
def get_internal_type(self):
return self.__rel.__class__.__name__
def _setup_reversed(self, target):
self.__target = target
if not isinstance(target, basestring):
target, self.reversed_name(self.__source))
attname = name = property(lambda self: self.__attname)
def rel_type(self):
return self._type
def relationship(self):
return self.__rel
def target_model(self):
return self.__target
def source_model(self):
return self.__source
def get_default(self):
return None
def contribute_to_class(self, source, name):
return self.__rel.contribute_to_class(source, name)
def _get_val_from_obj(self, obj):
return self.__get__(obj)
def value_to_string(self, obj):
return str(self.__get__(self))
def _state_for(instance, create=True):
state = instance.__state
state = {}
if create:
instance.__state = state
return state
def _relationships_for(obj_or_cls):
meta = obj_or_cls._meta
relationships = meta._relationships
meta._relationships = relationships = {}
return relationships
def _all_relationships_for(obj_or_cls):
new_rel_dict = {}
cls = obj_or_cls if isinstance(obj_or_cls, type) else type(obj_or_cls)
for parent in cls.__bases__:
if hasattr(parent, '_meta'):
return new_rel_dict
def _save_(instance, node):
state = BoundRelationship._state_for(instance, create=False)
if state:
rels = BoundRelationship._all_relationships_for(instance)
for key in state.keys():
rels[key]._save_relationship(instance, node, state[key])
if isinstance(state[key], tuple): # HACK, rearchitect
state[key] = (False, state[key][1])
#TODO this... well, consider revising
NodeModel._save_neo4j_relationships = staticmethod(_save_)
def _save_relationship(self, instance, node, state):
def _load_relationships(self, node):
#Returns all neo4j relationships attached to the provided neo4j node.
#TODO TODO we can probably trash this function with the new backend, refs 174
if self.direction is RELATIONSHIPS_OUT:
rel_func = node.relationships.outgoing
rel_func = node.relationships.incoming
rels = rel_func([self._type])
if not hasattr(self, '_relationships'):
attrs = {}
attrs = self._relationships.get_new_attrs()
self._relationships = AssignableList(rels)
for k in attrs.keys():
setattr(self._relationships, k, attrs[k])
return self._relationships
creation_counter = property(lambda self: self.__rel.creation_counter)
def _set_relationship(self, obj, state, value):
if value is None: # assume initialization - ignore
return # TODO: verify that obj is unsaved!
raise TypeError("<%s>.%s is not assignable" %
def _del_relationship(self, obj, state):
raise TypeError("Cannot delete <%s>.%s" %
def _create_neo_relationship(self, node, obj, **kwargs):
neo_rel_attrs = kwargs.get('attrs', {})
neo_rel_attrs[INTERNAL_ATTR] = True
if is None:
other = obj.node
# TODO: verify that it's ok in the reverse direction?
if self.direction != 'out':
node, other = other, node
node.relationships.create(self._type, other, **neo_rel_attrs)
def to_python(cls, value):
A python-centric alias for from_neo()
return cls.from_neo(value)
def from_python(cls, value):
A python-centric alias for to_neo()
return cls.to_neo(value)
def to_neo(cls, value):
return value
def from_neo(cls, value):
return value
def __getinitargs__(self):
return (self.__rel, self.__source, self.__type, self.__attname, self.serialize)
def __cmp__(self, other):
return cmp(self.creation_counter, other.creation_counter)
def __get__(self, obj, cls=None):
if obj is None:
return self
return self._get_relationship(obj, self._state_for(obj))
def __set__(self, obj, value):
self._set_relationship(obj, self._state_for(obj), value)
def __delete__(self, obj):
self._del_relationship(obj, self._state_for(obj))
class SingleNode(BoundRelationship):
#BoundRelationship subclass for a single node relationship without an
#associated relationship model.
def _get_relationship(self, obj, state):
if in state:
changed, result = state[]
return result
if hasattr(obj, 'node'):
this = obj.node
return None
result = self._load_related(this)
state[] = False, result
return result
def value_from_object(self, obj):
return self.__get__(obj)
def _load_related(self, node):
relationships = self._load_relationships(node)
#TODO seriously consider removing this restriction- I'm not sure I see
# any benefit, and it makes creating neo4django-compliant graphs that
# much more difficult.
django_relationships = filter(lambda rel: rel[INTERNAL_ATTR], relationships)
if len(django_relationships) < 1:
return None
elif len(django_relationships) > 1:
raise ValueError("There's an ambiguous relationship set in the "
"database from node %d - there should only be one"
" relationship flagged as '_neo4django' for a "
"single=True Relationship." %
return self._neo4j_instance(node, django_relationships[0])
def _neo4j_instance(self, this, relationship):
if ==
that = relationship.start
that = relationship.end # get the other node
return self.target_model._neo4j_instance(that)
def _del_relationship(self, obj, state):
self._set_relationship(obj, state, None)
def _set_relationship(self, obj, state, other):
state[] = True, other
def _save_relationship(self, instance, node, state):
changed, other = state
if not changed:
rels = self._load_relationships(node)
#delete old relationship
#create new relationship
if other is None:
#delete old relationship if it exists
if hasattr(rels, 'single') and rels.single:
rels.single.delete() # TODO this deletion should be transactional w creation
rels.single = None
rels.single = self._create_neo_relationship(node, other)
def save_form_data(self, instance, data):
# TODO we need a function like _get_relationship that only takes a
# model instance...
state = self._state_for(instance)
self._set_relationship(instance, state, data)
def _set_cached_relationship(self, obj, other):
state = BoundRelationship._state_for(obj)
if in state and state[]:
raise ValueError("Can't set the cache on an already initialized relationship!")
state[] = False, other
class BoundRelationshipModel(BoundRelationship):
def __init__(self, rel, cls, relname, attname, Model):
super(BoundRelationship, self).__init__(
rel, cls, relname, attname)
self.Model = Model
raise NotImplementedError("Support for extended relationship "
"models is not yet implemented.")
class SingleRelationship(BoundRelationshipModel): # WAIT!
def _get_relationship(self, obj, state):
def _set_relationship(self, obj, state, other):
class MultipleNodes(BoundRelationship):
#BoundRelationship subclass for a multi-node relationship without an
#associated relationship model.
def value_from_object(self, obj):
return self.__get__(obj).all()
def value_to_string(self, obj):
return str([ for item in list(self.__get__(obj).all())])
def save_form_data(self, instance, data):
# TODO we need a function like _get_relationship that only takes a
# model instance...
states = self._state_for(instance)
self._set_relationship(instance, states, list(data))
def clean(self, value, instance):
# XXX HACK since we don't use a proxy object like
# ForeignRelatedObjectsDescriptor (and actually return a
# RelationshipInstance on getattr(model, field.attname)) so we have to
# unpack a RelationshipInstance
return list(value._added)
def _get_state(self, obj, states):
state = states.get(
if state is None:
states[] = state = RelationshipInstance(self, obj)
return state
def _get_relationship(self, obj, states):
return self._get_state(obj, states)
def _set_relationship(self, obj, states, value):
if value is not None:
state = self._get_state(obj, states)
items = list(state.all())
notsaved = state._added
if items and len(notsaved) < len(items):
#TODO: make it so it only removes authors not in value
# and remove authors already there from value
#XXX: only works when removing from saved nodes
if notsaved:
notsaved[:] = [] # TODO give rel instance a method for this?
if hasattr(value, '__iter__'):
def _neo4j_instance(self, this, relationship):
if ==
that = relationship.end
that = relationship.start
return self.target_model._neo4j_instance(that)
def accept(self, obj):
pass # TODO: implement verification
def _save_relationship(self, instance, node, state):
def _load_relationships(self, node, ordered=False, **kwargs):
sup = super(MultipleNodes, self)._load_relationships(node, **kwargs)
if ordered:
return sorted(sup, key=lambda rel: rel[ORDER_ATTR])
return sup
def _create_neo_relationship(self, node, *args, **kwargs):
if self.ordered:
rels = self._load_relationships(node, ordered=True, **kwargs)
new_index = rels[-1][ORDER_ATTR] + 1 if len(rels) > 0 else 0
if 'attrs' in kwargs:
kwargs['attrs'][ORDER_ATTR] = new_index
kwargs['attrs'] = {ORDER_ATTR: new_index}
return super(MultipleNodes, self)._create_neo_relationship(node, *args, **kwargs)
class MultipleRelationships(BoundRelationshipModel): # WAIT!
def _get_relationship(self, obj, state):
def add(self, obj, other):
# TODO this needs to be supplanted by using somthing like django.db.models
# .fields.related.ForeignRelatedObjectsDescriptor
class RelationshipInstance(models.Manager):
A manager that keeps state for the many side (`MultipleNodes`) of
def __init__(self, rel, obj):
self._rel = rel
self._obj = obj
self._added = [] # contains domain objects
self._removed = [] # contains relationships
#holds cached domain objects (that have been added or loaded by query)
self._cache = None
self._cache_unique = set([])
# sender should be the associated model (not any associated LazyModel)
sender = (self._rel.target_model._model
if hasattr(self._rel.target_model, '_model')
else self._rel.target_model)
@receiver(post_delete, sender=sender, weak=False)
def delete_handler(sender, **kwargs):
deleted_obj = kwargs.pop('instance', None)
if deleted_obj:
if deleted_obj in self._added:
ordered = property(lambda self: self._rel.ordered)
def _add_to_cache(self, *relationship_neo4j_pairs):
for pair in relationship_neo4j_pairs:
if pair not in self._cache_unique:
def _remove_from_cache(self, obj):
if self._cache is not None:
for r, cached_obj in self._cache[:]:
if cached_obj == obj:
pair = (r, cached_obj)
def _has_cache(self):
return self._cache is not None
def _get_or_create_cache(self):
if self._cache is None:
self._cache = []
return self._cache
def __save__(self, node):
#Deletes all relationships removed since last save and adds any new
#relatonships to the database.
#TODO this should be batched
for relationship in self._removed:
for obj in self._added:
new_rel = self._rel._create_neo_relationship(node, obj)
self._add_to_cache((new_rel, obj))
self._removed[:] = []
self._added[:] = []
def _neo4j_relationships_and_models(self, node):
"Returns generator of relationship, neo4j instance tuples associated
with node.
if self._cache is None:
self._add_to_cache(*[(r, self._rel._neo4j_instance(node, r)) for r in
self._rel._load_relationships(node, ordered=self.ordered)])
for tup in self._get_or_create_cache():
if tup[0] not in self._removed:
yield tup
def _new(self):
for item in self._added:
yield item
def _old(self):
for item in self._removed:
yield item
def add(self, *objs):
Adds object(s) to the relationship. If ordered is True for
the relationship, these objects will all be put at the end of the line.
for obj in objs:
def remove(self, *objs):
Remove objects from the relationship. If ordered is True,
remove the first relationship to this object- otherwise, remove one
of the relationships indiscriminately.
rel = self._rel
if hasattr(self._obj, 'node'):
neo_rels = list(rel._load_relationships(self._obj.node,
rels_by_node = defaultdict(list)
for neo_rel in neo_rels:
for obj in objs:
candidate_rels = rels_by_node[obj.node.url] if hasattr(obj, 'node') else []
if candidate_rels:
if candidate_rels[0] not in self._removed:
except ValueError:
raise rel.target_model.DoesNotExist(
"%r is not related to %r." % (obj, self._obj))
for obj in objs:
if obj in self._added:
elif obj in self._cache:
except ValueError:
raise rel.target_model.DoesNotExist(
"%r is not related to %r." % (obj, self._obj))
def clear(self):
all_objs = list(self.all())
def clone(self):
# Should cache be updated as well?
cloned = RelationshipInstance(self._rel, self._obj)
return cloned
def create(self, **kwargs):
kwargs[self._rel.relationship._related_name] = self._obj
new_model = self._rel.relationship.target_model(**kwargs)
# TODO: saving twice, should only need
# to save self._obj after #89 fix
def get_or_create(self, *args, **kwargs):
def get_query_set(self):
return RelationshipQuerySet(self, self._rel, self._obj)
def get_empty_query_set(self):
return EmptyQuerySet()
class RelationshipQuerySet(NodeQuerySet):
def __init__(self, rel_instance, rel, model_instance, model=None,
query=None, using=DEFAULT_DB_ALIAS):
# TODO will cause issues with #138 - multi-typed relationships
target_model = model or rel.relationship.target_model
super(RelationshipQuerySet, self).__init__(
model=target_model, query=query or Query(target_model),
self._rel_instance = rel_instance
self._rel = rel
self._model_instance = model_instance
self.query.set_start_clause(self._get_start_clause(), lambda: {
def _get_start_clause(self):
Return a Cypher START fragment - either a str, or an object with an
as_cypher() method - that will be used as the first half of the query
built executing the query set. The query should expect a parameter named
"startParam" containing this side of the relationship's node id, and
should define a column "n" containing nodes to later be filtered
order_clause = """
ORDER BY r.`%s`
""" % ORDER_ATTR if self._rel_instance.ordered else ''
start = Start({'m': 'node({startParam})'}, ['startParam'])
direction = '>' if self._rel.direction == RELATIONSHIPS_OUT else '<'
match = Match([
order_by = OrderBy([OrderByTerm(ColumnExpression('r', ORDER_ATTR))]) \
if self._rel_instance.ordered else None
return Clauses([
With({'n': 'n', 'r': 'r', 'typeNode': 'typeNode'}, order_by=order_by)
def iterator(self):
added = list(self._rel_instance._new)
if is not None:
for m in super(RelationshipQuerySet, self).iterator():
yield m
for item in added:
yield item
def _clone(self, klass=None, setup=False, **kwargs):
klass = klass or self.__class__
klass = partial(klass, self._rel_instance, self._rel,
return super(RelationshipQuerySet, self)._clone(klass=klass,
setup=setup, **kwargs)
def count(self):
removed = list(self._rel_instance._old)
added = list(self._rel_instance._new)
diff_len = max(len(added) - len(removed), 0)
if is not None:
return super(RelationshipQuerySet, self).count() + diff_len
return diff_len