Database schema&models, retrieve discussions

This commit is contained in:
Marcin Armacki 2020-06-13 21:52:05 +02:00
parent f3860b9ac5
commit 16e7c414a4
3 changed files with 62 additions and 17 deletions

View File

@ -1,10 +1,32 @@
from xml.etree.ElementTree import parse from prototype.filehandler.models import Forum, Discussion, Post, Paragraph
def isValidXML(file):
try:
parse(file)
except:
return(False)
return(True)
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)

View File

@ -4,3 +4,25 @@ from django.db import models
class Document(models.Model): class Document(models.Model):
file = models.FileField(upload_to='documents/') file = models.FileField(upload_to='documents/')
uploaded_at = models.DateTimeField(auto_now_add=True) 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)

View File

@ -4,10 +4,11 @@ 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
from prototype.filehandler.models import Document 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
def home(request): def home(request):
documents = Document.objects.all() documents = Document.objects.all()
@ -18,12 +19,12 @@ def model_form_upload(request):
if request.method == 'POST': if request.method == 'POST':
form = DocumentForm(request.POST, request.FILES) form = DocumentForm(request.POST, request.FILES)
if form.is_valid(): if form.is_valid():
try:
data = parseData(request.FILES['file']) data = parseData(request.FILES['file'])
print(data) file_id = (form.save()).pk
form.save() addToDatabase(data, file_id)
return JsonResponse(data, safe=False) output = listDiscussionsFromFile(file_id)
except: return JsonResponse(output, safe=False)
else:
return HttpResponse('Niepoprawny format XML', status=406) return HttpResponse('Niepoprawny format XML', status=406)
else: else:
form = DocumentForm() form = DocumentForm()