def post(self, request, graph_slug, type_slug, format=None): """ Create a new node of a type for each element from data """ graph = get_object_or_404(Graph, slug=graph_slug) self.check_object_permissions(self.request, graph) nodetype = get_object_or_404(NodeType, slug=type_slug, schema__graph__slug=graph_slug) # We get the post data data = request.data if isinstance(data, (str, unicode)): # We check if we need to deserialize the object data = json.loads(data) nodes_ids = [] transaction_ok = True # We have in data a list of elements to create as new for node_data in data: node_form = NodeForm(graph=graph, itemtype=nodetype, data=node_data, user=request.user.username) if node_data and node_form.is_valid(): with transaction.atomic(): new_node = node_form.save() nodes_ids.append(new_node.id) else: transaction_ok = False break if transaction_ok: return Response(nodes_ids, status=status.HTTP_201_CREATED) else: return Response(node_form.errors, status=status.HTTP_400_BAD_REQUEST)
def nodes_edit(request, graph_slug, node_id): graph = get_object_or_404(Graph, slug=graph_slug) node = graph.nodes.get(node_id) nodetype = get_object_or_404(NodeType, id=node.label) try: media_node = MediaNode.objects.get(node_id=node.id, data=graph.data) except MediaNode.DoesNotExist: media_node = MediaNode() except MediaNode.MultipleObjectsReturned: media_nodes = MediaNode.objects.filter(node_id=node.id, data=graph.data) media_node = media_nodes.latest("id") if request.POST: data = request.POST.copy() mediafile_formset = MediaFileFormSet(instance=media_node, data=data, files=request.FILES, prefix="__files") medialink_formset = MediaLinkFormSet(instance=media_node, data=data, files=request.FILES, prefix="__links") else: data = None mediafile_formset = MediaFileFormSet(instance=media_node, data=data, prefix="__files") medialink_formset = MediaLinkFormSet(instance=media_node, data=data, prefix="__links") node_initial = node.properties.copy() node_initial.update({ITEM_FIELD_NAME: node.id}) node_form = NodeForm(graph=graph, itemtype=nodetype, initial=node_initial, data=data, user=request.user.username) # Outgoing relationships # initial = [] # for relationship in node.relationships.all(): # properties = relationship.properties # outgoing_type = RelationshipType.objects.get(id=relationship.label) # properties.update({ # outgoing_type.id: relationship.target.id, # ITEM_FIELD_NAME: relationship.id, # }) # initial.append(properties) prefixes = [] outgoing_formsets = SortedDict() allowed_outgoing_relationships = nodetype.get_outgoing_relationships( reflexive=True) for relationship in allowed_outgoing_relationships: initial = [] graph_relationships = node.relationships.filter(label=relationship.id) for graph_relationship in graph_relationships: # Only show outgoing relationships even if it is reflexive if graph_relationship.target.id != node.id: properties = graph_relationship.properties properties.update({ relationship.id: graph_relationship.target.id, ITEM_FIELD_NAME: graph_relationship.id, }) initial.append(properties) arity = relationship.arity_target if arity > 0: RelationshipFormSet = formset_factory(RelationshipForm, formset=TypeBaseFormSet, max_num=arity, extra=1) else: RelationshipFormSet = formset_factory(RelationshipForm, formset=TypeBaseFormSet, can_delete=True, extra=1) relationship_slug = "o_%s%s" % (relationship.name, relationship.id) formset_prefix = slugify(relationship_slug).replace("-", "_") prefixes.append({ "key": formset_prefix, "value": u"→ %s (%s)" % (relationship.name, relationship.target.name) }) outgoing_formset = RelationshipFormSet(graph=graph, itemtype=relationship, instance=nodetype, related_node=node, direction=TARGET, prefix=formset_prefix, initial=initial, data=data, user=request.user.username) outgoing_formsets[formset_prefix] = outgoing_formset # Incoming relationships # initial = [] # for relationship in node.relationships.all(): # properties = relationship.properties # incoming_type = RelationshipType.objects.get(id=relationship.label) # properties.update({ # incoming_type.id: relationship.source.id, # ITEM_FIELD_NAME: relationship.id, # }) # initial.append(properties) incoming_formsets = SortedDict() allowed_incoming_relationships = nodetype.get_incoming_relationships( reflexive=True) for relationship in allowed_incoming_relationships: initial = [] graph_relationships = node.relationships.filter(label=relationship.id) for graph_relationship in graph_relationships: # Only show incoming relationships even if it is reflexive if graph_relationship.source.id != node.id: properties = graph_relationship.properties properties.update({ relationship.id: graph_relationship.source.id, ITEM_FIELD_NAME: graph_relationship.id, }) initial.append(properties) arity = relationship.arity_source if arity > 0: RelationshipFormSet = formset_factory(RelationshipForm, formset=TypeBaseFormSet, max_num=arity, extra=1) else: RelationshipFormSet = formset_factory(RelationshipForm, formset=TypeBaseFormSet, can_delete=True, extra=1) relationship_slug = "i_%s%s" % (relationship.name, relationship.id) formset_prefix = slugify(relationship_slug).replace("-", "_") prefixes.append({ "key": formset_prefix, "value": u"← %s (%s)" % (relationship.name, relationship.source.name) }) incoming_formset = RelationshipFormSet(graph=graph, itemtype=relationship, instance=nodetype, related_node=node, direction=SOURCE, prefix=formset_prefix, initial=initial, data=data, user=request.user.username) incoming_formsets[formset_prefix] = incoming_formset # Save forms and formsets if (data and node_form.is_valid() and mediafile_formset.is_valid() and medialink_formset.is_valid() and all([rf.is_valid() for rf in outgoing_formsets.values()]) and all([rf.is_valid() for rf in incoming_formsets.values()])): with transaction.commit_manually(): try: as_new = 'as-new' in request.POST node = node_form.save(as_new=as_new) for outgoing_formset in outgoing_formsets.values(): for outgoing_form in outgoing_formset.forms: if not (outgoing_form.delete and as_new): # The if statement saves execution time outgoing_form.save(related_node=node, as_new=as_new) for incoming_formset in incoming_formsets.values(): for incoming_form in incoming_formset.forms: if not (incoming_form.delete and as_new): # The if statement saves execution time incoming_form.save(related_node=node, as_new=as_new) if as_new: mediafile_formset.forms = [ modify_media_form(form) for form in mediafile_formset.forms if can_media_save_as_new(form) ] medialink_formset.forms = [ modify_media_form(form) for form in medialink_formset.forms if can_media_save_as_new(form) ] mediafiles = mediafile_formset.save(commit=False) medialinks = medialink_formset.save(commit=False) # Manage files and links if ((as_new or not media_node.pk) and (mediafiles or medialinks)): media_node = MediaNode.objects.create(node_id=node.id, data=graph.data) for mediafile in mediafiles: mediafile.media_node = media_node if as_new and mediafile.pk: mediafile.pk = None clone_file(mediafile) mediafile.save() for medialink in medialinks: medialink.media_node = media_node if as_new and medialink.pk: medialink.pk = None medialink.save() if (media_node.pk and not media_node.files.exists() and not media_node.links.exists()): media_node.delete() except: transaction.rollback() else: transaction.commit() redirect_url = reverse("nodes_list_full", args=[graph.slug, nodetype.id]) return redirect(redirect_url) return render_to_response('nodes_editcreate.html', { "graph": graph, "nodetype": nodetype, "node_form": node_form, "node": node, "prefixes": prefixes, "outgoing_formsets": outgoing_formsets, "incoming_formsets": incoming_formsets, "mediafile_formset": mediafile_formset, "medialink_formset": medialink_formset, "action": _("Edit"), "delete": True, "as_new": True }, context_instance=RequestContext(request))
def nodes_create(request, graph_slug, node_type_id): graph = get_object_or_404(Graph, slug=graph_slug) nodetype = get_object_or_404(NodeType, id=node_type_id) if not nodetype.schema.graph == graph: raise Http404(_("Mismatch in requested graph and node type's graph.")) if request.POST: data = request.POST.copy() mediafile_formset = MediaFileFormSet(data=data, files=request.FILES, prefix="__files") medialink_formset = MediaLinkFormSet(data=data, files=request.FILES, prefix="__links") else: data = None mediafile_formset = MediaFileFormSet(prefix="__files") medialink_formset = MediaLinkFormSet(prefix="__links") node_form = NodeForm(graph=graph, itemtype=nodetype, data=data, user=request.user.username) outgoing_formsets = SortedDict() prefixes = [] for relationship in nodetype.outgoing_relationships.all(): arity = relationship.arity_target if arity > 0: RelationshipFormSet = formset_factory(RelationshipForm, formset=TypeBaseFormSet, max_num=arity, extra=1) else: RelationshipFormSet = formset_factory(RelationshipForm, formset=TypeBaseFormSet, can_delete=True, extra=1) relationship_slug = "o_%s%s" % (relationship.name, relationship.id) formset_prefix = slugify(relationship_slug).replace("-", "_") prefixes.append({ "key": formset_prefix, "value": u"→ %s (%s)" % (relationship.name, relationship.target.name) }) outgoing_formset = RelationshipFormSet(graph=graph, itemtype=relationship, instance=nodetype, direction=TARGET, prefix=formset_prefix, data=data, user=request.user.username) outgoing_formsets[formset_prefix] = outgoing_formset incoming_formsets = SortedDict() for relationship in nodetype.incoming_relationships.all(): arity = relationship.arity_source if arity > 0: RelationshipFormSet = formset_factory(RelationshipForm, formset=TypeBaseFormSet, max_num=arity, extra=1) else: RelationshipFormSet = formset_factory(RelationshipForm, formset=TypeBaseFormSet, can_delete=True, extra=1) relationship_slug = "i_%s%s" % (relationship.name, relationship.id) formset_prefix = slugify(relationship_slug).replace("-", "_") prefixes.append({ "key": formset_prefix, "value": u"← %s (%s)" % (relationship.name, relationship.source.name) }) incoming_formset = RelationshipFormSet(graph=graph, itemtype=relationship, instance=nodetype, direction=SOURCE, prefix=formset_prefix, data=data, user=request.user.username) incoming_formsets[formset_prefix] = incoming_formset if (data and node_form.is_valid() and mediafile_formset.is_valid() and medialink_formset.is_valid() and all([rf.is_valid() for rf in outgoing_formsets.values()]) and all([rf.is_valid() for rf in incoming_formsets.values()])): with transaction.commit_manually(): try: node = node_form.save() for outgoing_formset in outgoing_formsets.values(): for outgoing_form in outgoing_formset.forms: outgoing_form.save(related_node=node) for incoming_formset in incoming_formsets.values(): for incoming_form in incoming_formset.forms: incoming_form.save(related_node=node) # Manage files and links mediafiles = mediafile_formset.save(commit=False) medialinks = medialink_formset.save(commit=False) if mediafiles or medialinks: media_node = MediaNode.objects.create(node_id=node.id, data=graph.data) for mediafile in mediafiles: mediafile.media_node = media_node mediafile.save() for medialink in medialinks: medialink.media_node = media_node medialink.save() except: transaction.rollback() else: transaction.commit() redirect_url = reverse("nodes_list_full", args=[graph.slug, node_type_id]) return redirect(redirect_url) return render_to_response('nodes_editcreate.html', { "graph": graph, "nodetype": nodetype, "node_form": node_form, "prefixes": prefixes, "outgoing_formsets": outgoing_formsets, "incoming_formsets": incoming_formsets, "mediafile_formset": mediafile_formset, "medialink_formset": medialink_formset, "action": u"%s %s" % (_("New"), nodetype.name) }, context_instance=RequestContext(request))
def nodes_edit(request, graph_slug, node_id): graph = get_object_or_404(Graph, slug=graph_slug) node = graph.nodes.get(node_id) nodetype = get_object_or_404(NodeType, id=node.label) try: media_node = MediaNode.objects.get(node_id=node.id, data=graph.data) except MediaNode.MultipleObjectsReturned: media_nodes = MediaNode.objects.filter(node_id=node.id, data=graph.data) media_node = media_nodes.latest("id") except MediaNode.DoesNotExist: media_node = MediaNode() if request.POST: data = request.POST.copy() mediafile_formset = MediaFileFormSet(instance=media_node, data=data, files=request.FILES, prefix="__files") medialink_formset = MediaLinkFormSet(instance=media_node, data=data, files=request.FILES, prefix="__links") else: data = None mediafile_formset = MediaFileFormSet(instance=media_node, data=data, prefix="__files") medialink_formset = MediaLinkFormSet(instance=media_node, data=data, prefix="__links") node_initial = node.properties.copy() node_initial.update({ITEM_FIELD_NAME: node.id}) node_form = NodeForm(itemtype=nodetype, initial=node_initial, data=data) # Outgoing relationships # initial = [] # for relationship in node.relationships.all(): # properties = relationship.properties # outgoing_type = RelationshipType.objects.get(id=relationship.label) # properties.update({ # outgoing_type.id: relationship.target.id, # ITEM_FIELD_NAME: relationship.id, # }) # initial.append(properties) prefixes = [] outgoing_formsets = SortedDict() allowed_outgoing_relationships = nodetype.outgoing_relationships.all() for relationship in allowed_outgoing_relationships: initial = [] graph_relationships = node.relationships.filter(label=relationship.id) for graph_relationship in graph_relationships: properties = graph_relationship.properties properties.update({ relationship.id: graph_relationship.target.id, ITEM_FIELD_NAME: graph_relationship.id, }) initial.append(properties) arity = relationship.arity_target if arity > 0: RelationshipFormSet = formset_factory(RelationshipForm, formset=TypeBaseFormSet, max_num=arity, extra=1) else: RelationshipFormSet = formset_factory(RelationshipForm, formset=TypeBaseFormSet, can_delete=True, extra=1) relationship_slug = "o_%s%s_%s" % (relationship.name, relationship.id, relationship.target.id) formset_prefix = slugify(relationship_slug).replace("-", "_") prefixes.append({ "key": formset_prefix, "value": u"→ %s (%s)" % (relationship.name, relationship.target.name) }) outgoing_formset = RelationshipFormSet(itemtype=relationship, instance=nodetype, prefix=formset_prefix, initial=initial, data=data) outgoing_formsets[formset_prefix] = outgoing_formset # Incoming relationships # initial = [] # for relationship in node.relationships.all(): # properties = relationship.properties # incoming_type = RelationshipType.objects.get(id=relationship.label) # properties.update({ # incoming_type.id: relationship.source.id, # ITEM_FIELD_NAME: relationship.id, # }) # initial.append(properties) incoming_formsets = SortedDict() allowed_incoming_relationships = nodetype.get_incoming_relationships() for relationship in allowed_incoming_relationships: initial = [] graph_relationships = node.relationships.filter(label=relationship.id) for graph_relationship in graph_relationships: properties = graph_relationship.properties properties.update({ relationship.id: graph_relationship.source.id, ITEM_FIELD_NAME: graph_relationship.id, }) initial.append(properties) arity = relationship.arity_source if arity > 0: RelationshipFormSet = formset_factory(RelationshipForm, formset=TypeBaseFormSet, max_num=arity, extra=1) else: RelationshipFormSet = formset_factory(RelationshipForm, formset=TypeBaseFormSet, can_delete=True, extra=1) relationship_slug = "i_%s%s_%s" % (relationship.name, relationship.id, relationship.source.id) formset_prefix = slugify(relationship_slug).replace("-", "_") prefixes.append({ "key": formset_prefix, "value": u"← %s (%s)" % (relationship.name, relationship.source.name) }) incoming_formset = RelationshipFormSet(itemtype=relationship, instance=nodetype, prefix=formset_prefix, initial=initial, data=data) incoming_formsets[formset_prefix] = incoming_formset # Save forms and formsets if (data and node_form.is_valid() and mediafile_formset.is_valid() and medialink_formset.is_valid() and all([rf.is_valid() for rf in outgoing_formsets.values()]) and all([rf.is_valid() for rf in incoming_formsets.values()])): # TODO: This should be under a transaction node = node_form.save() for outgoing_formset in outgoing_formsets.values(): for outgoing_form in outgoing_formset.forms: outgoing_form.save(related_node=node) for incoming_formset in incoming_formsets.values(): for incoming_form in incoming_formset.forms: incoming_form.save(related_node=node) mediafiles = mediafile_formset.save(commit=False) # Manage files and links for mediafile in mediafiles: mediafile.media_node = media_node mediafile.save() medialinks = medialink_formset.save(commit=False) for medialink in medialinks: medialink.media_node = media_node medialink.save() redirect_url = reverse("nodes_list_full", args=[graph.slug, nodetype.id]) return redirect(redirect_url) return render_to_response('nodes_editcreate.html', { "graph": graph, "nodetype": nodetype, "node_form": node_form, "node": node, "prefixes": prefixes, "outgoing_formsets": outgoing_formsets, "incoming_formsets": incoming_formsets, "mediafile_formset": mediafile_formset, "medialink_formset": medialink_formset, "action": _("Edit"), "delete": True }, context_instance=RequestContext(request))
def nodes_edit(request, graph_slug, node_id): graph = get_object_or_404(Graph, slug=graph_slug) node = graph.nodes.get(node_id) nodetype = get_object_or_404(NodeType, id=node.label) try: media_node = MediaNode.objects.get(node_id=node.id, data=graph.data) except MediaNode.DoesNotExist: media_node = MediaNode() except MediaNode.MultipleObjectsReturned: media_nodes = MediaNode.objects.filter(node_id=node.id, data=graph.data) media_node = media_nodes.latest("id") if request.POST: data = request.POST.copy() as_modal = bool(data.get("asModal", False)) mediafile_formset = MediaFileFormSet(instance=media_node, data=data, files=request.FILES, prefix="__files") medialink_formset = MediaLinkFormSet(instance=media_node, data=data, files=request.FILES, prefix="__links") else: data = None as_modal = bool(request.GET.copy().get("asModal", False)) mediafile_formset = MediaFileFormSet(instance=media_node, data=data, prefix="__files") medialink_formset = MediaLinkFormSet(instance=media_node, data=data, prefix="__links") node_initial = node.properties.copy() node_initial.update({ITEM_FIELD_NAME: node.id}) node_form = NodeForm(graph=graph, itemtype=nodetype, initial=node_initial, data=data, user=request.user.username) # Outgoing relationships prefixes = [] outgoing_formsets = SortedDict() allowed_outgoing_relationships = nodetype.get_outgoing_relationships( reflexive=True) for relationship in allowed_outgoing_relationships: initial = [] graph_relationships = node.relationships.filter(label=relationship.id) for graph_relationship in graph_relationships: # Only show outgoing relationships even if it is reflexive if graph_relationship.target.id != node.id: properties = graph_relationship.properties properties.update({ relationship.id: graph_relationship.target.id, ITEM_FIELD_NAME: graph_relationship.id, }) initial.append(properties) arity = relationship.arity_target if arity > 0: RelationshipFormSet = formset_factory(RelationshipForm, formset=TypeBaseFormSet, max_num=arity, extra=1) else: RelationshipFormSet = formset_factory(RelationshipForm, formset=TypeBaseFormSet, can_delete=True, extra=1) relationship_slug = "o_%s%s" % (relationship.name, relationship.id) formset_prefix = slugify(relationship_slug).replace("-", "_") prefixes.append({ "key": formset_prefix, "value": u"→ %s (%s)" % (relationship.name, relationship.target.name) }) outgoing_formset = RelationshipFormSet(graph=graph, itemtype=relationship, instance=nodetype, related_node=node, direction=TARGET, prefix=formset_prefix, initial=initial, data=data, user=request.user.username) outgoing_formsets[formset_prefix] = outgoing_formset # Incoming relationships incoming_formsets = SortedDict() allowed_incoming_relationships = nodetype.get_incoming_relationships( reflexive=True) for relationship in allowed_incoming_relationships: initial = [] graph_relationships = node.relationships.filter(label=relationship.id) for graph_relationship in graph_relationships: # Only show incoming relationships even if it is reflexive if graph_relationship.source.id != node.id: properties = graph_relationship.properties properties.update({ relationship.id: graph_relationship.source.id, ITEM_FIELD_NAME: graph_relationship.id, }) initial.append(properties) arity = relationship.arity_source if arity > 0: RelationshipFormSet = formset_factory(RelationshipForm, formset=TypeBaseFormSet, max_num=arity, extra=1) else: RelationshipFormSet = formset_factory(RelationshipForm, formset=TypeBaseFormSet, can_delete=True, extra=1) relationship_slug = "i_%s%s" % (relationship.name, relationship.id) formset_prefix = slugify(relationship_slug).replace("-", "_") prefixes.append({ "key": formset_prefix, "value": u"← %s (%s)" % (relationship.name, relationship.source.name) }) incoming_formset = RelationshipFormSet(graph=graph, itemtype=relationship, instance=nodetype, related_node=node, direction=SOURCE, prefix=formset_prefix, initial=initial, data=data, user=request.user.username) incoming_formsets[formset_prefix] = incoming_formset # Save forms and formsets if (data and node_form.is_valid() and mediafile_formset.is_valid() and medialink_formset.is_valid() and all([rf.is_valid() for rf in outgoing_formsets.values()]) and all([rf.is_valid() for rf in incoming_formsets.values()])): with transaction.atomic(): as_new = 'as-new' in request.POST if as_modal and not as_new: # This is only needed for delete these relationships from the # Sigma's internal graph, only when the node is saved not as # new and when we are in modal mode. old_relationship_ids = [] for rel in node.relationships.all(): source_id = rel.source.id target_id = rel.target.id if (source_id == node.id or target_id == node.id): old_relationship_ids.append(str(rel.id)) node = node_form.save(as_new=as_new) for outgoing_formset in outgoing_formsets.values(): for outgoing_form in outgoing_formset.forms: if not (outgoing_form.delete and as_new): # The if statement saves execution time outgoing_form.save(related_node=node, as_new=as_new) for incoming_formset in incoming_formsets.values(): for incoming_form in incoming_formset.forms: if not (incoming_form.delete and as_new): # The if statement saves execution time incoming_form.save(related_node=node, as_new=as_new) if as_new: mediafile_formset.forms = [ modify_media_form(form) for form in mediafile_formset.forms if can_media_save_as_new(form) ] medialink_formset.forms = [ modify_media_form(form) for form in medialink_formset.forms if can_media_save_as_new(form) ] mediafiles = mediafile_formset.save(commit=False) medialinks = medialink_formset.save(commit=False) # Manage files and links if ((as_new or not media_node.pk) and (mediafiles or medialinks)): media_node = MediaNode.objects.create(node_id=node.id, data=graph.data) for mediafile in mediafiles: mediafile.media_node = media_node if as_new and mediafile.pk: mediafile.pk = None clone_file(mediafile) mediafile.save() for medialink in medialinks: medialink.media_node = media_node if as_new and medialink.pk: medialink.pk = None medialink.save() if (media_node.pk and not media_node.files.exists() and not media_node.links.exists()): media_node.delete() # If we are here it means that the form was valid and we don't need # to return a form again. if as_modal: if not as_new: action = 'edit' else: action = 'new' relationships = [] for rel in node.relationships.all(): reltype = rel.get_type() source_id = rel.source.id target_id = rel.target.id if (source_id == node.id or target_id == node.id): rel_json = { 'id': str(rel.id), 'source': str(source_id), 'target': str(target_id), 'reltypeId': reltype.id, 'reltype': rel.label_display, 'fullReltype': reltype.__unicode__(), 'color': reltype.get_color(), 'properties': rel.properties } relationships.append(rel_json) # We need to serialize the date and time types manually # because ujson doesn't do this. node = serialize_datetime(node) response = { 'type': 'data', 'action': action, 'nodeId': str(node.id), 'nodeLabel': node.display, 'node': node.to_json(), 'relationships': relationships } if not as_new: response['oldRelationshipIds'] = old_relationship_ids return HttpResponse(json.dumps(response), status=200, content_type='application/json') else: redirect_url = reverse("nodes_list_full", args=[graph.slug, nodetype.id]) return redirect(redirect_url) # If we are here, we need to return the HTML form, empty or with errors. if as_modal: base_template = 'empty.html' render = render_to_string else: base_template = 'base.html' render = render_to_response # This is a way to get the media needed by the form without repeat files forms_media = {'js': set(), 'css': set()} if not as_modal: for form in [node_form] + outgoing_formsets.values() + \ incoming_formsets.values(): forms_media['js'].update(set(form.media.render_js())) forms_media['css'].update( set([css for css in form.media.render_css()])) save_url = reverse("nodes_edit", args=[graph_slug, node_id]) delete_url = reverse("nodes_delete", args=[graph_slug, node_id]) broader_context = { "graph": graph, "nodetype": nodetype, "node_form": node_form, "node": node, "prefixes": prefixes, "outgoing_formsets": outgoing_formsets, "incoming_formsets": incoming_formsets, "mediafile_formset": mediafile_formset, "medialink_formset": medialink_formset, "forms_media": forms_media, "action": _("Edit"), "delete": True, "as_new": True, "base_template": base_template, "as_modal": as_modal, "save_url": save_url, "delete_url": delete_url } response = render('nodes_editcreate.html', broader_context, context_instance=RequestContext(request)) if as_modal: response = {'type': 'html', 'action': 'edit', 'html': response} return HttpResponse(json.dumps(response), status=200, content_type='application/json') else: return response
def nodes_create(request, graph_slug, node_type_id): graph = get_object_or_404(Graph, slug=graph_slug) nodetype = get_object_or_404(NodeType, id=node_type_id) # Variable to control if we want to stay in the saving page add_another = "add-another" if not nodetype.schema.graph == graph: raise Http404(_("Mismatch in requested graph and node type's graph.")) if request.POST: data = request.POST.copy() as_modal = bool(data.get("asModal", False)) mediafile_formset = MediaFileFormSet(data=data, files=request.FILES, prefix="__files") medialink_formset = MediaLinkFormSet(data=data, files=request.FILES, prefix="__links") else: data = None as_modal = bool(request.GET.copy().get("asModal", False)) mediafile_formset = MediaFileFormSet(prefix="__files") medialink_formset = MediaLinkFormSet(prefix="__links") node_form = NodeForm(graph=graph, itemtype=nodetype, data=data, user=request.user.username) outgoing_formsets = SortedDict() prefixes = [] for relationship in nodetype.outgoing_relationships.all(): arity = relationship.arity_target if arity > 0: RelationshipFormSet = formset_factory(RelationshipForm, formset=TypeBaseFormSet, max_num=arity, extra=1) else: RelationshipFormSet = formset_factory(RelationshipForm, formset=TypeBaseFormSet, can_delete=True, extra=1) relationship_slug = "o_%s%s" % (relationship.name, relationship.id) formset_prefix = slugify(relationship_slug).replace("-", "_") prefixes.append({ "key": formset_prefix, "value": u"→ %s (%s)" % (relationship.name, relationship.target.name) }) outgoing_formset = RelationshipFormSet(graph=graph, itemtype=relationship, instance=nodetype, direction=TARGET, prefix=formset_prefix, data=data, user=request.user.username) outgoing_formsets[formset_prefix] = outgoing_formset incoming_formsets = SortedDict() for relationship in nodetype.incoming_relationships.all(): arity = relationship.arity_source if arity > 0: RelationshipFormSet = formset_factory(RelationshipForm, formset=TypeBaseFormSet, max_num=arity, extra=1) else: RelationshipFormSet = formset_factory(RelationshipForm, formset=TypeBaseFormSet, can_delete=True, extra=1) relationship_slug = "i_%s%s" % (relationship.name, relationship.id) formset_prefix = slugify(relationship_slug).replace("-", "_") prefixes.append({ "key": formset_prefix, "value": u"← %s (%s)" % (relationship.name, relationship.source.name) }) incoming_formset = RelationshipFormSet(graph=graph, itemtype=relationship, instance=nodetype, direction=SOURCE, prefix=formset_prefix, data=data, user=request.user.username) incoming_formsets[formset_prefix] = incoming_formset if (data and node_form.is_valid() and mediafile_formset.is_valid() and medialink_formset.is_valid() and all([rf.is_valid() for rf in outgoing_formsets.values()]) and all([rf.is_valid() for rf in incoming_formsets.values()])): with transaction.atomic(): node = node_form.save() for outgoing_formset in outgoing_formsets.values(): for outgoing_form in outgoing_formset.forms: outgoing_form.save(related_node=node) for incoming_formset in incoming_formsets.values(): for incoming_form in incoming_formset.forms: incoming_form.save(related_node=node) # Manage files and links mediafiles = mediafile_formset.save(commit=False) medialinks = medialink_formset.save(commit=False) if mediafiles or medialinks: media_node = MediaNode.objects.create(node_id=node.id, data=graph.data) for mediafile in mediafiles: mediafile.media_node = media_node mediafile.save() for medialink in medialinks: medialink.media_node = media_node medialink.save() # If we are here it means that the form was valid and we don't need # to return a form again. if as_modal: relationships = [] for rel in node.relationships.all(): reltype = rel.get_type() source_id = rel.source.id target_id = rel.target.id if (source_id == node.id or target_id == node.id): rel_json = { 'id': str(rel.id), 'source': str(source_id), 'target': str(target_id), 'reltypeId': reltype.id, 'reltype': rel.label_display, 'fullReltype': reltype.__unicode__(), 'color': reltype.get_color(), 'properties': rel.properties } relationships.append(rel_json) # We need to serialize the date and time types manually # because ujson doesn't do this. node = serialize_datetime(node) response = { 'type': 'data', 'action': 'create', 'nodeId': str(node.id), 'nodeLabel': node.display, 'node': node.to_json(), 'relationships': relationships } return HttpResponse(json.dumps(response), status=200, content_type='application/json') elif add_another in request.POST: redirect_url = reverse("nodes_create", args=[graph.slug, node_type_id]) return redirect(redirect_url) else: redirect_url = reverse("nodes_list_full", args=[graph.slug, node_type_id]) return redirect(redirect_url) # If we are here, we need to return the HTML form, empty or with errors. if as_modal: base_template = 'empty.html' render = render_to_string else: base_template = 'base.html' render = render_to_response # This is a way to get the media needed by the form without repeat files forms_media = {'js': set(), 'css': set()} if not as_modal: for form in [node_form] + outgoing_formsets.values() + \ incoming_formsets.values(): forms_media['js'].update(set(form.media.render_js())) forms_media['css'].update( set([css for css in form.media.render_css()])) save_url = reverse("nodes_create", args=[graph_slug, nodetype.id]) delete_url = '' broader_context = { "graph": graph, "nodetype": nodetype, "node_form": node_form, "prefixes": prefixes, "outgoing_formsets": outgoing_formsets, "incoming_formsets": incoming_formsets, "mediafile_formset": mediafile_formset, "medialink_formset": medialink_formset, "forms_media": forms_media, "action": u"%s %s" % (_("New"), nodetype.name), "base_template": base_template, "as_modal": as_modal, "save_url": save_url, "delete_url": delete_url } response = render('nodes_editcreate.html', broader_context, context_instance=RequestContext(request)) if as_modal: response = {'type': 'html', 'action': 'create', 'html': response} return HttpResponse(json.dumps(response), status=200, content_type='application/json') else: return response