Endpoint for labels update

This commit is contained in:
Marcin Armacki 2020-06-24 19:16:34 +02:00
parent c6212fd1a3
commit d2e2d96b7e
3 changed files with 16 additions and 2 deletions

View File

@ -32,6 +32,11 @@ def createLabels(data):
return(True) 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): def addToDatabase(data, file_id):
out = {} out = {}
para_id = [] para_id = []

View File

@ -3,11 +3,12 @@ from django.conf import settings
from django.core.files.storage import FileSystemStorage from django.core.files.storage import FileSystemStorage
from django.views.decorators.csrf import csrf_exempt from django.views.decorators.csrf import csrf_exempt
from django.http import JsonResponse, HttpResponse from django.http import JsonResponse, HttpResponse
import json
from prototype.filehandler.models import Document, Forum from prototype.filehandler.models import Document, Forum
from prototype.filehandler.forms import DocumentForm from prototype.filehandler.forms import DocumentForm
from prototype.filehandler.xmlParser import parseData 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): def home(request):
@ -33,10 +34,19 @@ def model_form_upload(request):
'form' : form 'form' : form
}) })
@csrf_exempt
def discussions(request, id): def discussions(request, id):
if request.method == 'GET': if request.method == 'GET':
output = listParagraphsFromDiscussion(id) output = listParagraphsFromDiscussion(id)
return JsonResponse(output, safe=False) 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: else:
return HttpResponse('Nieobsługiwana metoda HTTP', status=406) return HttpResponse('Nieobsługiwana metoda HTTP', status=406)

View File

@ -26,7 +26,6 @@ urlpatterns = [
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
path('discussions/<int:id>', views.discussions), path('discussions/<int:id>', views.discussions),
path('visualize/<int:id>', views.visualize) path('visualize/<int:id>', views.visualize)
] ]
if settings.DEBUG: if settings.DEBUG: