Refactor email settings/preview views to use email handler

still have one thing to refactor before this is really "done" though..
This commit is contained in:
Lance Edgar 2019-02-13 20:52:57 -06:00
parent bd43884a1d
commit 489619c337

View file

@ -2,7 +2,7 @@
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2018 Lance Edgar
# Copyright © 2010-2019 Lance Edgar
#
# This file is part of Rattail.
#
@ -75,9 +75,17 @@ class ProfilesView(MasterView):
'enabled',
]
def __init__(self, request):
super(ProfilesView, self).__init__(request)
self.handler = self.get_handler()
def get_handler(self):
# TODO: should let config override which handler we use
return mail.EmailHandler(self.rattail_config)
def get_data(self, session=None):
data = []
for email in mail.iter_emails(self.rattail_config):
for email in self.handler.iter_emails():
key = email.key or email.__name__
email = email(self.rattail_config, key)
data.append(self.normalize(email))
@ -135,7 +143,7 @@ class ProfilesView(MasterView):
def get_instance(self):
key = self.request.matchdict['key']
return self.normalize(mail.get_email(self.rattail_config, key))
return self.normalize(self.handler.get_email(key))
def get_instance_title(self, email):
return email['_email'].get_complete_subject(render=False)
@ -211,7 +219,7 @@ class ProfilesView(MasterView):
def template_kwargs_view(self, **kwargs):
key = self.request.matchdict['key']
kwargs['email'] = mail.get_email(self.rattail_config, key)
kwargs['email'] = self.handler.get_email(key)
return kwargs
@ -261,6 +269,14 @@ class EmailPreview(View):
Lists available email templates, and can show previews of each.
"""
def __init__(self, request):
super(EmailPreview, self).__init__(request)
self.handler = self.get_handler()
def get_handler(self):
# TODO: should let config override which handler we use
return mail.EmailHandler(self.rattail_config)
def __call__(self):
# Forms submitted via POST are only used for sending emails.
@ -282,7 +298,7 @@ class EmailPreview(View):
if recipient:
key = self.request.POST.get('email_key')
if key:
email = mail.get_email(self.rattail_config, key)
email = self.handler.get_email(key)
data = email.sample_data(self.request)
msg = email.make_message(data)
@ -295,13 +311,14 @@ class EmailPreview(View):
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))
def preview_template(self, key, type_):
email = mail.get_email(self.rattail_config, key)
email = self.handler.get_email(key)
template = email.get_template(type_)
data = email.sample_data(self.request)
self.request.response.text = template.render(**data)