From d2e2d96b7e897ad5d1bb35fe9e9a3e70b2f31c06 Mon Sep 17 00:00:00 2001 From: Marcin Armacki Date: Wed, 24 Jun 2020 19:16:34 +0200 Subject: [PATCH] Endpoint for labels update --- backend/webapp/prototype/filehandler/functions.py | 5 +++++ backend/webapp/prototype/filehandler/views.py | 12 +++++++++++- backend/webapp/prototype/urls.py | 1 - 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/backend/webapp/prototype/filehandler/functions.py b/backend/webapp/prototype/filehandler/functions.py index 20ff7a9..778bb3f 100644 --- a/backend/webapp/prototype/filehandler/functions.py +++ b/backend/webapp/prototype/filehandler/functions.py @@ -32,6 +32,11 @@ def createLabels(data): return(True) +def updateLabelsByParagraphId(data): + for paragraph_ in data: + Paragraph.objects.filter(pk = paragraph_['id']).update(label = paragraph_['label']) + return(True) + def addToDatabase(data, file_id): out = {} para_id = [] diff --git a/backend/webapp/prototype/filehandler/views.py b/backend/webapp/prototype/filehandler/views.py index 21bb74c..48f252f 100644 --- a/backend/webapp/prototype/filehandler/views.py +++ b/backend/webapp/prototype/filehandler/views.py @@ -3,11 +3,12 @@ from django.conf import settings from django.core.files.storage import FileSystemStorage from django.views.decorators.csrf import csrf_exempt from django.http import JsonResponse, HttpResponse +import json from prototype.filehandler.models import Document, Forum from prototype.filehandler.forms import DocumentForm from prototype.filehandler.xmlParser import parseData -from prototype.filehandler.functions import addToDatabase, listDiscussionsFromFile, listParagraphsFromDiscussion, createLabels, listPostsFromDiscussion +from prototype.filehandler.functions import addToDatabase, listDiscussionsFromFile, listParagraphsFromDiscussion, createLabels, listPostsFromDiscussion, updateLabelsByParagraphId def home(request): @@ -33,10 +34,19 @@ def model_form_upload(request): 'form' : form }) +@csrf_exempt def discussions(request, id): if request.method == 'GET': output = listParagraphsFromDiscussion(id) return JsonResponse(output, safe=False) + + elif request.method == 'PATCH': + received_data = json.loads(request.body.decode("utf-8")) + if updateLabelsByParagraphId(received_data): + output = listParagraphsFromDiscussion(id) + return JsonResponse(output, safe=False) + else: + return HttpResponse('Błąd przy dodwaniu informacji do bazy danych', status=406) else: return HttpResponse('Nieobsługiwana metoda HTTP', status=406) diff --git a/backend/webapp/prototype/urls.py b/backend/webapp/prototype/urls.py index 2a4212a..5c4a5dd 100644 --- a/backend/webapp/prototype/urls.py +++ b/backend/webapp/prototype/urls.py @@ -26,7 +26,6 @@ urlpatterns = [ path('admin/', admin.site.urls), path('discussions/', views.discussions), path('visualize/', views.visualize) - ] if settings.DEBUG: