def schema_relationshiptype_editcreate(request, graph_slug, relationshiptype_id=None): graph = get_object_or_404(Graph, slug=graph_slug) if relationshiptype_id: empty_relationshiptype = get_object_or_404(RelationshipType, id=relationshiptype_id) else: empty_relationshiptype = RelationshipType() initial = {"arity_source": None, "arity_target": None} for field_name in ["source", "name", "target", "inverse"]: if field_name in request.GET: initial[field_name] = request.GET.get(field_name) form = RelationshipTypeForm(initial=initial, schema=graph.schema, instance=empty_relationshiptype) formset = RelationshipTypeFormSet(instance=empty_relationshiptype) if request.POST: data = request.POST.copy() form = RelationshipTypeForm(data=data, schema=graph.schema, instance=empty_relationshiptype) formset = RelationshipTypeFormSet(data=data, instance=empty_relationshiptype) if form.is_valid() and formset.is_valid(): with transaction.commit_on_success(): relationshiptype = form.save(commit=False) if (relationshiptype.source.schema != graph.schema or relationshiptype.target.schema != graph.schema): raise ValidationError("Operation not allowed") relationshiptype.schema = graph.schema relationshiptype.save() instances = formset.save(commit=False) for instance in instances: instance.relationship = relationshiptype instance.save() redirect_url = reverse("schema_edit", args=[graph.slug]) return redirect(redirect_url) return render_to_response('schemas_item_edit.html', { "graph": graph, "item_type_label": _("Allowed Relationship"), "item_type": "relationship", "item_type_id": relationshiptype_id, "form": form, "fields_to_hide": [ "plural_name", "inverse", "plural_inverse", "arity_source", "arity_target", "validation", "inheritance" ], "formset": formset }, context_instance=RequestContext(request))
def search(request, graph_slug, node_type_id=None, relationship_type_id=None): graph = get_object_or_404(Graph, slug=graph_slug) if request.GET: data = request.GET.copy() else: data = request.POST.copy() node_results = [] relationship_results = [] if data: q = data.get("q", "") analytics = bool(data.get("analytics", False)) display = bool(data.get("display", True)) if node_type_id: node_types = NodeType.objects.filter(id=node_type_id, schema__graph=graph) else: node_types = graph.schema.nodetype_set.all() for node_type in node_types: result = {} result["type"] = node_type result["key"] = node_type.name query = graph.Q() if display: properties = node_type.properties.filter(display=True) if not properties: properties = node_type.properties.all()[:2] else: properties = node_type.properties.all() for prop in properties: query |= graph.Q(prop.key, icontains=q, nullable=True) nodes = node_type.filter(query) if nodes: result["list"] = nodes node_results.append(result) # Everything before this point is for regular and analytics mode search if analytics: if ((request.is_ajax() or settings.DEBUG) and request.POST): node_ids = [] for node_result in node_results: node_ids.extend( [str(node.id) for node in node_result["list"]]) result = {'nodeIds': node_ids} return HttpResponse(json.dumps(result), status=200, content_type='application/json') raise Http404( _("Error: Invalid request (expected an AJAX request)")) else: # Beyond this point everything is for regular search if relationship_type_id: filter_args = { "id": relationship_type_id, "schema__graph": graph} relationship_types = RelationshipType.objectsfilter( **filter_args) else: relationship_types = graph.schema.relationshiptype_set.all() for relationship_type in relationship_types: result = {} result["type"] = relationship_type result["key"] = relationship_type.name query = graph.Q() if display: properties = relationship_type.properties.filter( display=True) if not properties: properties = relationship_type.properties.all()[:2] else: properties = relationship_type.properties.all() for prop in properties: query |= graph.Q(prop.key, icontains=q, nullable=True) rels = graph.relationships relationships = rels.filter(query, label=relationship_type.id) if relationships: result["list"] = relationships relationship_results.append(result) return render_to_response('search_results.html', { "graph": graph, "node_results": node_results, "relationship_results": relationship_results, "file_results": []}, context_instance=RequestContext(request))
def _clone_schema(self, schema, new_schema): nodetypes_map = {} # map old/new nodetypes IDs relationtypes_map = {} # map old/new relationtypes IDs nodetypes = schema.nodetype_set.all() for nt in nodetypes: new_nt = NodeType(schema=new_schema, name=nt.name, plural_name=nt.plural_name, description=nt.description, order=nt.order, total=0, validation=nt.validation) new_nt.save() nodetypes_map[nt.id] = new_nt.id properties = nt.properties.all() for np in properties: new_np = NodeProperty(node=new_nt, key=np.key, value=np.value, default=np.default, datatype=np.datatype, required=np.required, display=np.display, description=np.description, validation=np.validation, order=np.order) new_np.save() relationtypes = schema.relationshiptype_set.all() for rt in relationtypes: source = NodeType.objects.get(schema=new_schema, name=rt.source.name) target = NodeType.objects.get(schema=new_schema, name=rt.target.name) new_rt = RelationshipType(schema=new_schema, source=source, target=target, name=rt.name, plural_name=rt.plural_name, description=rt.description, order=rt.order, total=0, validation=rt.validation, inverse=rt.inverse, plural_inverse=rt.plural_inverse, arity_source=rt.arity_source, arity_target=rt.arity_target) new_rt.save() relationtypes_map[rt.id] = new_rt.id properties = rt.properties.all() for rp in properties: new_rp = RelationshipProperty(relationship=new_rt, key=rp.key, value=rp.value, default=rp.default, datatype=rp.datatype, required=rp.required, display=rp.display, description=rp.description, validation=rp.validation, order=rp.order) new_rp.save() return nodetypes_map, relationtypes_map
def search(request, graph_slug, node_type_id=None, relationship_type_id=None): graph = get_object_or_404(Graph, slug=graph_slug) if request.GET: data = request.GET.copy() else: data = request.POST.copy() node_results = [] relationship_results = [] if data: q = data.get("q", "") analytics = bool(data.get("analytics", False)) display = bool(data.get("display", True)) if node_type_id: node_types = NodeType.objects.filter(id=node_type_id, schema__graph=graph) else: node_types = graph.schema.nodetype_set.all() for node_type in node_types: result = {} result["type"] = node_type result["key"] = node_type.name query = graph.Q() if display: properties = node_type.properties.filter(display=True) if not properties: properties = node_type.properties.all()[:2] else: properties = node_type.properties.all() for prop in properties: query |= graph.Q(prop.key, icontains=q, nullable=True) nodes = node_type.filter(query) if nodes: result["list"] = nodes node_results.append(result) # Everything before this point is for regular and analytics mode search if analytics: if ((request.is_ajax() or settings.DEBUG) and request.POST): node_ids = [] for node_result in node_results: node_ids.extend( [str(node.id) for node in node_result["list"]]) result = {'nodeIds': node_ids} return HttpResponse(json.dumps(result), status=200, content_type='application/json') raise Http404( _("Error: Invalid request (expected an AJAX request)")) else: # Beyond this point everything is for regular search if relationship_type_id: filter_args = { "id": relationship_type_id, "schema__graph": graph } relationship_types = RelationshipType.objectsfilter( **filter_args) else: relationship_types = graph.schema.relationshiptype_set.all() for relationship_type in relationship_types: result = {} result["type"] = relationship_type result["key"] = relationship_type.name query = graph.Q() if display: properties = relationship_type.properties.filter( display=True) if not properties: properties = relationship_type.properties.all()[:2] else: properties = relationship_type.properties.all() for prop in properties: query |= graph.Q(prop.key, icontains=q, nullable=True) rels = graph.relationships relationships = rels.filter(query, label=relationship_type.id) if relationships: result["list"] = relationships relationship_results.append(result) return render_to_response('search_results.html', { "graph": graph, "node_results": node_results, "relationship_results": relationship_results, "file_results": [] }, context_instance=RequestContext(request))
def schema_relationshiptype_editcreate(request, graph_slug, relationshiptype_id=None): graph = get_object_or_404(Graph, slug=graph_slug) # We get the modal variable as_modal = bool(request.GET.get("asModal", False)) # We get the schema main view url schema_main_url = reverse("schema_edit", args=[graph.slug]) if relationshiptype_id: empty_relationshiptype = get_object_or_404(RelationshipType, id=relationshiptype_id) add_url = reverse("schema_relationshiptype_edit", args=[graph_slug, relationshiptype_id]) else: empty_relationshiptype = RelationshipType() add_url = reverse("schema_relationshiptype_create", args=[graph_slug]) initial = {"arity_source": None, "arity_target": None} for field_name in ["source", "name", "target", "inverse"]: if field_name in request.GET: initial[field_name] = request.GET.get(field_name) form = RelationshipTypeForm(initial=initial, schema=graph.schema, instance=empty_relationshiptype) formset = RelationshipTypeFormSet(instance=empty_relationshiptype) changed_props = request.session.get('schema_changed_props', None) deleted_props = request.session.get('schema_deleted_props', None) if changed_props: del request.session['schema_changed_props'] if deleted_props: del request.session['schema_deleted_props'] if request.POST: data = request.POST.copy() as_modal = bool(data.get("asModal", False)) form = RelationshipTypeForm(data=data, schema=graph.schema, instance=empty_relationshiptype) formset = RelationshipTypeFormSet(data=data, instance=empty_relationshiptype) if form.is_valid() and formset.is_valid(): with transaction.atomic(): relationshiptype = form.save(commit=False) if (relationshiptype.source.schema != graph.schema or relationshiptype.target.schema != graph.schema): raise ValidationError("Operation not allowed") relationshiptype.schema = graph.schema relationshiptype.save() instances = formset.save(commit=False) for instance in instances: instance.relationship = relationshiptype instance.save() schema_modified = False if formset.deleted_objects: schema_modified = True deleted_props = [] for prop_type in formset.deleted_objects: deleted_props.append({'key': prop_type.key}) request.session['schema_deleted_props'] = deleted_props if formset.changed_objects: changed_props = [] for prop_type, prop_dict in formset.changed_objects: if 'key' in prop_dict: schema_modified = True for data in formset.cleaned_data: if 'key' in data and 'id' in data and \ data['key'] == prop_type.key: changed_props.append({ 'key': data['id'].key, 'new_key': data['key'] }) request.session['schema_changed_props'] = changed_props if schema_modified: redirect_url = \ reverse("schema_relationshiptype_properties_mend", args=[graph.slug, relationshiptype.id]) action = "schema_relationship_editcreate" else: redirect_url = reverse("schema_edit", args=[graph.slug]) action = "schema_main" if as_modal: response = {'type': 'data', 'action': action} return HttpResponse(json.dumps(response), status=200, content_type='application/json') else: return redirect(redirect_url) if as_modal: base_template = 'empty.html' render = render_to_string else: base_template = 'base.html' render = render_to_response broader_context = { "graph": graph, "item_type_label": _("Allowed Relationship"), "item_type": "relationship", "item_type_id": relationshiptype_id, "form": form, "fields_to_hide": [ "plural_name", "inverse", "plural_inverse", "arity_source", "arity_target", "validation", "inheritance" ], "formset": formset, "base_template": base_template, "add_url": add_url, "schema_main_url": schema_main_url, "as_modal": as_modal } response = render('schemas_item_edit.html', broader_context, context_instance=RequestContext(request)) if as_modal: response = { 'type': 'html', 'action': 'schema_relationship_editcreate', 'html': response } return HttpResponse(json.dumps(response), status=200, content_type='application/json') else: return response
def schema_relationshiptype_editcreate(request, graph_slug, relationshiptype_id=None): graph = get_object_or_404(Graph, slug=graph_slug) if relationshiptype_id: empty_relationshiptype = get_object_or_404(RelationshipType, id=relationshiptype_id) else: empty_relationshiptype = RelationshipType() initial = {"arity_source": None, "arity_target": None} for field_name in ["source", "name", "target", "inverse"]: if field_name in request.GET: initial[field_name] = request.GET.get(field_name) form = RelationshipTypeForm(initial=initial, schema=graph.schema, instance=empty_relationshiptype) formset = RelationshipTypeFormSet(instance=empty_relationshiptype) changed_props = request.session.get('schema_changed_props', None) deleted_props = request.session.get('schema_deleted_props', None) if changed_props: del request.session['schema_changed_props'] if deleted_props: del request.session['schema_deleted_props'] if request.POST: data = request.POST.copy() form = RelationshipTypeForm(data=data, schema=graph.schema, instance=empty_relationshiptype) formset = RelationshipTypeFormSet(data=data, instance=empty_relationshiptype) if form.is_valid() and formset.is_valid(): with transaction.commit_on_success(): relationshiptype = form.save(commit=False) if (relationshiptype.source.schema != graph.schema or relationshiptype.target.schema != graph.schema): raise ValidationError("Operation not allowed") relationshiptype.schema = graph.schema relationshiptype.save() instances = formset.save(commit=False) for instance in instances: instance.relationship = relationshiptype instance.save() schema_modified = False if formset.deleted_objects: schema_modified = True deleted_props = [] for prop_type in formset.deleted_objects: deleted_props.append({'key': prop_type.key}) request.session['schema_deleted_props'] = deleted_props if formset.changed_objects: changed_props = [] for prop_type, prop_dict in formset.changed_objects: if 'key' in prop_dict: schema_modified = True for data in formset.cleaned_data: if 'key' in data and 'id' in data and \ data['key'] == prop_type.key: changed_props.append({ 'key': data['id'].key, 'new_key': data['key'] }) request.session['schema_changed_props'] = changed_props if schema_modified: redirect_url = \ reverse("schema_relationshiptype_properties_mend", args=[graph.slug, relationshiptype.id]) else: redirect_url = reverse("schema_edit", args=[graph.slug]) return redirect(redirect_url) return render_to_response('schemas_item_edit.html', { "graph": graph, "item_type_label": _("Allowed Relationship"), "item_type": "relationship", "item_type_id": relationshiptype_id, "form": form, "fields_to_hide": [ "plural_name", "inverse", "plural_inverse", "arity_source", "arity_target", "validation", "inheritance" ], "formset": formset }, context_instance=RequestContext(request))