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 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 _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 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))