def get_model_node(model): # Retreive the name of the model in the database model_db_name = "%s:%s" % (model._meta.app_label, model.__name__) model_cache_key = "model_node_%s" % model_db_name # Use the cache? if cache.get(model_cache_key, None) is not None: # Simply get the node return connection.node.get(cache.get(model_cache_key)) # Build the query to retreive this model query = """ START n=node(0) MATCH n-[r:`<<TYPE>>`]->m WHERE HAS(m.name) AND m.name = '%s' RETURN m """ % model_db_name # Extract the node for this model model_node = connection.query(query, returns=client.Node) # Does the node exist for this model? if len(model_node): # Cache the node id for later cache.set(model_cache_key, model_node[0][0].id) # And returns it return model_node[0][0] # The model may not exist YET else: # We force neo4django to create the model into the database # by creating a node from this model node = model() node.save() # Then we delete it instantanetly node.delete() # And recursivly call this function return get_model_node(model)
def get_model_node(model): # Retreive the name of the model in the database model_db_name = "%s:%s" % (model._meta.app_label, model.__name__) model_cache_key = "model_node_%s" % model_db_name # Use the cache? if cache.get(model_cache_key, None) is not None: # Simply get the node return connection.node.get( cache.get(model_cache_key) ) # Build the query to retreive this model query = """ START n=node(0) MATCH n-[r:`<<TYPE>>`]->m WHERE HAS(m.name) AND m.name = '%s' RETURN m """ % model_db_name # Extract the node for this model model_node = connection.query(query, returns=client.Node ) # Does the node exist for this model? if len(model_node): # Cache the node id for later cache.set(model_cache_key, model_node[0][0].id) # And returns it return model_node[0][0] # The model may not exist YET else: # We force neo4django to create the model into the database # by creating a node from this model node = model() node.save() # Then we delete it instantanetly node.delete() # And recursivly call this function return get_model_node(model)
def alter_detail_data_to_serialize(self, request, bundle, nested=False): model = self.get_model() # Get relationships fields fields = [ f for f in model._meta.fields if f.get_internal_type() == 'Relationship'] node_rels = bundle.obj.node.relationships.all() # If the nested parameter is True, this set node_to_retreive = set() # Resolve relationships manualy for field in fields: # Get relationships for this fields field_rels = [ rel for rel in node_rels[:] if rel.type == field._type] # Filter relationships to keep only the well oriented relationships # get the related field informations related_field = [f for f in iterate_model_fields(model) if "rel_type" in f and f["rel_type"] == field._type and "name" in f and f["name"] == field._BoundRelationship__attname] if related_field: # Note (edouard): check some assertions in case I forgot something assert len(related_field) == 1, related_field assert related_field[0]["direction"] # choose the end point to check end_point_side = "start" if related_field[0]["direction"] == "out" else "end" # filter the relationship field_rels = [rel for rel in field_rels if getattr(rel, end_point_side).id == bundle.obj.id] # Get node ids for those relationships field_oposites = [ graph.opposite(rel, bundle.obj.id) for rel in field_rels ] # Save the list into properities bundle.data[field.name] = field_oposites # Nested mode to true: we need to retreive every node if nested: node_to_retreive = set(list(node_to_retreive) + field_oposites) # There is node to extract for the graph if len(node_to_retreive): # Build the query to get all node in one request query = "start n=node(%s) RETURN ID(n), n" % ",".join(map(str, node_to_retreive)) # Get all nodes as raw values to avoid unintended request to the graph nodes = connection.query(query, returns=(int, dict)) # Helper lambda to retreive a node retreive_node = lambda idx: next(n[1]["data"] for n in nodes if n[0] == idx) # Populate the relationships field with there node instance for field in fields: # Retreive the list of ids for i, idx in enumerate(bundle.data[field.name]): rel_node = retreive_node(idx) # Save the id which is not a node property rel_node["id"] = idx # Update value bundle.data[field.name][i] = self.validate(rel_node, field.target_model, allow_missing=True) # Show additional field following the model's rules rules = request.current_topic.get_rules().model(self.get_model()).all() # All additional relationships for key in rules: # Filter rules to keep only Neomatch instance. # Neomatch is a class to create programmaticly a search related to # this node. if isinstance(rules[key], Neomatch): bundle.data[key] = rules[key].query(bundle.obj.id) return bundle