Skip to content

Commit 3b57c50

Browse files
authored
Merge pull request #601 from PROCOLLAB-github/feature/auto_sending_email
Устраён баг с линивым QuerySet
2 parents 670e18d + 800cd7c commit 3b57c50

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

mailing/constants.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,7 @@ def get_default_mailing_schema() -> dict[str, dict[str, str]]:
1010

1111

1212
MAILING_USERS_BATCH_SIZE = 100
13+
14+
FAILED_ANYMAIL_STATUSES = frozenset(
15+
{"rejected", "failed", "invalid", "bounced", "unknown"}
16+
)

mailing/tasks.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from django.utils import timezone
55

6+
from mailing.constants import FAILED_ANYMAIL_STATUSES
67
from mailing.models import MailingScenarioLog
78
from mailing.scenarios import RecipientRule, SCENARIOS, TriggerType
89
from mailing.utils import send_mass_mail_from_template
@@ -54,8 +55,8 @@ def _send_scenario_for_program(scenario, program, scheduled_for):
5455
],
5556
).values_list("user_id", flat=True)
5657

57-
recipients_to_send = recipients.exclude(id__in=pending_or_sent_ids)
58-
user_ids = list(recipients_to_send.values_list("id", flat=True))
58+
recipients_to_send = list(recipients.exclude(id__in=pending_or_sent_ids))
59+
user_ids = [user.id for user in recipients_to_send]
5960
if not user_ids:
6061
return 0
6162

@@ -99,18 +100,24 @@ def _normalize_status(status_value):
99100
if status_value is None:
100101
return set()
101102
if isinstance(status_value, dict):
102-
return {str(key) for key in status_value.keys()}
103+
statuses = set()
104+
for value in status_value.values():
105+
if isinstance(value, (set, list, tuple)):
106+
statuses.update(str(item) for item in value)
107+
else:
108+
statuses.add(str(value))
109+
return {status.lower() for status in statuses}
103110
if isinstance(status_value, (set, list, tuple)):
104-
return {str(item) for item in status_value}
105-
return {str(status_value)}
111+
return {str(item).lower() for item in status_value}
112+
return {str(status_value).lower()}
106113

107114
def status_callback(user, msg):
108115
nonlocal sent_count, failed_count
109116
status = getattr(msg, "anymail_status", None)
110117
message_id = getattr(status, "message_id", None) if status else None
111118
status_set = _normalize_status(getattr(status, "status", None))
112119
status_str = ",".join(sorted(status_set)) if status_set else "unknown"
113-
is_failed = bool(status_set & {"rejected", "failed", "invalid", "bounced"})
120+
is_failed = not status_set or bool(status_set & FAILED_ANYMAIL_STATUSES)
114121

115122
if not message_id:
116123
failed_count += 1

0 commit comments

Comments
 (0)