Skip to content

Commit 7ec7ca0

Browse files
Lukáš Klímaxkubov
authored andcommitted
feat: add EmailTemplate versioning
1 parent 881a58c commit 7ec7ca0

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

docs/installation.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ E-MAIL
130130

131131
.. attribute:: PYMESS_EMAIL_TEMPLATE_BASE_TEMPLATE
132132

133-
Path to the file containing an e-mail content in the Django template system format.
133+
Path to the file containing e-mail content in Django template format. For multiple template versions, define them as a dictionary: ``{"version": "path"}``.
134134

135135
.. attribute:: PYMESS_EMAIL_TEMPLATE_TEMPLATETAGS
136136

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Generated by Django 4.2.23 on 2025-08-06 10:19
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('pymess', '0032_migration'),
10+
]
11+
12+
operations = [
13+
migrations.AddField(
14+
model_name='emailtemplate',
15+
name='version',
16+
field=models.CharField(verbose_name='version', blank=True, max_length=10, null=True),
17+
),
18+
]

pymess/models/emails.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ class AbstractEmailTemplate(BaseAbstractTemplate):
163163
sender = models.EmailField(verbose_name=_('sender'), null=True, blank=True, max_length=200)
164164
sender_name = models.CharField(verbose_name=_('sender name'), blank=True, null=True, max_length=250)
165165
variant = models.CharField(verbose_name=_('variant'), max_length=10, null=True, blank=True, editable=False)
166+
version = models.CharField(verbose_name=_('version'), max_length=10, null=True, blank=True)
166167

167168
def get_controller(self):
168169
from pymess.backend.emails import EmailController
@@ -223,8 +224,23 @@ def clean_subject(self, context_data=None):
223224
super().clean_subject(context_data={'EMAIL_DISABLE_VARIABLE_VALIDATOR': True})
224225
raise_error_if_contains_banned_tags(self.subject)
225226

227+
def get_base_template(self):
228+
base_template_config = settings.EMAIL_TEMPLATE_BASE_TEMPLATE
229+
230+
if not base_template_config:
231+
raise ValidationError(gettext('EMAIL_TEMPLATE_BASE_TEMPLATE is not set'))
232+
233+
if isinstance(base_template_config, dict):
234+
if not self.version:
235+
return next(iter(base_template_config.values()), None)
236+
237+
return base_template_config.get(self.version)
238+
239+
return base_template_config
240+
241+
226242
def _extend_body(self, template_body):
227-
base_template = settings.EMAIL_TEMPLATE_BASE_TEMPLATE
243+
base_template = self.get_base_template()
228244
templatetags = settings.EMAIL_TEMPLATE_TEMPLATETAGS
229245

230246
template_content = '{{% block {} %}}{}{{% endblock %}}'.format(

0 commit comments

Comments
 (0)