Updated uploaded files handling

This commit is contained in:
Marcin Armacki 2020-05-28 21:32:29 +02:00
parent 01e33bd2df
commit 0d479e23ad
4 changed files with 38 additions and 36 deletions

View File

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

View File

@ -2,6 +2,5 @@ from __future__ import unicode_literals
from django.db import models
class Document(models.Model):
description = models.CharField(max_length=255, blank=True)
file = models.FileField(upload_to='documents/')
uploaded_at = models.DateTimeField(auto_now_add=True)

View File

@ -7,7 +7,6 @@ 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):
@ -17,17 +16,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)
try:
data = parseData(request.FILES['file'])
form = DocumentForm(request.POST, request.FILES)
if form.is_valid():
data = parseData(request.FILES['file'])
form.save()
return JsonResponse(data, safe=False)
except:
return HttpResponse('Niepoprawny format XML', status=406)
if form.is_valid():
form.save()
return JsonResponse(data, safe=False)
else:
form = DocumentForm()
return render(request, 'core/model_form_upload.html', {

View File

@ -3,39 +3,46 @@
import argparse
from bs4 import BeautifulSoup
import json
import tempfile
def parseData(file):
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")
fd = tempfile.NamedTemporaryFile()
f = open(fd.name, "wb+")
for chunk in file.chunks():
f.write(chunk)
f.close()
# 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,
'first_post': d.firstpost.text,
'posts': [
{
try:
# make a soup:
with open(fd.name) 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'):
posts = []
for p in d.find_all('post'):
posts.append({
'id': p.get('id'),
'parent': p.find('parent').text,
'author': p.userid.text,
'message': p.message.get_text()
} for p in d.find_all('post')]
})
return(out)
})
out['discussions'].append({
'id': d.get('id'),
'title': d.find('name').text,
'first_post': d.firstpost.text,
'posts': posts
})
return(out)
finally:
fd.close()