This commit is contained in:
Yohann D'ANELLO 2020-03-21 17:29:39 +01:00
parent b3bc30b72b
commit 2d0ba4750c
6 changed files with 45 additions and 37 deletions

View File

@ -3,7 +3,7 @@
from django.contrib import admin from django.contrib import admin
from treasury.models import Billing, Product from .models import Billing, Product
@admin.register(Billing) @admin.register(Billing)

View File

@ -2,7 +2,6 @@
# SPDX-License-Identifier: GPL-3.0-or-later # SPDX-License-Identifier: GPL-3.0-or-later
from django.apps import AppConfig from django.apps import AppConfig
from django.db.models.signals import pre_save, post_save, post_delete
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _

View File

@ -20,6 +20,7 @@ ProductFormSet = forms.inlineformset_factory(
extra=1, extra=1,
) )
class ProductFormSetHelper(FormHelper): class ProductFormSetHelper(FormHelper):
def __init__(self, form=None): def __init__(self, form=None):
super().__init__(form) super().__init__(form)

View File

@ -1,8 +1,8 @@
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay # Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later # SPDX-License-Identifier: GPL-3.0-or-later
from django.utils.translation import gettext_lazy as _
import django_tables2 as tables import django_tables2 as tables
from django.utils.translation import gettext_lazy as _
from django_tables2 import A from django_tables2 import A
from .models import Billing from .models import Billing
@ -11,17 +11,17 @@ from .models import Billing
class BillingTable(tables.Table): class BillingTable(tables.Table):
id = tables.LinkColumn("treasury:billing_update", id = tables.LinkColumn("treasury:billing_update",
args=[A("pk")], args=[A("pk")],
text=lambda record: _("Billing #{:d}").format(record.id),) text=lambda record: _("Billing #{:d}").format(record.id), )
billing = tables.LinkColumn("treasury:billing_render", billing = tables.LinkColumn("treasury:billing_render",
verbose_name=_("Billing"), verbose_name=_("Billing"),
args=[A("pk")], args=[A("pk")],
accessor="pk", accessor="pk",
text="", text="",
attrs={ attrs={
'a': {'class': 'fa fa-file-pdf-o'}, 'a': {'class': 'fa fa-file-pdf-o'},
'td': {'data-turbolinks': 'false'} 'td': {'data-turbolinks': 'false'}
}) })
class Meta: class Meta:
attrs = { attrs = {
@ -29,4 +29,4 @@ class BillingTable(tables.Table):
} }
model = Billing model = Billing
template_name = 'django_tables2/bootstrap4.html' template_name = 'django_tables2/bootstrap4.html'
fields = ('id', 'name', 'subject', 'acquitted', 'billing', ) fields = ('id', 'name', 'subject', 'acquitted', 'billing',)

View File

@ -15,7 +15,6 @@ from django.urls import reverse_lazy
from django.views.generic import CreateView, UpdateView from django.views.generic import CreateView, UpdateView
from django.views.generic.base import View from django.views.generic.base import View
from django_tables2 import SingleTableView from django_tables2 import SingleTableView
from note_kfet.settings.base import BASE_DIR from note_kfet.settings.base import BASE_DIR
from .forms import BillingForm, ProductFormSet, ProductFormSetHelper from .forms import BillingForm, ProductFormSet, ProductFormSetHelper
@ -130,30 +129,39 @@ class BillingRenderView(LoginRequiredMixin, View):
pass pass
tmp_dir = mkdtemp(prefix=BASE_DIR + "/tmp/") tmp_dir = mkdtemp(prefix=BASE_DIR + "/tmp/")
with open("{}/billing-{:d}.tex".format(tmp_dir, pk), "wb") as f: try:
f.write(tex.encode("UTF-8")) with open("{}/billing-{:d}.tex".format(tmp_dir, pk), "wb") as f:
del tex f.write(tex.encode("UTF-8"))
del tex
error = subprocess.Popen( error = subprocess.Popen(
["pdflatex", "billing-{}.tex".format(pk)], ["pdflatex", "billing-{}.tex".format(pk)],
cwd=tmp_dir, cwd=tmp_dir,
stdin=open(os.devnull, "r"), stdin=open(os.devnull, "r"),
stderr=open(os.devnull, "wb"), stderr=open(os.devnull, "wb"),
stdout=open(os.devnull, "wb") stdout=open(os.devnull, "wb")
).wait() ).wait()
error = subprocess.Popen( if error:
["pdflatex", "billing-{}.tex".format(pk)], raise IOError("An error attempted while generating a billing:", error)
cwd=tmp_dir,
stdin=open(os.devnull, "r"),
stderr=open(os.devnull, "wb"),
stdout=open(os.devnull, "wb")
).wait()
pdf = open("{}/billing-{}.pdf".format(tmp_dir, pk), 'rb').read() error = subprocess.Popen(
shutil.rmtree(tmp_dir) ["pdflatex", "billing-{}.tex".format(pk)],
cwd=tmp_dir,
stdin=open(os.devnull, "r"),
stderr=open(os.devnull, "wb"),
stdout=open(os.devnull, "wb")
).wait()
response = HttpResponse(pdf, content_type="application/pdf") if error:
response['Content-Disposition'] = "inline;filename=billing-{:d}.pdf".format(pk) raise IOError("An error attempted while generating a billing:", error)
pdf = open("{}/billing-{}.pdf".format(tmp_dir, pk), 'rb').read()
response = HttpResponse(pdf, content_type="application/pdf")
response['Content-Disposition'] = "inline;filename=billing-{:d}.pdf".format(pk)
except IOError as e:
raise e
finally:
shutil.rmtree(tmp_dir)
return response return response

View File

@ -32,7 +32,7 @@ deps =
pep8-naming pep8-naming
pyflakes pyflakes
commands = commands =
flake8 apps/activity apps/api apps/logs apps/member apps/note flake8 apps/activity apps/api apps/logs apps/member apps/note apps/treasury
[flake8] [flake8]
# Ignore too many errors, should be reduced in the future # Ignore too many errors, should be reduced in the future