Skip to content

Commit 39b5f78

Browse files
author
Łukasz Sitko
committed
ordering & default subject from registry
1 parent 947c82c commit 39b5f78

File tree

8 files changed

+127
-33
lines changed

8 files changed

+127
-33
lines changed

emailtemplates/forms.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class Meta:
2727
"subject",
2828
"content",
2929
"language",
30+
"ordering",
3031
"created",
3132
"modified",
3233
]
134 Bytes
Binary file not shown.

emailtemplates/locale/pl/LC_MESSAGES/django.po

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,23 @@ msgid ""
88
msgstr ""
99
"Project-Id-Version: PACKAGE VERSION\n"
1010
"Report-Msgid-Bugs-To: \n"
11-
"POT-Creation-Date: 2022-08-09 13:50+0200\n"
11+
"POT-Creation-Date: 2022-10-26 12:24+0200\n"
1212
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
1313
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1414
"Language-Team: LANGUAGE <[email protected]>\n"
1515
"Language: \n"
1616
"MIME-Version: 1.0\n"
1717
"Content-Type: text/plain; charset=UTF-8\n"
1818
"Content-Transfer-Encoding: 8bit\n"
19-
"Plural-Forms: nplurals=4; plural=(n==1 ? 0 : (n%10>=2 && n%10<=4) && (n"
20-
"%100<12 || n%100>14) ? 1 : n!=1 && (n%10>=0 && n%10<=1) || (n%10>=5 && n"
21-
"%10<=9) || (n%100>=12 && n%100<=14) ? 2 : 3);\n"
19+
"Plural-Forms: nplurals=4; plural=(n==1 ? 0 : (n%10>=2 && n%10<=4) && "
20+
"(n%100<12 || n%100>14) ? 1 : n!=1 && (n%10>=0 && n%10<=1) || (n%10>=5 && "
21+
"n%10<=9) || (n%100>=12 && n%100<=14) ? 2 : 3);\n"
2222

23-
#: emailtemplates/admin.py:19 emailtemplates/models.py:83
23+
#: emailtemplates/admin.py:19 emailtemplates/models.py:98
2424
msgid "Attachment"
2525
msgstr "Załącznik"
2626

27-
#: emailtemplates/admin.py:20 emailtemplates/models.py:84
27+
#: emailtemplates/admin.py:20 emailtemplates/models.py:99
2828
msgid "Attachments"
2929
msgstr "Załączniki"
3030

@@ -40,94 +40,103 @@ msgstr "Akcje"
4040
msgid "E-MAIL TEMPLATES"
4141
msgstr "Szblony wiadomości E-mail"
4242

43-
#: emailtemplates/forms.py:20 emailtemplates/models.py:29
43+
#: emailtemplates/forms.py:20 emailtemplates/models.py:31
4444
msgid "template"
4545
msgstr "szablon"
4646

47-
#: emailtemplates/models.py:27 emailtemplates/models.py:68
48-
#: emailtemplates/models.py:101
47+
#: emailtemplates/models.py:29 emailtemplates/models.py:83
48+
#: emailtemplates/models.py:116
4949
msgid "ID"
5050
msgstr ""
5151

52-
#: emailtemplates/models.py:30 emailtemplates/models.py:103
52+
#: emailtemplates/models.py:33 emailtemplates/models.py:118
5353
msgid "subject"
5454
msgstr "tytuł"
5555

56-
#: emailtemplates/models.py:31 emailtemplates/models.py:104
56+
#: emailtemplates/models.py:36
57+
msgid "you can use variables from table"
58+
msgstr "możesz użyć zmiennych kontekstowych z tabeli"
59+
60+
#: emailtemplates/models.py:38 emailtemplates/models.py:119
5761
msgid "content"
5862
msgstr "zawartość"
5963

60-
#: emailtemplates/models.py:33
64+
#: emailtemplates/models.py:40
6165
msgid "language"
6266
msgstr "język"
6367

64-
#: emailtemplates/models.py:39
68+
#: emailtemplates/models.py:45
69+
#| msgid "Ordering"
70+
msgid "ordering"
71+
msgstr "kolejność"
72+
73+
#: emailtemplates/models.py:47
6574
msgid "attachments"
6675
msgstr "załączniki"
6776

68-
#: emailtemplates/models.py:41
77+
#: emailtemplates/models.py:49
6978
msgid "created"
7079
msgstr "utworzono"
7180

72-
#: emailtemplates/models.py:42
81+
#: emailtemplates/models.py:50
7382
msgid "modified"
7483
msgstr "zmodyfikowano"
7584

76-
#: emailtemplates/models.py:46
85+
#: emailtemplates/models.py:54
7786
msgid "Email template"
7887
msgstr "Szablon wiadomości"
7988

80-
#: emailtemplates/models.py:47
89+
#: emailtemplates/models.py:55
8190
msgid "Email templates"
8291
msgstr "Szablony wiadomości"
8392

84-
#: emailtemplates/models.py:70
93+
#: emailtemplates/models.py:85
8594
msgid "name"
8695
msgstr "nazwa"
8796

