optimize file path
This commit is contained in:
@@ -1,82 +1,83 @@
|
||||
import io
|
||||
from pathlib import Path
|
||||
|
||||
from django.core.files import File
|
||||
from django.utils import timezone
|
||||
from pypdf import PdfReader, PdfWriter
|
||||
from pypdf.constants import FieldDictionaryAttributes as FA # noqa: N814
|
||||
|
||||
from .models import Bill, Wiref
|
||||
|
||||
|
||||
def generate_pdf(wiref):
|
||||
if wiref is None or wiref.status != Wiref.Status.OPENED:
|
||||
return False
|
||||
|
||||
bills = Bill.objects.filter(wiref=wiref).order_by("date")
|
||||
|
||||
# Get data for pdf
|
||||
data = {}
|
||||
data_invoice = {} # Own dict for fixing text to multiline
|
||||
for count, elem in enumerate(bills):
|
||||
data.update(
|
||||
{
|
||||
f"DatumRow{count + 1}": str(elem.date.strftime("%d.%m.%Y")),
|
||||
f"VerwendungszweckRow{count + 1}": elem.purpose,
|
||||
# Replace decimal separator from '.' to ','
|
||||
f"BetragRow{count + 1}": str(elem.amount).replace(".", ","),
|
||||
},
|
||||
)
|
||||
data_invoice.update(
|
||||
{
|
||||
f"RechnungsaustellerinRow{count + 1}": elem.invoice,
|
||||
},
|
||||
)
|
||||
|
||||
# Get budget year
|
||||
today = timezone.now().date()
|
||||
if today.month < 7:
|
||||
budget_year = f"{today.year - 1}-{today.year}"
|
||||
else:
|
||||
budget_year = f"{today.year}-{today.year + 1}"
|
||||
|
||||
# Get total of all bills of wiref form
|
||||
total = 0
|
||||
for elem in bills:
|
||||
total += elem.amount
|
||||
|
||||
data.update(
|
||||
{
|
||||
"Rechnungsnummer": str(wiref.wiref_id),
|
||||
"Budgetjahr": budget_year,
|
||||
# Replace decimal separator from '.' to ','
|
||||
"Summe": str(total).replace(".", ","),
|
||||
},
|
||||
)
|
||||
|
||||
# Write data in pdf
|
||||
pdf_path = Path(Path(__file__).parent) / "static/wiref/Vorlage.pdf"
|
||||
reader = PdfReader(pdf_path)
|
||||
writer = PdfWriter()
|
||||
writer.append(reader)
|
||||
|
||||
writer.update_page_form_field_values(
|
||||
writer.pages[0],
|
||||
data,
|
||||
)
|
||||
|
||||
# Add invoices and fix text to multiline
|
||||
writer.update_page_form_field_values(
|
||||
writer.pages[0],
|
||||
data_invoice,
|
||||
flags=FA.FfBits.Multiline,
|
||||
)
|
||||
|
||||
with io.BytesIO() as bytes_stream:
|
||||
writer.write(bytes_stream)
|
||||
|
||||
# Save pdf in wiref
|
||||
wiref_name = f"Abrechnungsformular-{wiref.wiref_id}.pdf"
|
||||
wiref.file_field.save(wiref_name, File(bytes_stream, wiref_name))
|
||||
|
||||
return True
|
||||
import io
|
||||
|
||||
from django.contrib.staticfiles import finders
|
||||
from django.core.files import File
|
||||
from django.utils import timezone
|
||||
from pypdf import PdfReader, PdfWriter
|
||||
from pypdf.constants import FieldDictionaryAttributes as FA # noqa: N814
|
||||
|
||||
from .models import Bill, Wiref
|
||||
|
||||
|
||||
def generate_pdf(wiref):
|
||||
if wiref is None or wiref.status != Wiref.Status.OPENED:
|
||||
return False
|
||||
|
||||
bills = Bill.objects.filter(wiref=wiref).order_by("date")
|
||||
|
||||
# Get data for pdf
|
||||
data = {}
|
||||
data_invoice = {} # Own dict for fixing text to multiline
|
||||
for count, elem in enumerate(bills):
|
||||
data.update(
|
||||
{
|
||||
f"DatumRow{count + 1}": str(elem.date.strftime("%d.%m.%Y")),
|
||||
f"VerwendungszweckRow{count + 1}": elem.purpose,
|
||||
# Replace decimal separator from '.' to ','
|
||||
f"BetragRow{count + 1}": str(elem.amount).replace(".", ","),
|
||||
},
|
||||
)
|
||||
data_invoice.update(
|
||||
{
|
||||
f"RechnungsaustellerinRow{count + 1}": elem.invoice,
|
||||
},
|
||||
)
|
||||
|
||||
# Get budget year
|
||||
today = timezone.now().date()
|
||||
if today.month < 7:
|
||||
budget_year = f"{today.year - 1}-{today.year}"
|
||||
else:
|
||||
budget_year = f"{today.year}-{today.year + 1}"
|
||||
|
||||
# Get total of all bills of wiref form
|
||||
total = 0
|
||||
for elem in bills:
|
||||
total += elem.amount
|
||||
|
||||
data.update(
|
||||
{
|
||||
"Rechnungsnummer": str(wiref.wiref_id),
|
||||
"Budgetjahr": budget_year,
|
||||
# Replace decimal separator from '.' to ','
|
||||
"Summe": str(total).replace(".", ","),
|
||||
},
|
||||
)
|
||||
|
||||
# Write data in pdf
|
||||
pdf_path_str = finders.find("wiref/Vorlage.pdf")
|
||||
reader = PdfReader(pdf_path_str)
|
||||
writer = PdfWriter()
|
||||
writer.append(reader)
|
||||
|
||||
writer.update_page_form_field_values(
|
||||
writer.pages[0],
|
||||
data,
|
||||
)
|
||||
|
||||
# Add invoices and fix text to multiline
|
||||
writer.update_page_form_field_values(
|
||||
writer.pages[0],
|
||||
data_invoice,
|
||||
flags=FA.FfBits.Multiline,
|
||||
)
|
||||
|
||||
with io.BytesIO() as bytes_stream:
|
||||
writer.write(bytes_stream)
|
||||
bytes_stream.seek(0)
|
||||
|
||||
# Save pdf in wiref
|
||||
wiref_name = f"Abrechnungsformular-{wiref.wiref_id}.pdf"
|
||||
wiref.file_field.save(wiref_name, File(bytes_stream, wiref_name))
|
||||
|
||||
return True
|
||||
|
||||
Reference in New Issue
Block a user