Skip to content

Commit 937980a

Browse files
committed
emails: add djmail proxy model, cleanup_email cmd
- new email message model - proxy djmail messages and add model manager with helper methods - cleanup_email cmd deletes draft emails with subject and optionally clears user flag - requires email subject - optional argument for userprofile flag field to clear sent status Bug: T409420 Bug: T412427
1 parent ff8b470 commit 937980a

File tree

5 files changed

+80
-2
lines changed

5 files changed

+80
-2
lines changed

TWLight/emails/backends/mediawiki.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,6 @@ def _send(self, email_message):
255255
if not emailable:
256256
logger.warning("User not emailable, email skipped.")
257257
continue
258-
259258
# POST request to send an email
260259
email_params = {
261260
"action": "emailuser",

TWLight/emails/management/commands/__init__.py

Whitespace-only changes.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# -*- coding: utf-8 -*-
2+
from django.core.management.base import BaseCommand
3+
4+
from TWLight.emails.models import Message
5+
6+
7+
class Command(BaseCommand):
8+
help = "Delete draft emails."
9+
10+
def add_arguments(self, parser):
11+
parser.add_argument(
12+
"--subject",
13+
type=str,
14+
required=True,
15+
help="Email subject",
16+
)
17+
parser.add_argument(
18+
"--userprofile_flag_field",
19+
type=str,
20+
help="Optionally unset userprofile boolean that tracks sent status for messages with the specified subject",
21+
)
22+
23+
def handle(self, *args, **options):
24+
subject = options["subject"]
25+
userprofile_flag_field = options["userprofile_flag_field"]
26+
27+
Message.objects.bulk_cleanup_drafts(
28+
subject=subject, userprofile_flag_field=userprofile_flag_field
29+
)

TWLight/emails/models.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# -*- coding: utf-8 -*-
2+
from django.contrib.auth.models import User
3+
from django.db import models
4+
5+
from djmail.models import Message as DjMail
6+
7+
from TWLight.users.models import UserProfile
8+
9+
10+
class MessageManager(models.Manager):
11+
def drafts(self, subject=None):
12+
if subject is not None:
13+
return self.filter(status=DjMail.STATUS_DRAFT, subject=subject)
14+
return self.filter(status=DjMail.STATUS_DRAFT)
15+
16+
def users_with_drafts(self, subject=None):
17+
email_addresses = self.drafts(subject).values_list("to_email", flat=True)
18+
19+
return User.objects.select_related("userprofile").filter(
20+
email__in=email_addresses
21+
)
22+
23+
def userprofiles_with_drafts(self, subject=None):
24+
email_addresses = self.drafts(subject).values_list("to_email", flat=True)
25+
26+
return UserProfile.objects.select_related("user").filter(
27+
user__email__in=email_addresses
28+
)
29+
30+
def bulk_cleanup_drafts(self, subject=None, userprofile_flag_field=None):
31+
if subject is not None and userprofile_flag_field is not None:
32+
userprofiles = self.userprofiles_with_drafts(subject=subject).filter(
33+
**{userprofile_flag_field: True}
34+
)
35+
for userprofile in userprofiles:
36+
setattr(userprofile, userprofile_flag_field, False)
37+
UserProfile.objects.bulk_update(
38+
userprofiles, [userprofile_flag_field], batch_size=1000
39+
)
40+
41+
if subject is not None:
42+
self.drafts(subject=subject).delete()
43+
44+
45+
class Message(DjMail):
46+
class Meta:
47+
proxy = True
48+
ordering = ["uuid"]
49+
app_label = "emails"
50+
51+
objects = MessageManager()

TWLight/emails/tasks.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
"""
2424

2525
from djmail import template_mail
26-
from djmail.models import Message
2726
from djmail.template_mail import MagicMailBuilder, InlineCSSTemplateMail
2827
import logging
2928
import os

0 commit comments

Comments
 (0)