diff --git a/apps/treasury/admin.py b/apps/treasury/admin.py index 2d2fbbef..fe9d3725 100644 --- a/apps/treasury/admin.py +++ b/apps/treasury/admin.py @@ -3,7 +3,7 @@ from django.contrib import admin -from treasury.models import Billing, Product +from .models import Billing, Product @admin.register(Billing) diff --git a/apps/treasury/apps.py b/apps/treasury/apps.py index 5f7f6dc9..96bab8a0 100644 --- a/apps/treasury/apps.py +++ b/apps/treasury/apps.py @@ -2,7 +2,6 @@ # SPDX-License-Identifier: GPL-3.0-or-later 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 _ diff --git a/apps/treasury/forms.py b/apps/treasury/forms.py index acf2eb7c..e8697fda 100644 --- a/apps/treasury/forms.py +++ b/apps/treasury/forms.py @@ -20,10 +20,11 @@ ProductFormSet = forms.inlineformset_factory( extra=1, ) + class ProductFormSetHelper(FormHelper): def __init__(self, form=None): super().__init__(form) self.form_tag = False self.form_method = 'POST' self.form_class = 'form-inline' - self.template = 'bootstrap4/table_inline_formset.html' \ No newline at end of file + self.template = 'bootstrap4/table_inline_formset.html' diff --git a/apps/treasury/tables.py b/apps/treasury/tables.py index acbcd606..c651d7f5 100644 --- a/apps/treasury/tables.py +++ b/apps/treasury/tables.py @@ -1,8 +1,8 @@ # Copyright (C) 2018-2020 by BDE ENS Paris-Saclay # SPDX-License-Identifier: GPL-3.0-or-later -from django.utils.translation import gettext_lazy as _ import django_tables2 as tables +from django.utils.translation import gettext_lazy as _ from django_tables2 import A from .models import Billing @@ -11,17 +11,17 @@ from .models import Billing class BillingTable(tables.Table): id = tables.LinkColumn("treasury:billing_update", 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", - verbose_name=_("Billing"), - args=[A("pk")], - accessor="pk", - text="", - attrs={ - 'a': {'class': 'fa fa-file-pdf-o'}, - 'td': {'data-turbolinks': 'false'} - }) + verbose_name=_("Billing"), + args=[A("pk")], + accessor="pk", + text="", + attrs={ + 'a': {'class': 'fa fa-file-pdf-o'}, + 'td': {'data-turbolinks': 'false'} + }) class Meta: attrs = { @@ -29,4 +29,4 @@ class BillingTable(tables.Table): } model = Billing template_name = 'django_tables2/bootstrap4.html' - fields = ('id', 'name', 'subject', 'acquitted', 'billing', ) + fields = ('id', 'name', 'subject', 'acquitted', 'billing',) diff --git a/apps/treasury/views.py b/apps/treasury/views.py index 1fb4bcf0..ed0065c7 100644 --- a/apps/treasury/views.py +++ b/apps/treasury/views.py @@ -15,7 +15,6 @@ from django.urls import reverse_lazy from django.views.generic import CreateView, UpdateView from django.views.generic.base import View from django_tables2 import SingleTableView - from note_kfet.settings.base import BASE_DIR from .forms import BillingForm, ProductFormSet, ProductFormSetHelper @@ -130,30 +129,39 @@ class BillingRenderView(LoginRequiredMixin, View): pass tmp_dir = mkdtemp(prefix=BASE_DIR + "/tmp/") - with open("{}/billing-{:d}.tex".format(tmp_dir, pk), "wb") as f: - f.write(tex.encode("UTF-8")) - del tex + try: + with open("{}/billing-{:d}.tex".format(tmp_dir, pk), "wb") as f: + f.write(tex.encode("UTF-8")) + del tex - error = subprocess.Popen( - ["pdflatex", "billing-{}.tex".format(pk)], - cwd=tmp_dir, - stdin=open(os.devnull, "r"), - stderr=open(os.devnull, "wb"), - stdout=open(os.devnull, "wb") - ).wait() + error = subprocess.Popen( + ["pdflatex", "billing-{}.tex".format(pk)], + cwd=tmp_dir, + stdin=open(os.devnull, "r"), + stderr=open(os.devnull, "wb"), + stdout=open(os.devnull, "wb") + ).wait() - error = subprocess.Popen( - ["pdflatex", "billing-{}.tex".format(pk)], - cwd=tmp_dir, - stdin=open(os.devnull, "r"), - stderr=open(os.devnull, "wb"), - stdout=open(os.devnull, "wb") - ).wait() + if error: + raise IOError("An error attempted while generating a billing:", error) - pdf = open("{}/billing-{}.pdf".format(tmp_dir, pk), 'rb').read() - shutil.rmtree(tmp_dir) + error = subprocess.Popen( + ["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") - response['Content-Disposition'] = "inline;filename=billing-{:d}.pdf".format(pk) + if error: + 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 diff --git a/tox.ini b/tox.ini index 2217b6bf..decdae9c 100644 --- a/tox.ini +++ b/tox.ini @@ -32,7 +32,7 @@ deps = pep8-naming pyflakes 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] # Ignore too many errors, should be reduced in the future