def _clone_data(self, new_graph, nodetypes_map, relationtypes_map): nodes_map = {} # map old/new nodes IDs data = self.data new_data = new_graph.data nodes = self.nodes.all() for n in nodes: node_label = int(n.label) if node_label in nodetypes_map: nt = NodeType.objects.get(pk=nodetypes_map[node_label]) new_node = new_graph.nodes.create(label=unicode(nt.id), properties=n.properties) nodes_map[n.id] = new_node relations = self.relationships.all() for r in relations: rel_label = int(r.label) if rel_label in relationtypes_map: rt = RelationshipType.objects.get( pk=relationtypes_map[rel_label]) new_graph.relationships.create(nodes_map[r.source.id], nodes_map[r.target.id], label=unicode(rt.id), properties=r.properties) media_nodes = data.data.all() for mn in media_nodes: node_id = int(mn.node_id) if node_id in nodes_map: new_mn = MediaNode(node_id=nodes_map[node_id].id, data=new_data) new_mn.save() media_links = mn.links.all() for ml in media_links: new_ml = MediaLink(media_node=new_mn, media_label=ml.media_label, media_link=ml.media_link) new_ml.save()
def _clone_data(self, new_graph, nodetypes_map, relationtypes_map): nodes_map = {} # map old/new nodes IDs data = self.data new_data = new_graph.data nodes = self.nodes.all() for n in nodes: node_label = int(n.label) if node_label in nodetypes_map: nt = NodeType.objects.get(pk=nodetypes_map[node_label]) new_node = new_graph.nodes.create(label=unicode(nt.id), properties=n.properties) nodes_map[n.id] = new_node relations = self.relationships.all() for r in relations: rel_label = int(r.label) if rel_label in relationtypes_map: rt = RelationshipType.objects.get(pk=relationtypes_map[rel_label]) new_graph.relationships.create(nodes_map[r.source.id], nodes_map[r.target.id], label=unicode(rt.id), properties=r.properties) media_nodes = data.data.all() for mn in media_nodes: node_id = int(mn.node_id) if node_id in nodes_map: new_mn = MediaNode(node_id=nodes_map[node_id].id, data=new_data) new_mn.save() media_links = mn.links.all() for ml in media_links: new_ml = MediaLink(media_node=new_mn, media_label=ml.media_label, media_link=ml.media_link) new_ml.save()
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_view(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) nodetype_properties = nodetype.properties.all() node_properties = [] for prop in nodetype_properties: if prop.key in node.properties: node_properties.append({ "key": prop.key, "value": node.properties[prop.key], "type": u"%s" % prop.get_datatype_display() }) 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") prefixes = [] allowed_outgoing_relationships = nodetype.outgoing_relationships.all() outgoing_relationships = [] for relationship in allowed_outgoing_relationships: relationship_slug = "o_%s%s_%s" % (relationship.name, relationship.id, relationship.target.id) prefix = slugify(relationship_slug).replace("-", "_") prefixes.append({ "key": prefix, "value": u"→ %s (%s)" % (relationship.name, relationship.target.name) }) graph_relationships = node.relationships.filter(label=relationship.id) if graph_relationships: outgoing_relationships.append({ "prefix": prefix, "relations": graph_relationships }) allowed_incoming_relationships = nodetype.get_incoming_relationships() incoming_relationships = [] for relationship in allowed_incoming_relationships: relationship_slug = "o_%s%s_%s" % (relationship.name, relationship.id, relationship.target.id) prefix = slugify(relationship_slug).replace("-", "_") prefixes.append({ "key": prefix, "value": u"← %s (%s)" % (relationship.name, relationship.source.name) }) graph_relationships = node.relationships.filter(label=relationship.id) if graph_relationships: incoming_relationships.append({ "prefix": prefix, "relations": graph_relationships }) view_graph_ajax_url = reverse('graphs.views.graph_data', args=[graph.slug, node_id]) edit_nodetype_color_ajax_url = reverse( 'schemas.views.schema_nodetype_edit_color', args=[graph.slug]) return render_to_response( 'nodes_view.html', { "graph": graph, "nodetype": nodetype, "node": node, "node_properties": node_properties, "prefixes": prefixes, "outgoing_relationships": outgoing_relationships, "incoming_relationships": incoming_relationships, "media_links": media_node.links.all(), "media_files": media_node.files.all(), "action": _("View"), "view_graph_ajax_url": view_graph_ajax_url, "edit_nodetype_color_ajax_url": edit_nodetype_color_ajax_url }, 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_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(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()])): 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) mediafiles = mediafile_formset.save(commit=False) medialinks = medialink_formset.save(commit=False) # Manage files and links if 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 mediafile.save() for medialink in medialinks: medialink.media_node = media_node 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}, 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