diff --git a/README.rst b/README.rst index 72213cb..92f1c3e 100644 --- a/README.rst +++ b/README.rst @@ -62,6 +62,29 @@ This packages are needed to make the email complient for all the email clients. npm install --save inline-css npm install --save html-minifier + +Add *'mail_editor'* to the installed apps: + +.. code:: python + + # settings.py + + INSTALLED_APPS = [ + ... + 'mail_editor', + 'ckeditor', + ... + ] + +Add the urls: + +.. code:: python + + # urls.py + + url(r'^mail-editor/', include('mail_editor.urls', namespace='mail_editor')), + + Using the template -------------------- diff --git a/mail_editor/mail_template.py b/mail_editor/mail_template.py index 2dfd4a2..b8f6166 100644 --- a/mail_editor/mail_template.py +++ b/mail_editor/mail_template.py @@ -63,14 +63,14 @@ def check_variables(self, template, field): message = _('These variables are required, but missing: {vars}').format( vars=self._format_vars(missing_vars) ) - raise ValidationError({field: message}, code=self.code) + raise ValidationError(params={field: message}, message=message, code=self.code) unexpected_vars = variables_seen - required_vars - optional_vars if unexpected_vars: message = _('These variables are present, but unexpected: {vars}').format( vars=self._format_vars(unexpected_vars) ) - raise ValidationError({field: message}, code=self.code) + raise ValidationError(params={field: message}, message=message, code=self.code) def _format_vars(self, variables): return ', '.join('{{{{ {} }}}}'.format(var) for var in variables) @@ -90,8 +90,9 @@ def validate_template(mail_template): if errors: main_error = errors[0] - for error in errors[1:]: - error.update_error_dict(main_error.error_dict) + if len(errors) > 1: + for error in errors[1:]: + main_error.update_error_dict(error.error_dict) raise main_error diff --git a/mail_editor/templates/admin/mail_editor/change_form.html b/mail_editor/templates/admin/mail_editor/change_form.html new file mode 100644 index 0000000..7fc0bec --- /dev/null +++ b/mail_editor/templates/admin/mail_editor/change_form.html @@ -0,0 +1,15 @@ +{% extends "admin/change_form.html" %} + + +{% block extrahead %} +{{ block.super }} + +{% endblock %} diff --git a/mail_editor/urls.py b/mail_editor/urls.py new file mode 100644 index 0000000..15adb6f --- /dev/null +++ b/mail_editor/urls.py @@ -0,0 +1,10 @@ +from django.conf.urls import url + +from .views import TemplateVariableView + +# Add app_name to support django 2.0+ +app_name = "mail_editor" + +urlpatterns = [ + url(r'^(?P[-\w]+)/$', TemplateVariableView.as_view(), name='template_variables'), +] diff --git a/mail_editor/views.py b/mail_editor/views.py new file mode 100644 index 0000000..825b712 --- /dev/null +++ b/mail_editor/views.py @@ -0,0 +1,10 @@ +from django.http import HttpResponse +from django.views.generic import View + +from .utils import variable_help_text + + +class TemplateVariableView(View): + def get(self, request, *args, **kwargs): + variables = variable_help_text(kwargs.get('template_type')) + return HttpResponse(variables) diff --git a/tests/settings.py b/tests/settings.py index 681b49a..f0b799f 100644 --- a/tests/settings.py +++ b/tests/settings.py @@ -30,3 +30,16 @@ 'OPTIONS': {}, }, ] + +MAIL_EDITOR_TEMPLATES = { + 'template': { + 'subject': [{ + 'name': 'foo', + 'required': True, + }], + 'body': [{ + 'name': 'bar', + 'required': True, + }], + } +} diff --git a/tests/test_template_validation.py b/tests/test_template_validation.py index c15f90e..75b5091 100644 --- a/tests/test_template_validation.py +++ b/tests/test_template_validation.py @@ -21,6 +21,7 @@ } } + class TemplateValidationTests(TestCase): def test_valid_template(self): settings.MAIL_EDITOR_CONF = CONFIG.copy() @@ -34,7 +35,6 @@ def test_valid_template(self): except ValidationError: pytest.fail("Unexpected validationError") - def test_template_syntax_error(self): settings.MAIL_EDITOR_CONF = CONFIG.copy() template = MailTemplate( @@ -45,5 +45,16 @@ def test_template_syntax_error(self): with pytest.raises(ValidationError) as excinfo: validate_template(template) - print(excinfo.value) - self.assertEqual(excinfo.value.error_code, 'syntax_error') + self.assertEqual(excinfo.value.code, 'syntax_error') + + def test_template_invalid_error(self): + settings.MAIL_EDITOR_CONF = CONFIG.copy() + template = MailTemplate( + template_type='template', + subject='{{ bar }}', + body='{{ bar }}' + ) + with pytest.raises(ValidationError) as excinfo: + validate_template(template) + + self.assertEqual(excinfo.value.message, 'These variables are required, but missing: {{ foo }}')