Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop to master #19

Merged
merged 7 commits into from
Feb 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,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
--------------------

Expand Down
9 changes: 5 additions & 4 deletions mail_editor/mail_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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


Expand Down
15 changes: 15 additions & 0 deletions mail_editor/templates/admin/mail_editor/change_form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% extends "admin/change_form.html" %}


{% block extrahead %}
{{ block.super }}
<script>
django.jQuery(document).ready(function() {
$('#id_template_type').change(function(){
$.get('{% url "mail_editor:template_variables" original.template_type %}', function(data, textStatus, jqXHR) {
$('.field-get_variable_help_text').innerHTML = data
});
});
});
</script>
{% endblock %}
10 changes: 10 additions & 0 deletions mail_editor/urls.py
Original file line number Diff line number Diff line change
@@ -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<template_type>[-\w]+)/$', TemplateVariableView.as_view(), name='template_variables'),
]
10 changes: 10 additions & 0 deletions mail_editor/views.py
Original file line number Diff line number Diff line change
@@ -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)
13 changes: 13 additions & 0 deletions tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,16 @@
'OPTIONS': {},
},
]

MAIL_EDITOR_TEMPLATES = {
'template': {
'subject': [{
'name': 'foo',
'required': True,
}],
'body': [{
'name': 'bar',
'required': True,
}],
}
}
17 changes: 14 additions & 3 deletions tests/test_template_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
}
}


class TemplateValidationTests(TestCase):
def test_valid_template(self):
settings.MAIL_EDITOR_CONF = CONFIG.copy()
Expand All @@ -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(
Expand All @@ -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 }}')