Added XML check

This commit is contained in:
Marcin Armacki 2020-04-23 20:09:28 +02:00
parent 39432acd3e
commit 626c7799cf
5 changed files with 20 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,12 @@ 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
def home(request):
documents = Document.objects.all()
@ -16,10 +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)
if form.is_valid():
form.save()
return HttpResponseRedirect('sucess')
data = [{'name': 'Peter', 'email': 'peter@example.org'}, {'name': 'Julia', 'email': 'julia@example.org'}]
return JsonResponse(data, safe=False)
else:
form = DocumentForm()
return render(request, 'core/model_form_upload.html', {