From 893f59808a315fc8b65f8450ca72e908a40a2ed1 Mon Sep 17 00:00:00 2001 From: Marcin Armacki Date: Sun, 14 Jun 2020 16:28:17 +0200 Subject: [PATCH] Added endpoint for posts retrieval by discussion id --- .../webapp/prototype/filehandler/functions.py | 25 +++++++++++++++++++ backend/webapp/prototype/filehandler/views.py | 9 ++++++- backend/webapp/prototype/urls.py | 2 ++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/backend/webapp/prototype/filehandler/functions.py b/backend/webapp/prototype/filehandler/functions.py index 24300a9..35b4c5e 100644 --- a/backend/webapp/prototype/filehandler/functions.py +++ b/backend/webapp/prototype/filehandler/functions.py @@ -67,3 +67,28 @@ def listDiscussionsFromFile(id): discussions_.append(obj) out['discussions'] = discussions_ return(out) + +def listParagraphsFromDiscussion(id): + out = {} + posts = Post.objects.filter(discussion_id = id) + posts_ = [] + for elem in posts: + obj = {} + obj['id'] = elem.pk + obj['parent'] = elem.parent + obj['author'] = elem.author + message = [] + para_id = [] + label = [] + paragraphs = Paragraph.objects.filter(post_id = elem.pk) + for paragraph in paragraphs: + message.append(paragraph.message) + para_id.append(paragraph.pk) + label.append(paragraph.label) + obj['message'] = message + obj['para_id'] = para_id + obj['label'] = label + posts_.append(obj) + out['posts'] = posts_ + return(out) + diff --git a/backend/webapp/prototype/filehandler/views.py b/backend/webapp/prototype/filehandler/views.py index 9ca5bdf..68cd55c 100644 --- a/backend/webapp/prototype/filehandler/views.py +++ b/backend/webapp/prototype/filehandler/views.py @@ -7,7 +7,7 @@ from django.http import JsonResponse, HttpResponse 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, createLabels +from prototype.filehandler.functions import addToDatabase, listDiscussionsFromFile, listParagraphsFromDiscussion, createLabels def home(request): @@ -32,3 +32,10 @@ def model_form_upload(request): return render(request, 'core/model_form_upload.html', { 'form' : form }) + +def discussions(request, id): + if request.method == 'GET': + output = listParagraphsFromDiscussion(id) + return JsonResponse(output, safe=False) + else: + return HttpResponse('Nieobsługiwana metoda HTTP', status=406) diff --git a/backend/webapp/prototype/urls.py b/backend/webapp/prototype/urls.py index e1c7532..aeb791b 100644 --- a/backend/webapp/prototype/urls.py +++ b/backend/webapp/prototype/urls.py @@ -24,6 +24,8 @@ urlpatterns = [ path('', views.home, name='home'), path('prototype/form/', views.model_form_upload, name='model_form_upload'), path('admin/', admin.site.urls), + path('discussions/', views.discussions) + ] if settings.DEBUG: