1
0
mirror of https://gitlab.crans.org/bde/nk20 synced 2025-06-20 17:41:55 +02:00

Store invoice source code instead of generate it everytime

This commit is contained in:
Yohann D'ANELLO
2020-08-06 22:30:14 +02:00
parent e23eafd56c
commit 1fb14ea33d
8 changed files with 224 additions and 180 deletions

View File

@ -28,7 +28,7 @@ class InvoiceForm(forms.ModelForm):
class Meta:
model = Invoice
exclude = ('bde', )
exclude = ('bde', 'tex', )
class ProductForm(forms.ModelForm):

View File

@ -6,6 +6,7 @@ from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
from django.db import models
from django.db.models import Q
from django.template.loader import render_to_string
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
from note.models import NoteSpecial, SpecialTransaction, MembershipTransaction
@ -62,6 +63,34 @@ class Invoice(models.Model):
verbose_name=_("Acquitted"),
)
tex = models.TextField(
default="",
verbose_name=_("tex source"),
)
def save(self, *args, **kwargs):
"""
When an invoice is generated, we store the tex source.
The advantage is to never change the template.
Warning: editing this model regenerate the tex source, so be careful.
"""
products = self.products.all()
self.place = "Gif-sur-Yvette"
self.my_name = "BDE ENS Cachan"
self.my_address_street = "4 avenue des Sciences"
self.my_city = "91190 Gif-sur-Yvette"
self.bank_code = 30003
self.desk_code = 3894
self.account_number = 37280662
self.rib_key = 14
self.bic = "SOGEFRPP"
# Fill the template with the information
self.tex = render_to_string("treasury/invoice_sample.tex", dict(obj=self, products=products))
return super().save(*args, **kwargs)
class Meta:
verbose_name = _("invoice")
verbose_name_plural = _("invoices")
@ -75,6 +104,8 @@ class Product(models.Model):
invoice = models.ForeignKey(
Invoice,
on_delete=models.PROTECT,
related_name="products",
verbose_name=_("invoice"),
)
designation = models.CharField(

View File

@ -13,6 +13,8 @@ def do_latex_escape(value):
.replace("_", "\\_")
.replace("{", "\\{")
.replace("}", "\\}")
.replace("\n", "\\\\")
.replace("\r", "")
)

View File

@ -139,23 +139,7 @@ class InvoiceRenderView(LoginRequiredMixin, View):
def get(self, request, **kwargs):
pk = kwargs["pk"]
invoice = Invoice.objects.filter(PermissionBackend.filter_queryset(request.user, Invoice, "view")).get(pk=pk)
products = Product.objects.filter(invoice=invoice).all()
invoice.place = "Gif-sur-Yvette"
invoice.my_name = "BDE ENS Cachan"
invoice.my_address_street = "4 avenue des Sciences"
invoice.my_city = "91190 Gif-sur-Yvette"
invoice.bank_code = 30003
invoice.desk_code = 3894
invoice.account_number = 37280662
invoice.rib_key = 14
invoice.bic = "SOGEFRPP"
# Replace line breaks with the LaTeX equivalent
invoice.description = invoice.description.replace("\r", "").replace("\n", "\\\\ ")
invoice.address = invoice.address.replace("\r", "").replace("\n", "\\\\ ")
# Fill the template with the information
tex = render_to_string("treasury/invoice_sample.tex", dict(obj=invoice, products=products))
tex = invoice.tex
try:
os.mkdir(BASE_DIR + "/tmp")