Skip to content

Commit 875153c

Browse files
committed
connection by groups
1 parent 449bd2a commit 875153c

3 files changed

Lines changed: 30 additions & 25 deletions

File tree

mailing/constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ def get_default_mailing_schema() -> dict[str, dict[str, str]]:
99
}
1010

1111

12-
QUANTITY_MAILING_USERS_IN_GROUP = 100
12+
MAILING_USERS_BATCH_SIZE = 100

mailing/utils.py

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from typing import Dict, List, Union
2-
from .constants import QUANTITY_MAILING_USERS_IN_GROUP
2+
from .constants import MAILING_USERS_BATCH_SIZE
33
from .models import MailingSchema
44
from users.models import CustomUser
55

@@ -12,7 +12,7 @@
1212
User = get_user_model()
1313

1414

15-
def prepare_mail_data(post_data):
15+
def prepare_mail_data(post_data) -> dict:
1616
users = post_data.getlist("users[]")
1717
schema_id = post_data["schemas"]
1818
subject = post_data["subject"]
@@ -32,16 +32,12 @@ def prepare_mail_data(post_data):
3232
return data_dict
3333

3434

35-
def new_connection(old_connection):
36-
old_connection.close()
37-
connection = mail.get_connection()
38-
return connection
39-
40-
41-
def get_users_groups(users_list):
42-
users_groups_list = [users_list[user:user + QUANTITY_MAILING_USERS_IN_GROUP] for user in
43-
range(0, len(users_list), QUANTITY_MAILING_USERS_IN_GROUP)]
44-
return users_groups_list
35+
def create_message_groups(messages: list) -> list[list]:
36+
grouped_messages: list[list] = [
37+
messages[message: message + MAILING_USERS_BATCH_SIZE]
38+
for message in range(0, len(messages), MAILING_USERS_BATCH_SIZE)
39+
]
40+
return grouped_messages
4541

4642

4743
def send_mail(
@@ -57,6 +53,13 @@ def send_mail(
5753
return send_mass_mail([user], subject, template_string, template_context, connection)
5854

5955

56+
def send_group_messages(messages: list) -> int:
57+
connection = mail.get_connection()
58+
num_sent = connection.send_messages(messages)
59+
connection.close()
60+
return num_sent
61+
62+
6063
def send_mass_mail(
6164
users: django.db.models.QuerySet | List[User],
6265
subject: str,
@@ -66,7 +69,7 @@ def send_mass_mail(
6669
List,
6770
] = None,
6871
connection=None,
69-
) -> None:
72+
) -> int:
7073
"""
7174
Begin mailing to specified users, sending rendered template with template_text arg.
7275
Throws an error if template render is unsuccessful.
@@ -80,16 +83,18 @@ def send_mass_mail(
8083
if template_context is None:
8184
template_context = {}
8285

83-
connection = connection or mail.get_connection()
8486
template = Template(template_string)
8587
messages = []
8688
for user in users:
8789
template_context["user"] = user
8890
html_msg = template.render(Context(template_context))
8991
plain_msg = template.render(Context(template_context))
90-
msg = EmailMultiAlternatives(
91-
subject, plain_msg, None, [user.email], connection=connection
92-
)
92+
msg = EmailMultiAlternatives(subject, plain_msg, None, [user.email])
9393
msg.attach_alternative(html_msg, "text/html")
9494
messages.append(msg)
95-
return connection.send_messages(messages)
95+
96+
grouped_messages = create_message_groups(messages)
97+
num_sent: int = 0
98+
for group in grouped_messages:
99+
num_sent += send_group_messages(group)
100+
return num_sent

mailing/views.py

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

1111

1212
class SendMailView(APIView):
13-
def post(self, request):
14-
mail_data_dict = prepare_mail_data(request.POST)
13+
def post(self, request) -> JsonResponse:
14+
mail_data: dict = prepare_mail_data(request.POST)
1515
send_mass_mail(
16-
mail_data_dict["users_to_send"],
17-
mail_data_dict["subject"],
18-
mail_data_dict["mail_schema_template"],
19-
mail_data_dict["context"],
16+
mail_data["users_to_send"],
17+
mail_data["subject"],
18+
mail_data["mail_schema_template"],
19+
mail_data["context"],
2020
)
2121
return JsonResponse({"detail": "ok"})
2222

0 commit comments

Comments
 (0)