From 5e37768777d6623a198bd3b8c6e051f5a533d0b2 Mon Sep 17 00:00:00 2001 From: Piotr Gawron <piotr.gawron@uni.lu> Date: Mon, 4 Jun 2018 14:16:25 +0200 Subject: [PATCH] document generation is allowed from voucher edit page --- .../web/migrations/0112_auto_20180604_1021.py | 26 +++++++++++++++++++ smash/web/models/mail_template.py | 9 +++++-- smash/web/templates/vouchers/add_edit.html | 2 ++ smash/web/tests/models/test_mail_template.py | 22 ++++++++++++++++ smash/web/views/mails.py | 5 ++++ smash/web/views/voucher.py | 8 +++++- 6 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 smash/web/migrations/0112_auto_20180604_1021.py diff --git a/smash/web/migrations/0112_auto_20180604_1021.py b/smash/web/migrations/0112_auto_20180604_1021.py new file mode 100644 index 00000000..5d99ccd9 --- /dev/null +++ b/smash/web/migrations/0112_auto_20180604_1021.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.7 on 2018-06-04 10:21 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('web', '0111_auto_20180601_1318'), + ] + + operations = [ + migrations.AlterField( + model_name='mailtemplate', + name='context', + field=models.CharField(choices=[(b'A', b'Appointment'), (b'S', b'Subject'), (b'V', b'Visit'), (b'C', b'Voucher')], max_length=1), + ), + migrations.AlterField( + model_name='mailtemplate', + name='language', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='web.Language'), + ), + ] diff --git a/smash/web/models/mail_template.py b/smash/web/models/mail_template.py index 72901525..2089ae1e 100644 --- a/smash/web/models/mail_template.py +++ b/smash/web/models/mail_template.py @@ -144,11 +144,16 @@ class MailTemplate(models.Model): active_templates = [] disabled_templates = [] for template in templates: - if template.language.name in languages_names: + if template.language is None: + if len(languages) == 0: + active_templates.append(template) + else: + disabled_templates.append(template) + elif template.language.name in languages_names: active_templates.append(template) else: disabled_templates.append(template) - active_templates.sort(key=lambda x: languages_names.index(x.language.name)) + active_templates.sort(key=lambda x: languages_names.index(x.language.name) if x.language is not None else -1) return active_templates, disabled_templates def apply(self, instance, user, stream): diff --git a/smash/web/templates/vouchers/add_edit.html b/smash/web/templates/vouchers/add_edit.html index 0ce71496..c3aa458e 100644 --- a/smash/web/templates/vouchers/add_edit.html +++ b/smash/web/templates/vouchers/add_edit.html @@ -90,6 +90,8 @@ </table> </div> + {% include 'includes/mail_templates_box.html' with instance_id=voucher.id %} + {% endif %} </div> </div> diff --git a/smash/web/tests/models/test_mail_template.py b/smash/web/tests/models/test_mail_template.py index 27d5dcca..500b3a07 100644 --- a/smash/web/tests/models/test_mail_template.py +++ b/smash/web/tests/models/test_mail_template.py @@ -115,6 +115,28 @@ class MailTemplateModelTests(TestCase): voucher.issue_date.strftime(DATE_FORMAT_SHORT) ]) + def test_get_mail_templates_for_context_without_language(self): + template_name_french = "test_without_language" + MailTemplate(name=template_name_french, language=None, + context=MAIL_TEMPLATE_CONTEXT_VOUCHER, + template_file="voucher_test.docx").save() + + templates = MailTemplate.get_mail_templates_for_context([], context=MAIL_TEMPLATE_CONTEXT_VOUCHER) + self.assertEquals(1, len(templates[0])) + + templates = MailTemplate.get_mail_templates_for_context([self.english_language], + context=MAIL_TEMPLATE_CONTEXT_VOUCHER) + self.assertEquals(0, len(templates[0])) + + def test_get_mail_templates_for_context_without_language_2(self): + template_name_french = "test_without_language" + MailTemplate(name=template_name_french, language=self.english_language, + context=MAIL_TEMPLATE_CONTEXT_VOUCHER, + template_file="voucher_test.docx").save() + + templates = MailTemplate.get_mail_templates_for_context([], context=MAIL_TEMPLATE_CONTEXT_VOUCHER) + self.assertEquals(0, len(templates[0])) + def test_apply_visit(self): template_name_french = "test_fr" visit = create_visit() diff --git a/smash/web/views/mails.py b/smash/web/views/mails.py index 53b8557e..8b7e8e89 100644 --- a/smash/web/views/mails.py +++ b/smash/web/views/mails.py @@ -49,6 +49,11 @@ class MailTemplatesCreateView(CreateView, WrappedView): success_url = reverse_lazy('web.views.mail_templates') success_message = "Template created" + def get_form(self, form_class=None): + form = super(MailTemplatesCreateView, self).get_form(form_class) + form.fields['language'].required = False + return form + class MailTemplatesDeleteView(DeleteView, WrappedView): model = MailTemplate diff --git a/smash/web/views/voucher.py b/smash/web/views/voucher.py index 45b5a8f4..3124cc30 100644 --- a/smash/web/views/voucher.py +++ b/smash/web/views/voucher.py @@ -10,7 +10,7 @@ from django_cron import CronJobBase, Schedule from web.views.notifications import get_today_midnight_date from web.forms import VoucherForm -from web.models import Voucher, StudySubject +from web.models import Voucher, StudySubject, MailTemplate from web.models.constants import GLOBAL_STUDY_ID, VOUCHER_STATUS_NEW, VOUCHER_STATUS_EXPIRED from . import WrappedView @@ -67,6 +67,12 @@ class VoucherEditView(SuccessMessageMixin, UpdateView, WrappedView): def get_study_subject_id(self): return Voucher.objects.get(id=self.kwargs['pk']).study_subject.id + def get_context_data(self, *args, **kwargs): + context = super(VoucherEditView, self).get_context_data(*args, **kwargs) + context['mail_templates']= MailTemplate.get_voucher_mail_templates([]) + print context + return context + class ExpireVouchersJob(CronJobBase): RUN_EVERY_MINUTES = 120 -- GitLab