From 5836099746eec80be94eead28435b36f99e09a9c Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Mon, 16 Aug 2021 19:29:48 -0500 Subject: [PATCH] Tweak how email preview is sent, and attempt "to" is displayed latter only have been changed for the grid view. preview now is sent "properly" via the configured mail handler, which also means that an attempt may be recorded (whereas previously it would not be) --- tailbone/views/email.py | 52 +++++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/tailbone/views/email.py b/tailbone/views/email.py index 2201f8f3..58a0320b 100644 --- a/tailbone/views/email.py +++ b/tailbone/views/email.py @@ -26,6 +26,8 @@ Email Views from __future__ import unicode_literals, absolute_import +import re + import six from rattail import mail @@ -80,8 +82,8 @@ class EmailSettingView(MasterView): self.handler = self.get_handler() def get_handler(self): - # TODO: should let config override which handler we use - return mail.EmailHandler(self.rattail_config) + app = self.get_rattail_app() + return app.get_mail_handler() def get_data(self, session=None): data = [] @@ -277,8 +279,8 @@ class EmailPreview(View): self.handler = self.get_handler() def get_handler(self): - # TODO: should let config override which handler we use - return mail.EmailHandler(self.rattail_config) + app = self.get_rattail_app() + return app.get_mail_handler() def __call__(self): @@ -303,22 +305,15 @@ class EmailPreview(View): if key: email = self.handler.get_email(key) data = email.obtain_sample_data(self.request) - msg = email.make_message(data) - subject = msg['Subject'] - del msg['Subject'] - msg['Subject'] = "[preview] {0}".format(subject) + self.handler.send_message(email, data, + subject_prefix="[PREVIEW] ", + to=[recipient], + cc=None, bcc=None) - del msg['To'] - del msg['Cc'] - del msg['Bcc'] - msg['To'] = recipient - - # TODO: should refactor this to use email handler - sent = mail.deliver_message(self.rattail_config, key, msg) - - self.request.session.flash("Preview for '{}' was {}emailed to {}".format( - key, '' if sent else '(NOT) ', recipient)) + self.request.session.flash( + "Preview for '{}' was emailed to {}".format( + key, recipient)) def preview_template(self, key, type_): email = self.handler.get_email(key) @@ -385,12 +380,33 @@ class EmailAttemptView(MasterView): # status_code g.set_enum('status_code', self.enum.EMAIL_ATTEMPT) + # to + g.set_renderer('to', self.render_to_short) + # links g.set_link('key') g.set_link('sender') g.set_link('subject') g.set_link('to') + to_pattern = re.compile(r'^\{(.*)\}$') + + def render_to_short(self, attempt, column): + value = attempt.to + if not value: + return + + match = self.to_pattern.match(value) + if match: + recips = parse_list(match.group(1)) + if len(recips) > 2: + recips = recips[:2] + recips.append('...') + recips = [HTML.escape(r) for r in recips] + return ', '.join(recips) + + return value + def configure_form(self, f): super(EmailAttemptView, self).configure_form(f)