Merge branch 'front-upload-module' of git.wmi.amu.edu.pl:s434776/PRI_2020-FE into front-upload-module
This commit is contained in:
commit
e27fef554a
@ -4,4 +4,4 @@ from prototype.filehandler.models import Document
|
|||||||
class DocumentForm(forms.ModelForm):
|
class DocumentForm(forms.ModelForm):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Document
|
model = Document
|
||||||
fields = ('description', 'document', )
|
fields = ('description', 'file', )
|
||||||
|
10
backend/webapp/prototype/filehandler/functions.py
Normal file
10
backend/webapp/prototype/filehandler/functions.py
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
from xml.etree.ElementTree import parse
|
||||||
|
|
||||||
|
def isValidXML(file):
|
||||||
|
try:
|
||||||
|
parse(file)
|
||||||
|
except:
|
||||||
|
return(False)
|
||||||
|
return(True)
|
||||||
|
|
||||||
|
|
@ -3,5 +3,5 @@ from django.db import models
|
|||||||
|
|
||||||
class Document(models.Model):
|
class Document(models.Model):
|
||||||
description = models.CharField(max_length=255, blank=True)
|
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)
|
uploaded_at = models.DateTimeField(auto_now_add=True)
|
||||||
|
@ -11,7 +11,7 @@
|
|||||||
<ul>
|
<ul>
|
||||||
{% for obj in documents %}
|
{% for obj in documents %}
|
||||||
<li>
|
<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>
|
<small>(Wysłane: {{ obj.uploaded_at }}</small>
|
||||||
</li>
|
</li>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
@ -2,12 +2,13 @@ from django.shortcuts import render, redirect
|
|||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.core.files.storage import FileSystemStorage
|
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 HttpResponseRedirect
|
from django.http import JsonResponse, HttpResponse
|
||||||
|
|
||||||
|
|
||||||
from prototype.filehandler.models import Document
|
from prototype.filehandler.models import Document
|
||||||
from prototype.filehandler.forms import DocumentForm
|
from prototype.filehandler.forms import DocumentForm
|
||||||
|
|
||||||
|
from prototype.filehandler.functions import isValidXML
|
||||||
|
from prototype.filehandler.xmlParser import parseData
|
||||||
|
|
||||||
def home(request):
|
def home(request):
|
||||||
documents = Document.objects.all()
|
documents = Document.objects.all()
|
||||||
@ -16,10 +17,14 @@ def home(request):
|
|||||||
@csrf_exempt
|
@csrf_exempt
|
||||||
def model_form_upload(request):
|
def model_form_upload(request):
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
|
if not isValidXML(request.FILES['file']):
|
||||||
|
return HttpResponse('Niepoprawny format XML', status=406)
|
||||||
|
|
||||||
form = DocumentForm(request.POST, request.FILES)
|
form = DocumentForm(request.POST, request.FILES)
|
||||||
if form.is_valid():
|
if form.is_valid():
|
||||||
form.save()
|
form.save()
|
||||||
return HttpResponseRedirect('sucess')
|
data = parseData(request.FILES['file'])
|
||||||
|
return JsonResponse(data, safe=False)
|
||||||
else:
|
else:
|
||||||
form = DocumentForm()
|
form = DocumentForm()
|
||||||
return render(request, 'core/model_form_upload.html', {
|
return render(request, 'core/model_form_upload.html', {
|
||||||
|
35
backend/webapp/prototype/filehandler/xmlParser.py
Normal file
35
backend/webapp/prototype/filehandler/xmlParser.py
Normal 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)
|
Loading…
Reference in New Issue
Block a user