From 16e7c414a4aa024453e376c5ab9c55f99521b3da Mon Sep 17 00:00:00 2001 From: Marcin Armacki Date: Sat, 13 Jun 2020 21:52:05 +0200 Subject: [PATCH] Database schema&models, retrieve discussions --- .../webapp/prototype/filehandler/functions.py | 38 +++++++++++++++---- .../webapp/prototype/filehandler/models.py | 22 +++++++++++ backend/webapp/prototype/filehandler/views.py | 19 +++++----- 3 files changed, 62 insertions(+), 17 deletions(-) diff --git a/backend/webapp/prototype/filehandler/functions.py b/backend/webapp/prototype/filehandler/functions.py index 56301b7..6d39e2c 100644 --- a/backend/webapp/prototype/filehandler/functions.py +++ b/backend/webapp/prototype/filehandler/functions.py @@ -1,10 +1,32 @@ -from xml.etree.ElementTree import parse - -def isValidXML(file): - try: - parse(file) - except: - return(False) - return(True) +from prototype.filehandler.models import Forum, Discussion, Post, Paragraph +def addToDatabase(data, file_id): + out = [] + forum = Forum(forum_id = data['id'], name = data['name'], document_id = file_id) + forum.save() + for discussion_ in data['discussions']: + discussion = Discussion(discussion_id = discussion_['id'], title = discussion_['title'], first_post = discussion_['first_post'], forum_id = forum.pk) + discussion.save() + for post_ in discussion_['posts']: + post = Post(post_id = post_['id'], parent = post_['parent'], author = post_['author'], discussion_id = discussion.pk) + post.save() + for paragraph_ in post_['message']: + paragraph = Paragraph(message = paragraph_, label = '', post_id = post.pk) + paragraph.save() + out.append(paragraph.pk) + return(out) +def listDiscussionsFromFile(id): + out = {} + forum = Forum.objects.get(document_id = id) + out['id'] = id + out['name'] = forum.name + discussions = Discussion.objects.filter(forum_id = forum.pk) + discussions_ = [] + for elem in discussions: + obj = {} + obj['id'] = elem.pk + obj['title'] = elem.title + discussions_.append(obj) + out['discussions'] = discussions_ + return(out) diff --git a/backend/webapp/prototype/filehandler/models.py b/backend/webapp/prototype/filehandler/models.py index 84cc772..a7dc1a0 100644 --- a/backend/webapp/prototype/filehandler/models.py +++ b/backend/webapp/prototype/filehandler/models.py @@ -4,3 +4,25 @@ from django.db import models class Document(models.Model): file = models.FileField(upload_to='documents/') uploaded_at = models.DateTimeField(auto_now_add=True) + +class Forum(models.Model): + forum_id = models.IntegerField() + name = models.CharField(max_length=255) + document = models.ForeignKey(Document, on_delete=models.CASCADE) + +class Discussion(models.Model): + discussion_id = models.IntegerField() + title = models.CharField(max_length=255) + first_post = models.IntegerField() + forum = models.ForeignKey(Forum, on_delete=models.CASCADE) + +class Post(models.Model): + post_id = models.IntegerField() + parent = models.IntegerField() + author = models.IntegerField() + discussion = models.ForeignKey(Discussion, on_delete=models.CASCADE) + +class Paragraph(models.Model): + message = models.TextField() + label = models.CharField(max_length=255) + post = models.ForeignKey(Post, on_delete=models.CASCADE) diff --git a/backend/webapp/prototype/filehandler/views.py b/backend/webapp/prototype/filehandler/views.py index 42942dc..05a01f4 100644 --- a/backend/webapp/prototype/filehandler/views.py +++ b/backend/webapp/prototype/filehandler/views.py @@ -4,10 +4,11 @@ from django.core.files.storage import FileSystemStorage from django.views.decorators.csrf import csrf_exempt from django.http import JsonResponse, HttpResponse -from prototype.filehandler.models import Document +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 + def home(request): documents = Document.objects.all() @@ -18,13 +19,13 @@ def model_form_upload(request): if request.method == 'POST': form = DocumentForm(request.POST, request.FILES) if form.is_valid(): - try: - data = parseData(request.FILES['file']) - print(data) - form.save() - return JsonResponse(data, safe=False) - except: - return HttpResponse('Niepoprawny format XML', status=406) + data = parseData(request.FILES['file']) + file_id = (form.save()).pk + addToDatabase(data, file_id) + output = listDiscussionsFromFile(file_id) + return JsonResponse(output, safe=False) + else: + return HttpResponse('Niepoprawny format XML', status=406) else: form = DocumentForm() return render(request, 'core/model_form_upload.html', {