Просмотр исходного кода

fix(api): use first configured email lane as default

This is taken from the TASK_CONFIG dict. This is possible
as dicts are ordered since Python 3.7.
Peter Thomassen 5 лет назад
Родитель
Сommit
ac982af8b4
3 измененных файлов с 7 добавлено и 5 удалено
  1. 2 2
      api/api/settings.py
  2. 2 1
      api/desecapi/mail_backends.py
  3. 3 2
      api/desecapi/tests/test_mail_backends.py

+ 2 - 2
api/api/settings.py

@@ -150,9 +150,9 @@ NSMASTER_PDNS_API_TOKEN = os.environ['DESECSTACK_NSMASTER_APIKEY']
 CELERY_BROKER_URL = 'amqp://rabbitmq'
 CELERY_EMAIL_MESSAGE_EXTRA_ATTRIBUTES = []  # required because djcelery_email.utils accesses it
 CELERY_TASK_TIME_LIMIT = 30
-TASK_CONFIG = {
-    'email_fast_lane': {'rate_limit': '1/s'},
+TASK_CONFIG = {  # The first entry is the default queue
     'email_slow_lane': {'rate_limit': '3/m'},
+    'email_fast_lane': {'rate_limit': '1/s'},
 }
 
 # pdns accepts request payloads of this size.

+ 2 - 1
api/desecapi/mail_backends.py

@@ -14,7 +14,8 @@ class MultiLaneEmailBackend(BaseEmailBackend):
     config = {'ignore_result': True, 'queue': 'celery'}
     default_backend = 'django.core.mail.backends.smtp.EmailBackend'
 
-    def __init__(self, lane: str, fail_silently=False, **kwargs):
+    def __init__(self, lane: str = None, fail_silently=False, **kwargs):
+        lane = lane or next(iter(settings.TASK_CONFIG))
         self.config.update(name=lane)
         self.config.update(settings.TASK_CONFIG[lane])
         self.task_kwargs = kwargs.copy()

+ 3 - 2
api/desecapi/tests/test_mail_backends.py

@@ -19,11 +19,12 @@ class MultiLaneEmailBackendTestCase(TestCase):
         debug_params_orig = debug_params.copy()
 
         with self.settings(EMAIL_BACKEND='desecapi.mail_backends.MultiLaneEmailBackend'):
-            for lane in ['email_slow_lane', 'email_fast_lane']:
+            for lane in ['email_slow_lane', 'email_fast_lane', None]:
                 subject = f'Test subject for lane {lane}'
                 connection = get_connection(lane=lane, backbackend=self.test_backend, debug=debug_params)
                 EmailMessage(subject=subject, to=['to@test.invalid'], connection=connection).send()
-                self.assertDictEqual(mail.outbox[-1].connection.task_kwargs['debug'], {'lane': lane, **debug_params})
+                self.assertEqual(mail.outbox[-1].connection.task_kwargs['debug'],
+                                 {'lane': lane or 'email_slow_lane', **debug_params})
                 self.assertEqual(mail.outbox[-1].subject, subject)
 
         # Check that the backend hasn't modified the dict we passed