Skip to content

Commit e19f98d

Browse files
committed
store mailing template in db, set verbose names for mailing app and models
1 parent 2c67d97 commit e19f98d

9 files changed

Lines changed: 64 additions & 26 deletions

File tree

mailing/admin.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,11 @@ class MailingSchemaAdmin(admin.ModelAdmin):
88
"id",
99
"name",
1010
)
11+
list_display_links = (
12+
"id",
13+
"name",
14+
)
15+
search_fields = (
16+
"id",
17+
"name",
18+
)

mailing/apps.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
class MailingConfig(AppConfig):
55
default_auto_field = "django.db.models.BigAutoField"
66
name = "mailing"
7+
verbose_name = "Рассылка"

mailing/constants.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
default_mailing_schema = {
2-
"key_with_default_value": {
3-
"title": "Ключ с стандартным значением.",
4-
"default": "Значение",
2+
"title": {
3+
"title": "Заголовок письма",
4+
"default": "Рассылка | Procollab",
55
},
6-
"default_key": {"title": "Просто ключ."},
6+
"text": {"title": "Основной текст письма"},
7+
"button_text": {"title": "Текст кнопки", "default": "Кнопка"},
78
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Generated by Django 4.2.3 on 2023-10-07 11:44
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
("mailing", "0005_alter_mailingschema_name"),
10+
]
11+
12+
operations = [
13+
migrations.AlterField(
14+
model_name="mailingschema",
15+
name="schema",
16+
field=models.JSONField(
17+
blank=True,
18+
default={
19+
"default_key": {"title": "Просто ключ."},
20+
"key_with_default_value": {
21+
"default": "Значение",
22+
"title": "Ключ с стандартным значением.",
23+
},
24+
},
25+
null=True,
26+
),
27+
),
28+
migrations.AlterField(
29+
model_name="mailingschema",
30+
name="template",
31+
field=models.TextField(),
32+
),
33+
]

mailing/models.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import os
22
import uuid
33

4-
from django.core.validators import FileExtensionValidator
54
from django.db import models
65
from .constants import default_mailing_schema
76

@@ -15,10 +14,11 @@ def get_template_path(instance, filename):
1514
class MailingSchema(models.Model):
1615
name = models.CharField(max_length=100, unique=True)
1716
schema = models.JSONField(default=default_mailing_schema, null=True, blank=True)
18-
template = models.FileField(
19-
upload_to=get_template_path,
20-
validators=[FileExtensionValidator(allowed_extensions=["html"])],
21-
)
17+
template = models.TextField()
18+
19+
class Meta:
20+
verbose_name = "Схема шаблона письма"
21+
verbose_name_plural = "Схемы шаблонов писем"
2222

2323
def __str__(self):
24-
return self.name
24+
return f"MailingSchema<{self.name}>"

mailing/utils.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,31 @@
1-
import pathlib
2-
from typing import Union, List, Dict
1+
from typing import Dict, List, Union
32

43
import django.db.models
54
from django.contrib.auth import get_user_model
65
from django.core import mail
76
from django.core.mail import EmailMultiAlternatives
8-
from django.template.loader import render_to_string
7+
from django.template import Context, Template
98

109
User = get_user_model()
1110

1211

1312
def send_mail(
1413
user: User,
1514
subject: str,
16-
template_path: str,
15+
template_string: str,
1716
template_context: Union[
1817
Dict,
1918
List,
2019
] = None,
2120
connection=None,
2221
):
23-
return send_mass_mail([user], subject, template_path, template_context, connection)
22+
return send_mass_mail([user], subject, template_string, template_context, connection)
2423

2524

2625
def send_mass_mail(
2726
users: django.db.models.QuerySet | List[User],
2827
subject: str,
29-
template_path: str,
28+
template_string: str,
3029
template_context: Union[
3130
Dict,
3231
List,
@@ -38,20 +37,21 @@ def send_mass_mail(
3837
Throws an error if template render is unsuccessful.
3938
Args:
4039
users: - The list of users who should receive the email.
41-
template_path: str of template_path
40+
template_string: str of template_path
4241
subject: Subject of mail.
4342
template_context: Context for template render.
4443
connection: Connection to mail backend
4544
"""
4645
if template_context is None:
4746
template_context = {}
48-
template_path = pathlib.Path(template_path).absolute()
47+
4948
connection = connection or mail.get_connection()
49+
template = Template(template_string)
5050
messages = []
5151
for user in users:
5252
template_context["user"] = user
53-
html_msg = render_to_string(template_path, template_context)
54-
plain_msg = render_to_string(template_path, template_context)
53+
html_msg = template.render(Context(template_context))
54+
plain_msg = template.render(Context(template_context))
5555
msg = EmailMultiAlternatives(
5656
subject, plain_msg, None, [user.email], connection=connection
5757
)

mailing/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def post(self, request):
2020
if key_in_post in request.POST:
2121
context[variable_name] = request.POST[key_in_post]
2222
users_to_send = CustomUser.objects.filter(pk__in=users)
23-
send_mass_mail(users_to_send, subject, mail_schema.template.path)
23+
send_mass_mail(users_to_send, subject, mail_schema.template, context)
2424
return JsonResponse({"detail": "ok"})
2525

2626

templates/email/bb91facb-1180-423c-9f4d-eed03ad99655.html

Lines changed: 0 additions & 4 deletions
This file was deleted.

templates/mailing/emails/33e403a7-88cb-4daf-a28a-8b42c7a8d45e.html

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)