88-
#: emailtemplates/models.py:72
97+
#: emailtemplates/models.py:87
8998
msgid "Attachment file"
9099
msgstr "Plik załącznika"
91100

92-
#: emailtemplates/models.py:75
101+
#: emailtemplates/models.py:90
93102
msgid "Comment"
94103
msgstr "Komentarz"
95104

96-
#: emailtemplates/models.py:75
105+
#: emailtemplates/models.py:90
97106
msgid "visible only in admin"
98107
msgstr "widoczne tylko w panelu administracyjnym"
99108

100-
#: emailtemplates/models.py:77
109+
#: emailtemplates/models.py:92
101110
msgid "Ordering"
102111
msgstr "Kolejność"
103112

104-
#: emailtemplates/models.py:78
113+
#: emailtemplates/models.py:93
105114
msgid "Send as link"
106115
msgstr "Wyślij jako link"
107116

108-
#: emailtemplates/models.py:87
117+
#: emailtemplates/models.py:102
109118
#, python-format
110119
msgid "Attachment: %s"
111120
msgstr "Załącznik: %s"
112121

113-
#: emailtemplates/models.py:105
122+
#: emailtemplates/models.py:120
114123
msgid "sent"
115124
msgstr "wysłano"
116125

117-
#: emailtemplates/models.py:108
126+
#: emailtemplates/models.py:123
118127
msgid "Mass email message"
119128
msgstr "Wiadomość grupowa"
120129

121-
#: emailtemplates/models.py:109
130+
#: emailtemplates/models.py:124
122131
msgid "Mass email messages"
123132
msgstr "Wiadomości grupowe"
124133

125-
#: emailtemplates/registry.py:80
134+
#: emailtemplates/registry.py:81
126135
#, python-format
127136
msgid "<b>USAGE: %s</b>"
128137
msgstr "<b>UŻYCIE: %s</b>"
129138

130-
#: emailtemplates/registry.py:83
139+
#: emailtemplates/registry.py:84
131140
#, python-format
132141
msgid "<b>CONTEXT:</b><br/>%s"
133142
msgstr "<b>KONTEKST:</b><br/>%s"
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Generated by Django 3.1.13 on 2022-10-26 07:53
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
dependencies = [
8+
("emailtemplates", "0010_auto_20220803_1419"),
9+
]
10+
11+
operations = [
12+
migrations.AlterModelOptions(
13+
name="emailtemplate",
14+
options={
15+
"ordering": ("ordering",),
16+
"verbose_name": "Email template",
17+
"verbose_name_plural": "Email templates",
18+
},
19+
),
20+
migrations.AddField(
21+
model_name="emailtemplate",
22+
name="ordering",
23+
field=models.PositiveIntegerField(default=1, verbose_name="ordering"),
24+
),
25+
migrations.AlterField(
26+
model_name="emailtemplate",
27+
name="subject",
28+
field=models.CharField(
29+
blank=True,
30+
help_text="you can use variables from table",
31+
max_length=255,
32+
verbose_name="subject",
33+
),
34+
),
35+
]

emailtemplates/models.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
from django.conf import settings
66
from django.db import models
7+
from django.utils import translation
78
from django.utils.translation import gettext_lazy as _
89

910
from emailtemplates.helpers import TemplateSourceLoader, mass_mailing_recipients
11+
from emailtemplates.registry import email_templates, NotRegistered
1012

1113
try:
1214
from django.utils.timezone import now
@@ -27,14 +29,20 @@ class EmailTemplate(models.Model):
2729
auto_created=True, primary_key=True, serialize=False, verbose_name=_("ID")
2830
)
2931
title = models.CharField(_("template"), max_length=255)
30-
subject = models.CharField(_("subject"), max_length=255, blank=True)
32+
subject = models.CharField(
33+
_("subject"),
34+
max_length=255,
35+
blank=True,
36+
help_text=_("you can use variables from table"),
37+
)
3138
content = models.TextField(_("content"))
3239
language = models.CharField(
3340
_("language"),
3441
max_length=10,
3542
choices=settings.LANGUAGES,
3643
default=settings.LANGUAGE_CODE,
3744
)
45+
ordering = models.PositiveIntegerField(verbose_name=_("ordering"), default=1)
3846
attachments = models.ManyToManyField(
3947
"EmailAttachment", blank=True, verbose_name=_("attachments")
4048
)
@@ -45,6 +53,7 @@ class Meta:
4553
unique_together = (("title", "language"),)
4654
verbose_name = _("Email template")
4755
verbose_name_plural = _("Email templates")
56+
ordering = ("ordering",)
4857

4958
def __str__(self):
5059
return "%s -> %s" % (self.title, self.language)
@@ -57,9 +66,18 @@ def get_default_content(self):
5766
logger.error("Error loading template %s. Details: %s ", self.title, e)
5867
return ""
5968

69+
def get_default_subject(self):
70+
translation.activate(self.language)
71+
try:
72+
return email_templates.get_subject(self.title)
73+
except NotRegistered:
74+
return ""
75+
6076
def save(self, *args, **kwargs):
6177
if not self.content:
6278
self.content = self.get_default_content()
79+
if not self.subject:
80+
self.subject = self.get_default_subject()
6381
super(EmailTemplate, self).save(*args, **kwargs)
6482

