Merge branch 'front-upload-module' of git.wmi.amu.edu.pl:s434776/PRI_2020-FE into front-upload-module

This commit is contained in:
Michał Romaszkin 2020-04-23 21:49:20 +02:00
commit e27fef554a
6 changed files with 56 additions and 6 deletions

View File

@ -4,4 +4,4 @@ from prototype.filehandler.models import Document
class DocumentForm(forms.ModelForm):
class Meta:
model = Document
fields = ('description', 'document', )
fields = ('description', 'file', )

View File

@ -0,0 +1,10 @@
from xml.etree.ElementTree import parse
def isValidXML(file):
try:
parse(file)
except:
return(False)
return(True)

View File

@ -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)

View File

@ -11,7 +11,7 @@
<ul>
{% for obj in documents %}
<li>
<a href="{{ obj.document.url }}">{{ obj.document.name }}</a>
<a href="{{ obj.file.url }}">{{ obj.file.name }}</a>
<small>(Wysłane: {{ obj.uploaded_at }}</small>
</li>
{% endfor %}

View File

@ -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', {

View File

@ -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)