Added endpoint for posts retrieval by discussion id

This commit is contained in:
Marcin Armacki 2020-06-14 16:28:17 +02:00
parent 5902c72f2e
commit 893f59808a
3 changed files with 35 additions and 1 deletions

View File

@ -67,3 +67,28 @@ def listDiscussionsFromFile(id):
discussions_.append(obj) discussions_.append(obj)
out['discussions'] = discussions_ out['discussions'] = discussions_
return(out) 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)

View File

@ -7,7 +7,7 @@ from django.http import JsonResponse, HttpResponse
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, createLabels from prototype.filehandler.functions import addToDatabase, listDiscussionsFromFile, listParagraphsFromDiscussion, createLabels
def home(request): def home(request):
@ -32,3 +32,10 @@ def model_form_upload(request):
return render(request, 'core/model_form_upload.html', { return render(request, 'core/model_form_upload.html', {
'form' : form '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)

View File

@ -24,6 +24,8 @@ urlpatterns = [
path('', views.home, name='home'), path('', views.home, name='home'),
path('prototype/form/', views.model_form_upload, name='model_form_upload'), path('prototype/form/', views.model_form_upload, name='model_form_upload'),
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
path('discussions/<int:id>', views.discussions)
] ]
if settings.DEBUG: if settings.DEBUG: