Integrated parser with back-end

This commit is contained in:
Marcin Armacki 2020-04-23 21:13:53 +02:00
parent 626c7799cf
commit 4c5ce996c9
2 changed files with 37 additions and 1 deletions

View File

@ -8,6 +8,7 @@ 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()
@ -22,7 +23,7 @@ def model_form_upload(request):
form = DocumentForm(request.POST, request.FILES)
if form.is_valid():
form.save()
data = [{'name': 'Peter', 'email': 'peter@example.org'}, {'name': 'Julia', 'email': 'julia@example.org'}]
data = parseData(request.FILES['file'])
return JsonResponse(data, safe=False)
else:
form = DocumentForm()

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)