Added endpoint for listing posts for discussion visualization

This commit is contained in:
Marcin Armacki 2020-06-23 14:49:17 +02:00
parent 16579fd1ff
commit 630e27e7b4
3 changed files with 27 additions and 2 deletions

View File

@ -92,3 +92,20 @@ def listParagraphsFromDiscussion(id):
out['posts'] = posts_ out['posts'] = posts_
return(out) return(out)
def listPostsFromDiscussion(id):
out = {}
posts = Post.objects.filter(discussion_id = id)
posts_array = []
for post in posts:
obj = {}
obj['author'] = post.author
obj['id'] = post.post_id
obj['parent'] = post.parent
message = ""
paragraphs = Paragraph.objects.filter(post_id = post.pk)
for paragraph in paragraphs:
message += paragraph.message + " "
obj['message'] = message.rstrip()
posts_array.append(obj)
out['posts'] = posts_array
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, listParagraphsFromDiscussion, createLabels from prototype.filehandler.functions import addToDatabase, listDiscussionsFromFile, listParagraphsFromDiscussion, createLabels, listPostsFromDiscussion
def home(request): def home(request):
@ -39,3 +39,10 @@ def discussions(request, id):
return JsonResponse(output, safe=False) return JsonResponse(output, safe=False)
else: else:
return HttpResponse('Nieobsługiwana metoda HTTP', status=406) return HttpResponse('Nieobsługiwana metoda HTTP', status=406)
def visualize(request, id):
if request.method == 'GET':
output = listPostsFromDiscussion(id)
return JsonResponse(output, safe=False)
else:
return HttpResponse('Nieobsługiwana metoda HTTP', status=406)

View File

@ -24,7 +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) path('discussions/<int:id>', views.discussions),
path('visualize/<int:id>', views.visualize)
] ]