remove special-case imports for legacy versions

standardise use, or avoidance, of timezone, template from_string, and Context
This commit is contained in:
Jonathan Barratt
2016-11-21 09:12:19 +07:00
parent b4aa7767bd
commit 7738bba2ab
5 changed files with 13 additions and 57 deletions

View File

@ -57,21 +57,15 @@ def send_templated_mail(template_name,
along with the File objects to be read. files can be blank.
"""
from django import VERSION
from django.conf import settings
from django.core.mail import EmailMultiAlternatives
from django.template import loader, Context
from helpdesk.models import EmailTemplate
from helpdesk.settings import HELPDESK_EMAIL_SUBJECT_TEMPLATE, \
HELPDESK_EMAIL_FALLBACK_LOCALE
import os
# RemovedInDjango110Warning: render() must be called with a dict, not a Context.
if VERSION >= (1, 8):
context = email_context
else:
context = Context(email_context)
context = email_context
if hasattr(context['queue'], 'locale'):
locale = getattr(context['queue'], 'locale', '')
@ -99,13 +93,8 @@ def send_templated_mail(template_name,
footer_file = os.path.join('helpdesk', locale, 'email_text_footer.txt')
# get_template_from_string was removed in Django 1.8
# http://django.readthedocs.org/en/1.8.x/ref/templates/upgrading.html
try:
from django.template import engines
template_func = engines['django'].from_string
except ImportError: # occurs in django < 1.8
template_func = loader.get_template_from_string
from django.template import engines
template_func = engines['django'].from_string
text_part = template_func(
"%s{%% include '%s' %%}" % (t.plain_text, footer_file)
@ -119,16 +108,12 @@ def send_templated_mail(template_name,
html_txt = html_txt.replace('\r\n', '<br>')
context['comment'] = mark_safe(html_txt)
# get_template_from_string was removed in Django 1.8
# http://django.readthedocs.org/en/1.8.x/ref/templates/upgrading.html
html_part = template_func(
"{%% extends '%s' %%}{%% block title %%}"
"%s"
"{%% endblock %%}{%% block content %%}%s{%% endblock %%}" %
(email_html_base_file, t.heading, t.html)).render(context)
# get_template_from_string was removed in Django 1.8
# http://django.readthedocs.org/en/1.8.x/ref/templates/upgrading.html
subject_part = template_func(
HELPDESK_EMAIL_SUBJECT_TEMPLATE % {
"subject": t.subject,