From 0a8335c375554d9118ed412e850efc59cdf3861d Mon Sep 17 00:00:00 2001 From: Hugo LEVY-FALK Date: Thu, 3 Jan 2019 22:34:45 +0100 Subject: [PATCH] Enable template selection for invoices. --- cotisations/tex.py | 5 ++++- preferences/forms.py | 8 ++++++++ preferences/models.py | 12 ++++++++++++ .../preferences/display_preferences.html | 19 +++++++++++++++++++ preferences/urls.py | 5 +++++ preferences/views.py | 3 +++ re2o/settings.py | 1 + 7 files changed, 52 insertions(+), 1 deletion(-) diff --git a/cotisations/tex.py b/cotisations/tex.py index 873c9346..b253d12b 100644 --- a/cotisations/tex.py +++ b/cotisations/tex.py @@ -40,6 +40,7 @@ from django.utils.text import slugify from django.utils.translation import ugettext_lazy as _ from re2o.mixins import AclMixin, RevMixin +from preferences.models import CotisationsOption TEMP_PREFIX = getattr(settings, 'TEX_TEMP_PREFIX', 'render_tex-') @@ -73,6 +74,7 @@ def render_invoice(_request, ctx={}): Render an invoice using some available information such as the current date, the user, the articles, the prices, ... """ + options, _ = CotisationsOption.objects.get_or_create() is_estimate = ctx.get('is_estimate', False) filename = '_'.join([ 'cost_estimate' if is_estimate else 'invoice', @@ -82,7 +84,8 @@ def render_invoice(_request, ctx={}): str(ctx.get('DATE', datetime.now()).month), str(ctx.get('DATE', datetime.now()).day), ]) - r = render_tex(_request, 'cotisations/factures.tex', ctx) + templatename = options.invoice_template.template.name.split('/')[-1] + r = render_tex(_request, templatename, ctx) r['Content-Disposition'] = 'attachment; filename="{name}.pdf"'.format( name=filename ) diff --git a/preferences/forms.py b/preferences/forms.py index 3d461ef2..d8e762e1 100644 --- a/preferences/forms.py +++ b/preferences/forms.py @@ -43,6 +43,7 @@ from .models import ( RadiusKey, SwitchManagementCred, RadiusOption, + CotisationsOption ) from topologie.models import Switch @@ -253,6 +254,13 @@ class EditRadiusOptionForm(ModelForm): return cleaned_data +class EditCotisationsOptionForm(ModelForm): + """Edition forms for Cotisations options""" + class Meta: + model = CotisationsOption + fields = '__all__' + + class ServiceForm(ModelForm): """Edition, ajout de services sur la page d'accueil""" class Meta: diff --git a/preferences/models.py b/preferences/models.py index daeb4a05..781107af 100644 --- a/preferences/models.py +++ b/preferences/models.py @@ -687,3 +687,15 @@ class RadiusOption(AclMixin, PreferencesModel): null=True ) + +class CotisationsOption(AclMixin, PreferencesModel): + class Meta: + verbose_name = _("cotisations options") + + invoice_template = models.OneToOneField( + 'cotisations.DocumentTemplate', + verbose_name=_("Template for invoices"), + related_name="invoice_template", + on_delete=models.PROTECT, + ) + diff --git a/preferences/templates/preferences/display_preferences.html b/preferences/templates/preferences/display_preferences.html index 50b2b647..b0fd8dd8 100644 --- a/preferences/templates/preferences/display_preferences.html +++ b/preferences/templates/preferences/display_preferences.html @@ -346,6 +346,25 @@ with this program; if not, write to the Free Software Foundation, Inc., +
+ +
+ + + + + + + + +
{% trans "Invoices' template" %}{{ cotisationsoptions.invoice_template }}
+
+
+
diff --git a/preferences/urls.py b/preferences/urls.py index 30163868..75ce0a8a 100644 --- a/preferences/urls.py +++ b/preferences/urls.py @@ -71,6 +71,11 @@ urlpatterns = [ views.edit_options, name='edit-options' ), + url( + r'^edit_options/(?P
CotisationsOption)$', + views.edit_options, + name='edit-options' + ), url(r'^add_service/$', views.add_service, name='add-service'), url( r'^edit_service/(?P[0-9]+)$', diff --git a/preferences/views.py b/preferences/views.py index 0e86713c..229b64e8 100644 --- a/preferences/views.py +++ b/preferences/views.py @@ -64,6 +64,7 @@ from .models import ( RadiusKey, SwitchManagementCred, RadiusOption, + CotisationsOption, ) from . import models from . import forms @@ -88,6 +89,7 @@ def display_options(request): radiuskey_list = RadiusKey.objects.all() switchmanagementcred_list = SwitchManagementCred.objects.all() radiusoptions, _ = RadiusOption.objects.get_or_create() + cotisationsoptions, _created = CotisationsOption.objects.get_or_create() return form({ 'useroptions': useroptions, 'machineoptions': machineoptions, @@ -102,6 +104,7 @@ def display_options(request): 'radiuskey_list' : radiuskey_list, 'switchmanagementcred_list': switchmanagementcred_list, 'radiusoptions' : radiusoptions, + 'cotisationsoptions': cotisationsoptions, }, 'preferences/display_preferences.html', request) diff --git a/re2o/settings.py b/re2o/settings.py index 0a019d2d..f2557b44 100644 --- a/re2o/settings.py +++ b/re2o/settings.py @@ -120,6 +120,7 @@ TEMPLATES = [ 'DIRS': [ # Use only absolute paths with '/' delimiters even on Windows os.path.join(BASE_DIR, 'templates').replace('\\', '/'), + os.path.join(BASE_DIR, 'media', 'templates').replace('\\', '/'), ], 'APP_DIRS': True, 'OPTIONS': {