diff --git a/backend/webapp/prototype/filehandler/forms.py b/backend/webapp/prototype/filehandler/forms.py
index 13a4c94..a8d8888 100644
--- a/backend/webapp/prototype/filehandler/forms.py
+++ b/backend/webapp/prototype/filehandler/forms.py
@@ -4,4 +4,4 @@ from prototype.filehandler.models import Document
class DocumentForm(forms.ModelForm):
class Meta:
model = Document
- fields = ('description', 'document', )
+ fields = ('description', 'file', )
diff --git a/backend/webapp/prototype/filehandler/functions.py b/backend/webapp/prototype/filehandler/functions.py
new file mode 100644
index 0000000..56301b7
--- /dev/null
+++ b/backend/webapp/prototype/filehandler/functions.py
@@ -0,0 +1,10 @@
+from xml.etree.ElementTree import parse
+
+def isValidXML(file):
+ try:
+ parse(file)
+ except:
+ return(False)
+ return(True)
+
+
diff --git a/backend/webapp/prototype/filehandler/models.py b/backend/webapp/prototype/filehandler/models.py
index 928e2d0..5ccb64b 100644
--- a/backend/webapp/prototype/filehandler/models.py
+++ b/backend/webapp/prototype/filehandler/models.py
@@ -3,5 +3,5 @@ from django.db import models
class Document(models.Model):
description = models.CharField(max_length=255, blank=True)
- document = models.FileField(upload_to='documents/')
+ file = models.FileField(upload_to='documents/')
uploaded_at = models.DateTimeField(auto_now_add=True)
diff --git a/backend/webapp/prototype/filehandler/templates/core/home.html b/backend/webapp/prototype/filehandler/templates/core/home.html
index 9d273a9..d37bff5 100644
--- a/backend/webapp/prototype/filehandler/templates/core/home.html
+++ b/backend/webapp/prototype/filehandler/templates/core/home.html
@@ -11,7 +11,7 @@
{% for obj in documents %}
-
- {{ obj.document.name }}
+ {{ obj.file.name }}
(Wysłane: {{ obj.uploaded_at }}
{% endfor %}
diff --git a/backend/webapp/prototype/filehandler/views.py b/backend/webapp/prototype/filehandler/views.py
index a099c62..e4e14c0 100644
--- a/backend/webapp/prototype/filehandler/views.py
+++ b/backend/webapp/prototype/filehandler/views.py
@@ -2,12 +2,13 @@ from django.shortcuts import render, redirect
from django.conf import settings
from django.core.files.storage import FileSystemStorage
from django.views.decorators.csrf import csrf_exempt
-from django.http import HttpResponseRedirect
-
+from django.http import JsonResponse, HttpResponse
from prototype.filehandler.models import Document
from prototype.filehandler.forms import DocumentForm
+from prototype.filehandler.functions import isValidXML
+from prototype.filehandler.xmlParser import parseData
def home(request):
documents = Document.objects.all()
@@ -16,10 +17,14 @@ def home(request):
@csrf_exempt
def model_form_upload(request):
if request.method == 'POST':
+ if not isValidXML(request.FILES['file']):
+ return HttpResponse('Niepoprawny format XML', status=406)
+
form = DocumentForm(request.POST, request.FILES)
if form.is_valid():
form.save()
- return HttpResponseRedirect('sucess')
+ data = parseData(request.FILES['file'])
+ return JsonResponse(data, safe=False)
else:
form = DocumentForm()
return render(request, 'core/model_form_upload.html', {
diff --git a/backend/webapp/prototype/filehandler/xmlParser.py b/backend/webapp/prototype/filehandler/xmlParser.py
new file mode 100644
index 0000000..114160e
--- /dev/null
+++ b/backend/webapp/prototype/filehandler/xmlParser.py
@@ -0,0 +1,35 @@
+#!/usr/bin/env python3
+
+import argparse
+from bs4 import BeautifulSoup
+import json
+
+def parseData(file):
+ # arguments
+ parser = argparse.ArgumentParser(description='Process some xml files.')
+ parser.add_argument('filename', help='xml forum file')
+ args = parser.parse_args()
+
+ # write file first
+ with open('temp.xml', 'wb+') as destination:
+ for chunk in file.chunks():
+ destination.write(chunk)
+
+ # make a soup:
+ with open('temp.xml') as forum:
+ soup = BeautifulSoup(forum, "xml")
+
+ # put json together
+ out = {}
+ out['id'] = soup.forum.get('id')
+ out['name'] = soup.forum.find('name').text
+ out['discussions'] = []
+ for d in soup.forum.find_all('discussion'):
+ out['discussions'].append({
+ 'id': d.get('id'),
+ 'title': d.find('name').text,
+ 'posts': [
+ {'id': p.get('id'), 'author': p.userid.text,
+ 'message': p.message.get_text()} for p in d.find_all('post')]
+ })
+ return(out)