From 03a71edc03df94d2b2a35eee28ce19e596090320 Mon Sep 17 00:00:00 2001 From: Tobias Bengfort Date: Thu, 9 Sep 2021 08:26:13 +0200 Subject: [PATCH 1/2] mail: allow to reuse connections across contexts --- castellum/utils/mail.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/castellum/utils/mail.py b/castellum/utils/mail.py index 11c5f6df2..d4be6ce9d 100644 --- a/castellum/utils/mail.py +++ b/castellum/utils/mail.py @@ -41,8 +41,9 @@ def render_body_with_fallback(get_mail_data, text, fallback=None, lang=None): class MailContext: - def __init__(self, name=None): - self.connection = None + def __init__(self, name=None, connection=None): + self.connection = connection + self.close_connection = False self.data = { 'from_email': settings.DEFAULT_FROM_EMAIL, 'language': settings.LANGUAGE_CODE, @@ -57,6 +58,7 @@ class MailContext: def __enter__(self): if not self.connection: self.connection = get_connection(**self.data['connection']) + self.close_connection = True try: self.connection.open() except Exception: @@ -66,7 +68,8 @@ class MailContext: return self def __exit__(self, type, value, traceback): - self.connection.close() + if self.close_connection: + self.connection.close() self.connection = None def prepare_body(self, body, recipients_string=''): -- GitLab From c2196bde9608be13befb23f93d3bb012cfcebcac Mon Sep 17 00:00:00 2001 From: Tobias Bengfort Date: Thu, 9 Sep 2021 08:27:18 +0200 Subject: [PATCH 2/2] reduce the amount of mail connections in send_appointment_reminder --- .../commands/send_appointment_reminders.py | 50 +++++++++---------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/castellum/appointments/management/commands/send_appointment_reminders.py b/castellum/appointments/management/commands/send_appointment_reminders.py index eac18375f..e1ded801c 100644 --- a/castellum/appointments/management/commands/send_appointment_reminders.py +++ b/castellum/appointments/management/commands/send_appointment_reminders.py @@ -46,7 +46,7 @@ def send_mail_to_subject(ctx, study, appointment): ) -def send_mail_to_study_conductors(study, not_reachable, base_url): +def send_mail_to_study_conductors(study, not_reachable, base_url, ctx): urls = '\n'.join( base_url + reverse( 'execution:participation-detail', @@ -54,16 +54,15 @@ def send_mail_to_study_conductors(study, not_reachable, base_url): ) for appointment in not_reachable ) - with MailContext('internal') as ctx: - return ctx.send_mail( - settings.CASTELLUM_APPOINTMENT_NO_EMAIL_MAIL_SUBJECT.format(study=study), - ( - lambda: {'urls': urls}, - settings.CASTELLUM_APPOINTMENT_NO_EMAIL_MAIL_BODY, - settings.CASTELLUM_APPOINTMENT_NO_EMAIL_MAIL_BODY_EN, - ), - [user.email for user in study.conductors], - ) + return ctx.send_mail( + settings.CASTELLUM_APPOINTMENT_NO_EMAIL_MAIL_SUBJECT.format(study=study), + ( + lambda: {'urls': urls}, + settings.CASTELLUM_APPOINTMENT_NO_EMAIL_MAIL_BODY, + settings.CASTELLUM_APPOINTMENT_NO_EMAIL_MAIL_BODY_EN, + ), + [user.email for user in study.conductors], + ) def send_appointment_reminders(base_url): @@ -71,17 +70,17 @@ def send_appointment_reminders(base_url): reached_count = 0 not_reached_count = 0 - for study in Study.objects.filter(status=Study.EXECUTION): - due_appointments = Appointment.objects.filter( - start__gte=now, - start__lte=now + settings.CASTELLUM_APPOINTMENT_REMINDER_PERIOD, - reminded=False, - session__study=study, - participation__status=Participation.INVITED, - ).select_related('participation__subject') - not_reachable = [] - - with MailContext('recruitment') as ctx: + with MailContext('recruitment') as ctx: + for study in Study.objects.filter(status=Study.EXECUTION): + due_appointments = Appointment.objects.filter( + start__gte=now, + start__lte=now + settings.CASTELLUM_APPOINTMENT_REMINDER_PERIOD, + reminded=False, + session__study=study, + participation__status=Participation.INVITED, + ).select_related('participation__subject') + not_reachable = [] + for appointment in due_appointments: success = send_mail_to_subject(ctx, study, appointment) if success: @@ -90,10 +89,11 @@ def send_appointment_reminders(base_url): not_reached_count += 1 not_reachable.append(appointment) - if not_reachable: - send_mail_to_study_conductors(study, not_reachable, base_url) + if not_reachable: + with MailContext('internal', ctx.connection) as internal_ctx: + send_mail_to_study_conductors(study, not_reachable, base_url, internal_ctx) - due_appointments.update(reminded=True) + due_appointments.update(reminded=True) return reached_count, not_reached_count -- GitLab