From ca1199453857b1302be389bdf856fba838be2302 Mon Sep 17 00:00:00 2001 From: Jesse Vickery Date: Tue, 16 Jul 2024 20:24:01 +0000 Subject: [PATCH] feat(dev): gc notify support; - Support for GC Notify mailer. --- ckanext/security/mailer.py | 50 +++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/ckanext/security/mailer.py b/ckanext/security/mailer.py index 573d022..0442596 100644 --- a/ckanext/security/mailer.py +++ b/ckanext/security/mailer.py @@ -7,6 +7,7 @@ from ckan.common import config, is_flask_request from ckan.lib.base import render_jinja2, render from ckan.lib.mailer import get_reset_link_body, mail_user +import ckan.lib.mailer as mailer # (canada fork only): GC Notify support from ckan.plugins import toolkit as tk from ckan import model @@ -66,29 +67,34 @@ def _build_footer_content(extra_vars): def notify_lockout(user, lockout_timeout): - extra_vars = { - 'site_title': config.get('ckan.site_title'), - 'site_url': config.get('ckan.site_url'), - 'user_name': user.name, - 'password_reset_url': - config.get('ckan.site_url').rstrip('/') + '/user/login', - 'lockout_mins': lockout_timeout // 60, - } + # (canada fork only): GC Notify support + try: + # see: ckanext.gcnotify.mailer.notify_lockout + mailer.notify_lockout(user, lockout_timeout) + except mailer.MailerException: + extra_vars = { + 'site_title': config.get('ckan.site_title'), + 'site_url': config.get('ckan.site_url'), + 'user_name': user.name, + 'password_reset_url': + config.get('ckan.site_url').rstrip('/') + '/user/login', + 'lockout_mins': lockout_timeout // 60, + } - if is_flask_request(): - subject = render( - 'security/emails/lockout_subject.txt', extra_vars) - else: - subject = render_jinja2( - 'security/emails/lockout_subject.txt', extra_vars) + if is_flask_request(): + subject = render( + 'security/emails/lockout_subject.txt', extra_vars) + else: + subject = render_jinja2( + 'security/emails/lockout_subject.txt', extra_vars) - subject = subject.split('\n')[0] # Make sure we only use the first line + subject = subject.split('\n')[0] # Make sure we only use the first line - if is_flask_request(): - body = render('security/emails/lockout_mail.txt', extra_vars)\ - + _build_footer_content(extra_vars) - else: - body = render_jinja2('security/emails/lockout_mail.txt', extra_vars)\ - + _build_footer_content(extra_vars) + if is_flask_request(): + body = render('security/emails/lockout_mail.txt', extra_vars)\ + + _build_footer_content(extra_vars) + else: + body = render_jinja2('security/emails/lockout_mail.txt', extra_vars)\ + + _build_footer_content(extra_vars) - mail_user(user, subject, body) + mail_user(user, subject, body)