2020-02-12 12:10:41 +00:00
|
|
|
import copy
|
2018-08-30 10:18:56 +00:00
|
|
|
|
2020-02-12 12:10:41 +00:00
|
|
|
import magic
|
2018-08-30 10:18:56 +00:00
|
|
|
from django import forms
|
2020-02-12 12:10:41 +00:00
|
|
|
|
2018-11-20 16:12:32 +00:00
|
|
|
from .models import Expense, Revenue
|
2018-08-30 10:18:56 +00:00
|
|
|
|
|
|
|
|
2018-11-20 16:12:32 +00:00
|
|
|
class CleanInvoiceForm(forms.ModelForm):
|
2018-08-30 10:18:56 +00:00
|
|
|
"""
|
|
|
|
We have to define this form explicitly because we want our ImageField to accept PDF files as well as images,
|
|
|
|
and we cannot change the clean_* methods with an autogenerated form from inside views.py
|
|
|
|
"""
|
2019-06-16 12:32:24 +00:00
|
|
|
|
2018-08-30 10:18:56 +00:00
|
|
|
invoice = forms.FileField()
|
|
|
|
|
|
|
|
def clean_invoice(self):
|
|
|
|
# get the uploaded file from cleaned_data
|
2019-06-16 12:32:24 +00:00
|
|
|
uploaded_file = self.cleaned_data["invoice"]
|
2018-08-30 10:18:56 +00:00
|
|
|
# is this a valid image?
|
|
|
|
try:
|
|
|
|
# create an ImageField instance
|
|
|
|
im = forms.ImageField()
|
|
|
|
# now check if the file is a valid image
|
|
|
|
im.to_python(uploaded_file)
|
|
|
|
except forms.ValidationError:
|
|
|
|
# file is not a valid image, so check if it's a pdf
|
|
|
|
# do a deep copy so we dont mess with the file object we might be passing on
|
|
|
|
testfile = copy.deepcopy(uploaded_file)
|
|
|
|
# read the uploaded file into memory (the webserver limits uploads to a reasonable max size so this should be safe)
|
|
|
|
mimetype = magic.from_buffer(testfile.open().read(), mime=True)
|
|
|
|
if mimetype != "application/pdf":
|
|
|
|
raise forms.ValidationError("Only images and PDF files allowed")
|
|
|
|
|
|
|
|
# this is either a valid image, or has mimetype application/pdf, all good
|
|
|
|
return uploaded_file
|
|
|
|
|
2018-11-20 16:12:32 +00:00
|
|
|
|
|
|
|
class ExpenseCreateForm(CleanInvoiceForm):
|
|
|
|
class Meta:
|
|
|
|
model = Expense
|
2019-06-16 12:32:24 +00:00
|
|
|
fields = [
|
|
|
|
"description",
|
|
|
|
"amount",
|
|
|
|
"invoice_date",
|
|
|
|
"invoice",
|
|
|
|
"paid_by_bornhack",
|
|
|
|
"responsible_team",
|
|
|
|
]
|
2018-11-20 16:12:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ExpenseUpdateForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = Expense
|
2019-06-16 12:32:24 +00:00
|
|
|
fields = [
|
|
|
|
"description",
|
|
|
|
"amount",
|
|
|
|
"invoice_date",
|
|
|
|
"paid_by_bornhack",
|
|
|
|
"responsible_team",
|
|
|
|
]
|
2018-11-20 16:12:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
class RevenueCreateForm(CleanInvoiceForm):
|
|
|
|
class Meta:
|
|
|
|
model = Revenue
|
2019-06-16 12:32:24 +00:00
|
|
|
fields = [
|
|
|
|
"description",
|
|
|
|
"amount",
|
|
|
|
"invoice_date",
|
|
|
|
"invoice",
|
|
|
|
"responsible_team",
|
|
|
|
]
|
2018-11-20 16:12:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
class RevenueUpdateForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = Revenue
|
2019-06-16 12:32:24 +00:00
|
|
|
fields = ["description", "amount", "invoice_date", "responsible_team"]
|