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)
This commit is contained in:
Lance Edgar 2021-08-16 19:29:48 -05:00
parent a10de791a1
commit 5836099746

View file

@ -26,6 +26,8 @@ Email Views
from __future__ import unicode_literals, absolute_import from __future__ import unicode_literals, absolute_import
import re
import six import six
from rattail import mail from rattail import mail
@ -80,8 +82,8 @@ class EmailSettingView(MasterView):
self.handler = self.get_handler() self.handler = self.get_handler()
def get_handler(self): def get_handler(self):
# TODO: should let config override which handler we use app = self.get_rattail_app()
return mail.EmailHandler(self.rattail_config) return app.get_mail_handler()
def get_data(self, session=None): def get_data(self, session=None):
data = [] data = []
@ -277,8 +279,8 @@ class EmailPreview(View):
self.handler = self.get_handler() self.handler = self.get_handler()
def get_handler(self): def get_handler(self):
# TODO: should let config override which handler we use app = self.get_rattail_app()
return mail.EmailHandler(self.rattail_config) return app.get_mail_handler()
def __call__(self): def __call__(self):
@ -303,22 +305,15 @@ class EmailPreview(View):
if key: if key:
email = self.handler.get_email(key) email = self.handler.get_email(key)
data = email.obtain_sample_data(self.request) data = email.obtain_sample_data(self.request)
msg = email.make_message(data)
subject = msg['Subject'] self.handler.send_message(email, data,
del msg['Subject'] subject_prefix="[PREVIEW] ",
msg['Subject'] = "[preview] {0}".format(subject) to=[recipient],
cc=None, bcc=None)
del msg['To'] self.request.session.flash(
del msg['Cc'] "Preview for '{}' was emailed to {}".format(
del msg['Bcc'] key, recipient))
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))
def preview_template(self, key, type_): def preview_template(self, key, type_):
email = self.handler.get_email(key) email = self.handler.get_email(key)
@ -385,12 +380,33 @@ class EmailAttemptView(MasterView):
# status_code # status_code
g.set_enum('status_code', self.enum.EMAIL_ATTEMPT) g.set_enum('status_code', self.enum.EMAIL_ATTEMPT)
# to
g.set_renderer('to', self.render_to_short)
# links # links
g.set_link('key') g.set_link('key')
g.set_link('sender') g.set_link('sender')
g.set_link('subject') g.set_link('subject')
g.set_link('to') 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): def configure_form(self, f):
super(EmailAttemptView, self).configure_form(f) super(EmailAttemptView, self).configure_form(f)