Skip to content

Commit 6007b18

Browse files
committed
HOTFIX: emails: improve test command & mw backend
Bug: T409420
1 parent f638c34 commit 6007b18

4 files changed

Lines changed: 38 additions & 15 deletions

File tree

TWLight/emails/backends/mediawiki.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,10 @@ def open(self):
195195
self.email_token = email_token
196196
logger.info("Email API session ready.")
197197
return True
198-
except:
198+
except Exception as e:
199199
if not self.fail_silently:
200-
raise
200+
logger.error(e)
201+
raise e
201202

202203
def close(self):
203204
"""Unset the session."""
@@ -282,8 +283,9 @@ def _send(self, email_message):
282283
raise Exception(dumps(emailuser_response))
283284

284285
logger.info("Email sent.")
285-
except:
286+
except Exception as e:
286287
if not self.fail_silently:
287-
raise
288+
logger.error(e)
289+
raise e
288290
return False
289291
return True

TWLight/emails/tasks.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
from TWLight.applications.signals import Reminder
4444
from TWLight.resources.models import AccessCode, Partner
4545
from TWLight.users.groups import get_restricted
46-
from TWLight.users.signals import Notice, TestEmail, UserLoginRetrieval
46+
from TWLight.users.signals import Notice, UserLoginRetrieval
4747
from djmail.models import Message
4848

4949
logger = logging.getLogger(__name__)
@@ -222,12 +222,19 @@ def send_survey_active_user_email(**kwargs):
222222
email.send()
223223

224224

225-
@receiver(TestEmail.test)
226225
def send_test(sender, **kwargs):
227-
user_email = kwargs["email"]
228-
connection = get_connection(
229-
backend="TWLight.emails.backends.mediawiki.EmailBackend"
226+
backend = (
227+
kwargs["backend"]
228+
if "backend" in kwargs
229+
else "TWLight.emails.backends.mediawiki.EmailBackend"
230+
)
231+
connection = (
232+
kwargs["connection"]
233+
if "connection" in kwargs
234+
else get_connection(backend=backend)
230235
)
236+
237+
user_email = kwargs["email"]
231238
template_email = Test()
232239
email = template_email.make_email_object(user_email, {}, connection=connection)
233240
email.send()

TWLight/users/management/commands/test_email.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# -*- coding: utf-8 -*-
22
from django.contrib.auth.models import User
3+
from django.core.mail import get_connection
34
from django.core.management.base import BaseCommand
45

5-
from TWLight.users.signals import TestEmail
6+
from TWLight.emails.tasks import send_test
67

78

89
class Command(BaseCommand):
@@ -14,13 +15,30 @@ def add_arguments(self, parser):
1415
type=str,
1516
help="The wikipedia editor to send the test email to",
1617
)
18+
parser.add_argument(
19+
"--backend",
20+
type=str,
21+
required=False,
22+
help="djmail backend to use; default is TWLight.emails.backends.mediawiki.EmailBackend",
23+
)
1724

1825
def handle(self, *args, **options):
1926
user = User.objects.select_related("editor").get(
2027
editor__wp_username=options["wp_username"]
2128
)
22-
TestEmail.test.send(
29+
backend = (
30+
options["backend"]
31+
if options["backend"]
32+
else "TWLight.emails.backends.mediawiki.EmailBackend"
33+
)
34+
# Use a single connection to send all emails
35+
connection = get_connection(backend=backend)
36+
connection.open()
37+
send_test(
2338
sender=self.__class__,
39+
backend=backend, # allows setting the djmail backend back to default for testing
40+
connection=connection, # passing in the connection lets us handle these in bulk
2441
wp_username=user.editor.wp_username,
2542
email=user.email,
2643
)
44+
connection.close()

TWLight/users/signals.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ class Notice(object):
2525
user_renewal_notice = Signal()
2626

2727

28-
class TestEmail(object):
29-
test = Signal()
30-
31-
3228
class UserLoginRetrieval(object):
3329
user_retrieve_monthly_logins = Signal()
3430

0 commit comments

Comments
 (0)