def post(self, request, resourceid=None): lang = request.GET.get("lang", settings.LANGUAGE_CODE) se = SearchEngineFactory().create() res = dict(request.POST) relationship_type = res["relationship_properties[relationship_type]"][0] datefrom = res["relationship_properties[datefrom]"][0] dateto = res["relationship_properties[dateto]"][0] dateto = None if dateto == "" else dateto datefrom = None if datefrom == "" else datefrom notes = res["relationship_properties[notes]"][0] root_resourceinstanceid = res["root_resourceinstanceid"] instances_to_relate = [] relationships_to_update = [] if "instances_to_relate[]" in res: instances_to_relate = res["instances_to_relate[]"] if "relationship_ids[]" in res: relationships_to_update = res["relationship_ids[]"] def get_relatable_resources(graphid): """ Takes the graphid of a resource, finds the graphs root node, and returns the relatable graphids """ nodes = models.Node.objects.filter(graph_id=graphid) top_node = [node for node in nodes if node.istopnode == True][0] relatable_resources = [str(node.graph_id) for node in top_node.get_relatable_resources()] return relatable_resources def confirm_relationship_permitted(to_id, from_id): resource_instance_to = models.ResourceInstance.objects.filter(resourceinstanceid=to_id)[0] resource_instance_from = models.ResourceInstance.objects.filter(resourceinstanceid=from_id)[0] relatable_to = get_relatable_resources(resource_instance_to.graph_id) relatable_from = get_relatable_resources(resource_instance_from.graph_id) relatable_to_is_valid = str(resource_instance_to.graph_id) in relatable_from relatable_from_is_valid = str(resource_instance_from.graph_id) in relatable_to return relatable_to_is_valid is True and relatable_from_is_valid is True for instanceid in instances_to_relate: permitted = confirm_relationship_permitted(instanceid, root_resourceinstanceid[0]) if permitted is True: rr = models.ResourceXResource( resourceinstanceidfrom=Resource(root_resourceinstanceid[0]), resourceinstanceidto=Resource(instanceid), notes=notes, relationshiptype=relationship_type, datestarted=datefrom, dateended=dateto, ) try: rr.save() except ModelInactiveError as e: message = _("Unable to save. Please verify the model status is active") return JSONResponse({"status": "false", "message": [_(e.title), _(str(message))]}, status=500) else: print("relationship not permitted") for relationshipid in relationships_to_update: rr = models.ResourceXResource.objects.get(pk=relationshipid) rr.notes = notes rr.relationshiptype = relationship_type rr.datestarted = datefrom rr.dateended = dateto try: rr.save() except ModelInactiveError as e: message = _("Unable to save. Please verify the model status is active") return JSONResponse({"status": "false", "message": [_(e.title), _(str(message))]}, status=500) start = request.GET.get("start", 0) se.es.indices.refresh(index=se._add_prefix("resource_relations")) resource = Resource.objects.get(pk=root_resourceinstanceid[0]) page = 1 if request.GET.get("page") == "" else int(request.GET.get("page", 1)) related_resources = resource.get_related_resources(lang=lang, start=start, limit=1000, page=page) ret = [] if related_resources is not None: ret = self.paginate_related_resources(related_resources, page, request) return JSONResponse(ret, indent=4)
def post(self, request, resourceid=None): lang = request.GET.get('lang', settings.LANGUAGE_CODE) se = SearchEngineFactory().create() res = dict(request.POST) relationship_type = res['relationship_properties[relationship_type]'][ 0] datefrom = res['relationship_properties[datefrom]'][0] dateto = res['relationship_properties[dateto]'][0] dateto = None if dateto == '' else dateto datefrom = None if datefrom == '' else datefrom notes = res['relationship_properties[notes]'][0] root_resourceinstanceid = res['root_resourceinstanceid'] instances_to_relate = [] relationships_to_update = [] if 'instances_to_relate[]' in res: instances_to_relate = res['instances_to_relate[]'] if 'relationship_ids[]' in res: relationships_to_update = res['relationship_ids[]'] def get_relatable_resources(graphid): """ Takes the graphid of a resource, finds the graphs root node, and returns the relatable graphids """ nodes = models.Node.objects.filter(graph_id=graphid) top_node = [node for node in nodes if node.istopnode == True][0] relatable_resources = [ str(node.graph_id) for node in top_node.get_relatable_resources() ] return relatable_resources def confirm_relationship_permitted(to_id, from_id): resource_instance_to = models.ResourceInstance.objects.filter( resourceinstanceid=to_id)[0] resource_instance_from = models.ResourceInstance.objects.filter( resourceinstanceid=from_id)[0] relatable_to = get_relatable_resources( resource_instance_to.graph_id) relatable_from = get_relatable_resources( resource_instance_from.graph_id) relatable_to_is_valid = str( resource_instance_to.graph_id) in relatable_from relatable_from_is_valid = str( resource_instance_from.graph_id) in relatable_to return (relatable_to_is_valid == True and relatable_from_is_valid == True) for instanceid in instances_to_relate: permitted = confirm_relationship_permitted( instanceid, root_resourceinstanceid[0]) if permitted == True: rr = models.ResourceXResource( resourceinstanceidfrom=Resource( root_resourceinstanceid[0]), resourceinstanceidto=Resource(instanceid), notes=notes, relationshiptype=relationship_type, datestarted=datefrom, dateended=dateto) rr.save() else: print 'relationship not permitted' for relationshipid in relationships_to_update: rr = models.ResourceXResource.objects.get(pk=relationshipid) rr.notes = notes rr.relationshiptype = relationship_type rr.datestarted = datefrom rr.dateended = dateto rr.save() start = request.GET.get('start', 0) se.es.indices.refresh(index=se._add_prefix("resource_relations")) resource = Resource.objects.get(pk=root_resourceinstanceid[0]) page = 1 if request.GET.get('page') == '' else int( request.GET.get('page', 1)) related_resources = resource.get_related_resources(lang=lang, start=start, limit=1000, page=page) ret = [] if related_resources is not None: ret = self.paginate_related_resources(related_resources, page, request) return JSONResponse(ret, indent=4)