Add global context from handler, for email previews

This commit is contained in:
Lance Edgar 2022-08-20 23:07:19 -05:00
parent 2ca93a07e9
commit bdbbe990dd

View file

@ -381,7 +381,15 @@ class EmailPreview(View):
def __init__(self, request): def __init__(self, request):
super(EmailPreview, self).__init__(request) super(EmailPreview, self).__init__(request)
self.email_handler = self.get_handler()
if hasattr(self, 'get_handler'):
warnings.warn("defining a get_handler() method is deprecated; "
"please use AppHandler.get_email_handler() instead",
DeprecationWarning, stacklevel=2)
self.email_handler = get_handler()
else:
app = self.get_rattail_app()
self.email_handler = app.get_email_handler()
@property @property
def handler(self): def handler(self):
@ -390,10 +398,6 @@ class EmailPreview(View):
DeprecationWarning, stacklevel=2) DeprecationWarning, stacklevel=2)
return self.email_handler return self.email_handler
def get_handler(self):
app = self.get_rattail_app()
return app.get_email_handler()
def __call__(self): def __call__(self):
# Forms submitted via POST are only used for sending emails. # Forms submitted via POST are only used for sending emails.
@ -416,10 +420,12 @@ class EmailPreview(View):
key = self.request.POST.get('email_key') key = self.request.POST.get('email_key')
if key: if key:
email = self.email_handler.get_email(key) email = self.email_handler.get_email(key)
data = email.obtain_sample_data(self.request)
context = self.email_handler.make_context()
context.update(email.obtain_sample_data(self.request))
try: try:
self.email_handler.send_message(email, data, self.email_handler.send_message(email, context,
subject_prefix="[PREVIEW] ", subject_prefix="[PREVIEW] ",
to=[recipient], to=[recipient],
cc=None, bcc=None) cc=None, bcc=None)
@ -433,8 +439,11 @@ class EmailPreview(View):
def preview_template(self, key, type_): def preview_template(self, key, type_):
email = self.email_handler.get_email(key) email = self.email_handler.get_email(key)
template = email.get_template(type_) template = email.get_template(type_)
data = email.obtain_sample_data(self.request)
self.request.response.text = template.render(**data) context = self.email_handler.make_context()
context.update(email.obtain_sample_data(self.request))
self.request.response.text = template.render(**context)
if type_ == 'txt': if type_ == 'txt':
self.request.response.content_type = str('text/plain') self.request.response.content_type = str('text/plain')
return self.request.response return self.request.response