6583

emailtemplates/registry.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,11 @@ def get_help_values(self):
4949

5050

5151
class RegistrationItem(object):
52-
def __init__(self, path, help_text="", help_context=None, name=""):
52+
def __init__(self, path, help_text="", help_context=None, name="", subject=""):
5353
self.name = name or path
5454
self.path = path
5555
self.help_text = help_text
56+
self.subject = subject
5657
self.help_context_obj = HelpContext(help_context)
5758

5859
@property
@@ -97,7 +98,7 @@ class EmailTemplateRegistry(object):
9798
def __init__(self):
9899
self._registry = {}
99100

100-
def register(self, path, name="", help_text=None, help_context=None):
101+
def register(self, path, name="", help_text=None, help_context=None, subject=""):
101102
"""
102103
Registers email template.
103104
@@ -109,6 +110,7 @@ def register(self, path, name="", help_text=None, help_context=None):
109110
:param path: Template file path. It will become immutable registry lookup key.
110111
:param help_text: Help text to describe template in admin site
111112
:param help_context: Dictionary of possible keys used in the context and description of their content
113+
:param subject: Default subject of email [optional]
112114
113115
`help_context` items values may be strings or tuples of two strings. If strings, then email template preview
114116
will use variable names to fill context, otherwise the second tuple element will become example value.
@@ -118,7 +120,7 @@ def register(self, path, name="", help_text=None, help_context=None):
118120
if path in self._registry:
119121
raise AlreadyRegistered("The template %s is already registered" % path)
120122
self._registry[path] = RegistrationItem(
121-
path, help_text, help_context, name=name
123+
path, help_text, help_context, name=name, subject=subject
122124
)
123125
logger.debug("Registered email template %s", path)
124126

@@ -147,6 +149,9 @@ def get_help_context(self, path):
147149
def get_help_content(self, path):
148150
return self.get_registration(path).get_help_content()
149151

152+
def get_subject(self, path):
153+
return self.get_registration(path).subject
154+
150155
def registration_items(self):
151156
return self._registry.values()
152157

emailtemplates/tests/test_models.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010

1111
from emailtemplates.helpers import TemplateSourceLoader
1212
from emailtemplates.models import EmailTemplate, MassEmailMessage, MassEmailAttachment
13+
from emailtemplates.registry import email_templates, NotRegistered
1314

1415

1516
class EmailTemplateTest(TestCase):
1617
def setUp(self):
1718
self.default_content = "<h1>TEST DEFAULT CONTENT</h1>"
19+
self.subject = "Subject"
1820
self.email_template = EmailTemplate.objects.create(title="template-1.html")
1921

2022
@mock.patch.object(TemplateSourceLoader, "get_source")
@@ -24,18 +26,35 @@ def test_get_default_content(self, mock_source):
2426
self.email_template.get_default_content(), self.default_content
2527
)
2628

29+
@mock.patch.object(email_templates, "get_subject")
30+
def test_get_default_subject(self, mock_subject):
31+
mock_subject.return_value = self.subject
32+
self.assertEqual(self.email_template.get_default_subject(), self.subject)
33+
2734
@mock.patch.object(
2835
TemplateSourceLoader, "get_source", mock.Mock(side_effect=Exception("error..."))
2936
)
3037
def test_get_empty_default_content_if_error(self):
3138
self.assertEqual(self.email_template.get_default_content(), "")
3239

40+
@mock.patch.object(
41+
email_templates, "get_subject", mock.Mock(side_effect=NotRegistered("error..."))
42+
)
43+
def test_get_empty_default_subject_if_error(self):
44+
self.assertEqual(self.email_template.get_default_subject(), "")
45+
3346
@mock.patch.object(TemplateSourceLoader, "get_source")
3447
def test_save_default_content(self, mock_source):
3548
mock_source.return_value = self.default_content
3649
email_template = EmailTemplate.objects.create(title="template-2.html")
3750
self.assertEqual(email_template.content, self.default_content)
3851

52+
@mock.patch.object(email_templates, "get_subject")
53+
def test_save_default_subject(self, mock_subject):
54+
mock_subject.return_value = self.subject
55+
email_template = EmailTemplate.objects.create(title="template-2.html")
56+
self.assertEqual(email_template.subject, self.subject)
57+
3958
@mock.patch.object(TemplateSourceLoader, "get_source")
4059
def test_do_not_override_existing_content(self, mock_source):
4160
mock_source.return_value = self.default_content

emailtemplates/tests/test_template_registry.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ def test_is_registered(self):
8181
registry.register("hello_template.html")
8282
self.assertTrue(registry.is_registered("hello_template.html"))
8383

84+
def test_get_subject(self):
85+
template_registry = EmailTemplateRegistry()
86+
template_registry.register("hello_template.html", subject="subject")
87+
self.assertEqual(
88+
template_registry.get_subject("hello_template.html"), "subject"
89+
)
90+
8491
def test_get_help_text(self):
8592
template_registry = EmailTemplateRegistry()
8693
template_registry.register(

0 commit comments

Comments
 (0)