def get(self, request, *args, **kwargs): concept = self.get_object() links = get_links_for_concept(concept) seen_concepts = set() nodes = [] edges = [] for link in links: # Id to use in output for link (so it doesnt clash with concept ids) link_id = 'link_{id}'.format(id=link.id) # Add link node nodes.append({ 'id': link_id, 'name': link.relation.name, 'definition': link.relation.definition, 'short_definition': link.relation.short_definition, }) for end in link.linkend_set.all(): # Add concept node if end.concept_id not in seen_concepts: nodes.append(ConceptSerializer(end.concept).data) seen_concepts.add(end.concept_id) # Add edge edges.append({ 'from': link_id, 'to': end.concept_id }) return Response( {'nodes': nodes, 'edges': edges}, status.HTTP_200_OK )
def link_json_for_item(request, iid): item = get_object_or_404(MDR._concept, pk=iid).item links = get_links_for_concept(item) nodes = [] edges = [] for link in links: for end in link.linkend_set.all(): if 'concept_%s' % end.concept.id not in [i['id'] for i in nodes]: if end.concept == item.concept: nodes.append({ 'id': 'concept_%s' % end.concept.id, 'label': end.concept.name, 'group': 'active', 'title': "<i>This item</i>", }) else: nodes.append({ 'id': 'concept_%s' % end.concept.id, 'label': end.concept.name, 'group': 'regular', 'title': '<a href="%s">%s</a>' % (end.concept.get_absolute_url(), end.concept.name), }) if end.concept == item.concept: edges.append({ 'to': 'link_%s_%s' % (link.relation.id, link.id), 'from': 'concept_%s' % end.concept.id, # 'label': end.role.name }) else: edges.append({ 'from': 'link_%s_%s' % (link.relation.id, link.id), 'to': 'concept_%s' % end.concept.id, # 'label': end.role.name 'title': end.role.name }) if 'link_%s_%s' % (link.relation.id, link.id) not in [ i['id'] for i in nodes ]: nodes.append({ 'id': 'link_%s_%s' % (link.relation.id, link.id), 'label': link.relation.name, 'group': 'relation', 'title': '<a href="%s">%s</a>' % (link.relation.get_absolute_url(), link.relation.definition), }) return JsonResponse({ 'nodes': nodes, 'edges': edges, })
def link_json_for_item(request, iid): item = get_object_or_404(MDR._concept, pk=iid).item links = get_links_for_concept(item) nodes = [] edges = [] for link in links: for end in link.linkend_set.all(): if 'concept_%s' % end.concept.id not in [i['id'] for i in nodes]: if end.concept == item.concept: nodes.append({ 'id': 'concept_%s' % end.concept.id, 'label': end.concept.name, 'group': 'active', 'title': "<i>This item</i>", }) else: nodes.append({ 'id': 'concept_%s' % end.concept.id, 'label': end.concept.name, 'group': 'regular', 'title': '<a href="%s">%s</a>' % (end.concept.get_absolute_url(), end.concept.name), }) if end.concept == item.concept: edges.append({ 'to': 'link_%s_%s' % (link.relation.id, link.id), 'from': 'concept_%s' % end.concept.id, # 'label': end.role.name }) else: edges.append({ 'from': 'link_%s_%s' % (link.relation.id, link.id), 'to': 'concept_%s' % end.concept.id, # 'label': end.role.name 'title': end.role.name }) if 'link_%s_%s' % (link.relation.id, link.id) not in [i['id'] for i in nodes]: nodes.append({ 'id': 'link_%s_%s' % (link.relation.id, link.id), 'label': link.relation.name, 'group': 'relation', 'title': '<a href="%s">%s</a>' % (link.relation.get_absolute_url(), link.relation.definition), }) return JsonResponse({ 'nodes': nodes, 'edges': edges, })
def get_links(self): return get_links_for_concept(self.item)
def get_links(self): return get_links_for_concept(self.item)