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 treasury.models import Billing, Product
from .models import Billing, Product
@admin.register(Billing)

View File

@ -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 _

View File

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

View File

@ -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

View File

@ -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,6 +129,7 @@ class BillingRenderView(LoginRequiredMixin, View):
pass
tmp_dir = mkdtemp(prefix=BASE_DIR + "/tmp/")
try:
with open("{}/billing-{:d}.tex".format(tmp_dir, pk), "wb") as f:
f.write(tex.encode("UTF-8"))
del tex
@ -142,6 +142,9 @@ class BillingRenderView(LoginRequiredMixin, View):
stdout=open(os.devnull, "wb")
).wait()
if error:
raise IOError("An error attempted while generating a billing:", error)
error = subprocess.Popen(
["pdflatex", "billing-{}.tex".format(pk)],
cwd=tmp_dir,
@ -150,10 +153,15 @@ class BillingRenderView(LoginRequiredMixin, View):
stdout=open(os.devnull, "wb")
).wait()
pdf = open("{}/billing-{}.pdf".format(tmp_dir, pk), 'rb').read()
shutil.rmtree(tmp_dir)
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

View File

@ -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