From f43b985966cd1e188e4b4e572cc703312c177859 Mon Sep 17 00:00:00 2001 From: Hugo LEVY-FALK Date: Sun, 20 Jan 2019 18:53:25 +0100 Subject: [PATCH] Meilleure gestion des default pour les templates invoice et voucher --- install_re2o.sh | 2 +- .../migrations/0059_cotisationsoption.py | 40 ++----------------- preferences/models.py | 19 +++++++++ 3 files changed, 24 insertions(+), 37 deletions(-) diff --git a/install_re2o.sh b/install_re2o.sh index d64cdb50..ff09e6d8 100755 --- a/install_re2o.sh +++ b/install_re2o.sh @@ -797,8 +797,8 @@ main_function() { update ) install_requirements - update_django copy_templates_files + update_django ;; copy-templates-files ) diff --git a/preferences/migrations/0059_cotisationsoption.py b/preferences/migrations/0059_cotisationsoption.py index 1d36bf06..9fe53203 100644 --- a/preferences/migrations/0059_cotisationsoption.py +++ b/preferences/migrations/0059_cotisationsoption.py @@ -1,48 +1,16 @@ # -*- coding: utf-8 -*- # Generated by Django 1.10.7 on 2019-01-03 19:56 from __future__ import unicode_literals -import os from django.db import migrations, models import django.db.models.deletion -from django.core.files import File -from django.conf import settings import re2o.mixins +import preferences.models def initialize_invoice_template(apps, schema_editor): CotisationsOption = apps.get_model('preferences', 'CotisationsOption') - DocumentTemplate = apps.get_model('cotisations', 'DocumentTemplate') - invoice_path = os.path.join( - settings.BASE_DIR, - "cotisations", - "templates", - "cotisations", - "factures.tex" - ) - voucher_path = os.path.join( - settings.BASE_DIR, - "cotisations", - "templates", - "cotisations", - "voucher.tex" - ) - with open(invoice_path) as f: - tpl_invoice, _ = DocumentTemplate.objects.get_or_create( - name="Re2o default invoice", - ) - tpl_invoice.template.save('default_invoice.tex', File(f)) - tpl_invoice.save() - with open(voucher_path) as f: - tpl_voucher, _ = DocumentTemplate.objects.get_or_create( - name="Re2o default voucher", - ) - tpl_voucher.template.save('default_voucher.tex', File(f)) - tpl_voucher.save() - CotisationsOption.objects.create( - invoice_template=tpl_invoice, - voucher_template=tpl_voucher, - ) + CotisationsOption.objects.get_or_create() class Migration(migrations.Migration): @@ -57,8 +25,8 @@ class Migration(migrations.Migration): name='CotisationsOption', fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('invoice_template', models.OneToOneField(on_delete=django.db.models.deletion.PROTECT, related_name='invoice_template', to='cotisations.DocumentTemplate', verbose_name='Template for invoices')), - ('voucher_template', models.OneToOneField(on_delete=django.db.models.deletion.PROTECT, related_name='voucher_template', to='cotisations.DocumentTemplate', verbose_name='Template for subscription voucher')), + ('invoice_template', models.OneToOneField(default=preferences.models.default_invoice,on_delete=django.db.models.deletion.PROTECT, related_name='invoice_template', to='cotisations.DocumentTemplate', verbose_name='Template for invoices')), + ('voucher_template', models.OneToOneField(default=preferences.models.default_voucher, on_delete=django.db.models.deletion.PROTECT, related_name='voucher_template', to='cotisations.DocumentTemplate', verbose_name='Template for subscription voucher')), ], options={ 'verbose_name': 'cotisations options', diff --git a/preferences/models.py b/preferences/models.py index f03a6719..71f1ffa0 100644 --- a/preferences/models.py +++ b/preferences/models.py @@ -35,6 +35,7 @@ from django.forms import ValidationError from django.utils.translation import ugettext_lazy as _ import machines.models +import cotisations.models from re2o.mixins import AclMixin from re2o.aes_field import AESEncryptedField @@ -694,6 +695,22 @@ class RadiusOption(AclMixin, PreferencesModel): ) +def default_invoice(): + tpl, _ = cotisations.models.DocumentTemplate.objects.get_or_create( + name="Re2o default invoice", + template="templates/default_invoice.tex" + ) + return tpl.id + + +def default_voucher(): + tpl, _ = cotisations.models.DocumentTemplate.objects.get_or_create( + name="Re2o default voucher", + template="templates/default_voucher.tex" + ) + return tpl.id + + class CotisationsOption(AclMixin, PreferencesModel): class Meta: verbose_name = _("cotisations options") @@ -703,10 +720,12 @@ class CotisationsOption(AclMixin, PreferencesModel): verbose_name=_("Template for invoices"), related_name="invoice_template", on_delete=models.PROTECT, + default=default_invoice, ) voucher_template = models.OneToOneField( 'cotisations.DocumentTemplate', verbose_name=_("Template for subscription voucher"), related_name="voucher_template", on_delete=models.PROTECT, + default=default_voucher, )