-
-
Notifications
You must be signed in to change notification settings - Fork 673
Expand file tree
/
Copy pathforms.py
More file actions
27 lines (20 loc) · 893 Bytes
/
forms.py
File metadata and controls
27 lines (20 loc) · 893 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
"""Forms for the mailing app."""
from django import forms
from django.template import Context, TemplateSyntaxError
from apps.mailing.models import BaseEmailTemplate
class BaseEmailTemplateForm(forms.ModelForm):
"""Form for editing email templates with Django template syntax validation."""
def clean_content(self):
"""Validate that the content field contains valid Django template syntax."""
content = self.cleaned_data["content"]
try:
template = BaseEmailTemplate.template_engine.from_string(content)
template.render(Context({}))
except TemplateSyntaxError as e:
raise forms.ValidationError(e) from e
else:
return content
class Meta:
"""Meta configuration for BaseEmailTemplateForm."""
model = BaseEmailTemplate
fields = ["internal_name", "subject", "content"]