Split address from file upload so multiple files can be associated with an address

master
Tait Hoyem 4 years ago
parent 3e2a991917
commit 07dfe6a794

@ -0,0 +1,32 @@
# Generated by Django 3.1.2 on 2020-10-24 21:23
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('download', '0005_auto_20201024_2028'),
]
operations = [
migrations.CreateModel(
name='Address',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('address', models.CharField(max_length=128)),
('city', models.CharField(max_length=32)),
],
),
migrations.RemoveField(
model_name='pdf',
name='address_string',
),
migrations.AddField(
model_name='pdf',
name='address',
field=models.ForeignKey(default=None, on_delete=django.db.models.deletion.CASCADE, related_name='uploads', to='download.address'),
preserve_default=False,
),
]

@ -0,0 +1,20 @@
# Generated by Django 3.1.2 on 2020-10-24 21:34
from django.db import migrations, models
import django.utils.timezone
class Migration(migrations.Migration):
dependencies = [
('download', '0006_auto_20201024_2123'),
]
operations = [
migrations.AddField(
model_name='pdf',
name='upload_date',
field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
preserve_default=False,
),
]

@ -3,8 +3,14 @@ from core.models import CalgaryAddress
from .validators import RestrictedFileField from .validators import RestrictedFileField
# Create your models here. # Create your models here.
class Address(models.Model):
address = models.CharField(max_length=128)
# TODO: add validation columns in relation to city
city = models.CharField(max_length=32)
class PDF(models.Model): class PDF(models.Model):
path = models.CharField(max_length=64) path = models.CharField(max_length=64)
code = models.CharField(max_length=8) code = models.CharField(max_length=8)
address_string = models.CharField(max_length=128) upload_file = RestrictedFileField(max_upload_size=1024*1024*50, content_types=['pdf', 'application/pdf'], upload_to='uploads/%Y/%m/%d/')
upload_file = RestrictedFileField(max_upload_size=1024*1024*1024*50, content_types=['pdf', 'application/pdf'], upload_to='uploads/%Y/%m/%d/') address = models.ForeignKey(Address, on_delete=models.CASCADE, related_name='uploads')
upload_date = models.DateTimeField(auto_now_add=True, editable=False, null=False, blank=False)

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

@ -1,23 +1,24 @@
from django.shortcuts import render, HttpResponse from django.shortcuts import render, HttpResponse
from django.http import FileResponse from django.http import FileResponse
from .models import PDF from .models import Address, PDF
from .forms import CodeForm from .forms import CodeForm
# Create your views here. # Create your views here.
def starter(request): def starter(request):
print(list(PDF.objects.all()))
return render(request, 'download/download-page.html', { return render(request, 'download/download-page.html', {
'things': list(PDF.objects.all()) 'things': list(Address.objects.all())
}) })
def download(request, pdfid): def download(request, pdfid):
if request.method == 'POST': if request.method == 'POST':
form = CodeForm(request.POST) form = CodeForm(request.POST)
if form.is_valid(): if form.is_valid():
pdfs = PDF.objects.filter(id=pdfid) addr = Address.objects.filter(id=pdfid)
if len(pdfs) == 0: if len(addr) == 0:
return render(request, 'common/not-found.html') return render(request, 'common/not-found.html')
pdf = pdfs.filter(code=form.cleaned_data['code']) addr = addr[0]
# TODO: If same address + different code, the old file is still visible if the old code is still known
pdf = PDF.objects.filter(address=addr, code=form.cleaned_data['code']).order_by('upload_date').reverse()
if len(pdf) == 0: if len(pdf) == 0:
return render(request, 'common/password-incorrect.html') return render(request, 'common/password-incorrect.html')
pdf = pdf[0] pdf = pdf[0]

@ -1,6 +1,6 @@
from sodapy import Socrata from sodapy import Socrata
from core.models import CalgaryAddress from core.models import CalgaryAddress
from download.models import PDF from download.models import PDF, Address
from django.shortcuts import render, HttpResponse from django.shortcuts import render, HttpResponse
import json import json
from .forms import PDFForm from .forms import PDFForm
@ -43,22 +43,33 @@ def upload(request):
print(pdf_file) print(pdf_file)
filename = pdf_file.name filename = pdf_file.name
code = filename.split(".")[0].split(" ")[-1] code = filename.split(".")[0].split(" ")[-1]
city = filename.split(".")[0].split(" ")[-2]
address = ' '.join(filename.split(" ")[:-1]) address = ' '.join(filename.split(" ")[:-1])
name = 'quote_'+code+'.pdf' name = 'quote_'+code+'.pdf'
possible_match = PDF.objects.filter(address_string=address) possible_match = Address.objects.filter(address=address)
# if address already entered # if address already entered
if len(possible_match) > 0: if len(possible_match) > 0:
# update file pdf = PDF.objects.create(
possible_match[0].upload_file = pdf_file path=name,
possible_match[0].save() code=code,
upload_file=pdf_file,
address=possible_match[0]
)
pdf.save()
else: else:
# Save new address # Save new address
addr = Address.objects.create(
address=address,
city=city
)
# code is currently changable per file
pdf = PDF.objects.create( pdf = PDF.objects.create(
path=name, path=name,
address_string=address,
code=code, code=code,
upload_file=pdf_file upload_file=pdf_file,
address=addr
) )
addr.save()
pdf.save() pdf.save()
return HttpResponse("File(s) saved") # TODO: "x Files Saved" return HttpResponse("File(s) saved") # TODO: "x Files Saved"
elif request.method == "GET": elif request.method == "GET":

Loading…
Cancel
Save