Allow multiple file upload; remove City of Calgary address validation

master
Tait Hoyem 4 years ago
parent 8d3de02ef6
commit 3e2a991917

@ -0,0 +1,17 @@
# Generated by Django 3.1.2 on 2020-10-24 20:23
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('download', '0003_auto_20201015_1337'),
]
operations = [
migrations.RemoveField(
model_name='pdf',
name='address',
),
]

@ -0,0 +1,18 @@
# Generated by Django 3.1.2 on 2020-10-24 20:28
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('download', '0004_remove_pdf_address'),
]
operations = [
migrations.AlterField(
model_name='pdf',
name='address_string',
field=models.CharField(max_length=128),
),
]

@ -6,6 +6,5 @@ from .validators import RestrictedFileField
class PDF(models.Model):
path = models.CharField(max_length=64)
code = models.CharField(max_length=8)
address_string = models.CharField(max_length=32)
address = models.ForeignKey(CalgaryAddress, related_name='pdfs', on_delete=models.CASCADE)
address_string = models.CharField(max_length=128)
upload_file = RestrictedFileField(max_upload_size=1024*1024*1024*50, content_types=['pdf', 'application/pdf'], upload_to='uploads/%Y/%m/%d/')

@ -2,6 +2,6 @@
{% block body %}
<h1>Download Your Quote</h1>
{% for thing in things %}
<p><a href="./d/{{ thing.id }}">{{ thing.address.address }}</a></p>
<p><a href="./d/{{ thing.id }}">{{ thing.address_string }}</a></p>
{% endfor %}
{% endblock %}

@ -1,19 +1,20 @@
from django.shortcuts import render, HttpResponse
from django.http import FileResponse
from . import models
from .models import PDF
from .forms import CodeForm
# Create your views here.
def starter(request):
print(list(PDF.objects.all()))
return render(request, 'download/download-page.html', {
'things': list(models.PDF.objects.all())
});
'things': list(PDF.objects.all())
})
def download(request, pdfid):
if request.method == 'POST':
form = CodeForm(request.POST)
if form.is_valid():
pdfs = models.PDF.objects.filter(id=pdfid)
pdfs = PDF.objects.filter(id=pdfid)
if len(pdfs) == 0:
return render(request, 'common/not-found.html')
pdf = pdfs.filter(code=form.cleaned_data['code'])

@ -1,7 +1,5 @@
from django import forms
from download.models import PDF
class PDFForm(forms.ModelForm):
class Meta:
model = PDF
fields = ['address_string', 'upload_file']
class PDFForm(forms.Form):
files = forms.FileField(widget=forms.ClearableFileInput(attrs={'multiple': True}))

@ -4,11 +4,7 @@
<h1>Upload</h1>
<form action="" method="POST" enctype="multipart/form-data">
{% csrf_token %}
<label for="{{ form.address_string.id_for_label }}">Address:</label>
{{ form.address_string }}
<ul id="addr_list"></ul>
<label for="{{ form.upload_file.id_for_label }}">File:</label>
{{ form.upload_file }}
<input type="file" multiple name="files">
<input type="submit" value="Submit">
</form>
<script>

@ -37,34 +37,30 @@ def search_api(request, address):
def upload(request):
if request.method == 'POST':
upload_form = PDFForm(request.POST, request.FILES)
print(upload_form.data)
print(request.FILES)
print(upload_form.is_valid())
print(upload_form.cleaned_data)
address = upload_form.cleaned_data['address_string']
code = 'XYZ'
name = 'quote_'+code+'.pdf'
pdf_file = upload_form.cleaned_data['upload_file']
addr = c.get('uwj2-d2wc',
where='address = "{0}"'.format(address),
limit=1)[0]
# Save address
addr = CalgaryAddress.objects.create(
address=addr['address'],
house_alpha=addr['house_alpha'] if 'house_alpha' in addr else '',
street_quad=addr['street_quad'],
street_name=addr['street_name'],
street_type=addr['street_type']
)
pdf = PDF.objects.create(
path=name,
address=addr,
code=code,
upload_file=pdf_file
)
pdf.save()
return HttpResponse("File saved")
files = request.FILES.getlist('files')
if upload_form.is_valid():
for pdf_file in files:
print(pdf_file)
filename = pdf_file.name
code = filename.split(".")[0].split(" ")[-1]
address = ' '.join(filename.split(" ")[:-1])
name = 'quote_'+code+'.pdf'
possible_match = PDF.objects.filter(address_string=address)
# if address already entered
if len(possible_match) > 0:
# update file
possible_match[0].upload_file = pdf_file
possible_match[0].save()
else:
# Save new address
pdf = PDF.objects.create(
path=name,
address_string=address,
code=code,
upload_file=pdf_file
)
pdf.save()
return HttpResponse("File(s) saved") # TODO: "x Files Saved"
elif request.method == "GET":
return render(request, 'upload/upload.html', {
'form': PDFForm()

Loading…
Cancel
Save