Beispiel #1
0
def save_graph(request):
    if request.method == 'POST':
        jsonData = json.loads(request.body.decode('utf-8'))
        jsonNodes = jsonData["nodes"]
        jsonNodesClean = []
        jsonEdges = jsonData["edges"]
        jsonEdgesClean = []


        for node in jsonNodes:
            tmp = {}
            for attribute in node:
                if (attribute == "id"):
                    tmp[attribute] = node[attribute]
                if (attribute == "name"):
                    tmp[attribute] = node[attribute]
                    tmp["slug"] = slugify(node[attribute])
            jsonNodesClean.append(tmp)

        for edge in jsonEdges:
            tmp = {}
            tmp["parent_slug"] = slugify(edge["parent_name"])
            tmp["child_slug"] = slugify(edge["child_name"])
            tmp["slug"] = tmp["parent_slug"] + tmp["child_slug"]
            jsonEdgesClean.append(tmp)

        querysetNodes = Semantic.objects.all()
        querysetEdges = SemanticEdge.objects.all()

        serializerNodes = SemanticNodeSerializer(querysetNodes, data=jsonNodesClean, many=True)
        serializerEdges = SemanticEdgeSerializer(querysetEdges, data=jsonEdgesClean, many=True)

        if serializerNodes.is_valid():
            serializerNodes.save()

        if serializerEdges.is_valid():
            serializerEdges.save()

        return HttpResponse(json.dumps({'message': 0}))

    return HttpResponse(json.dumps({'message': 1}))
Beispiel #2
0
def add_edge(request):
    if request.method == 'POST':
        jsonData = json.loads(request.body.decode('utf-8'))

        edge = {}

        for attribute in jsonData:
            edge["parent_slug"] = slugify(jsonData["parent_name"])
            edge["child_slug"] = slugify(jsonData["child_name"])
            edge["slug"] = edge["parent_slug"] + edge["child_slug"]

        serializerEdges = SemanticEdgeSerializer(data=edge)

        if serializerEdges.is_valid():
            serializerEdges.save()

            return HttpResponse(json.dumps({'message': 0}))
        else:
            print("ERROR")
            print(serializerEdges.errors)
            return HttpResponse(json.dumps({'message': 1}))