From d2d5b17dcac857503b3040dabc7c93d567c68054 Mon Sep 17 00:00:00 2001 From: Michael van Tellingen Date: Tue, 4 Apr 2017 23:27:38 +0200 Subject: [PATCH 2/8] Don't use django.template.Context for template rendering Since Django 1.11 dicts should be passed to template rendering functions --- src/oscar/apps/customer/abstract_models.py | 5 +++-- src/oscar/apps/customer/alerts/utils.py | 10 +++++----- src/oscar/forms/widgets.py | 5 ++--- src/oscar/templatetags/product_tags.py | 2 ++ 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/oscar/apps/customer/abstract_models.py b/src/oscar/apps/customer/abstract_models.py index fbebf60..9a6c5ee 100644 --- a/src/oscar/apps/customer/abstract_models.py +++ b/src/oscar/apps/customer/abstract_models.py @@ -6,7 +6,7 @@ from django.contrib.auth import models as auth_models from django.core.urlresolvers import reverse from django.core.validators import RegexValidator from django.db import models -from django.template import Context, Template, TemplateDoesNotExist +from django.template import Template, TemplateDoesNotExist from django.template.loader import get_template from django.utils import six, timezone from django.utils.encoding import python_2_unicode_compatible @@ -238,8 +238,9 @@ class AbstractCommunicationEventType(models.Model): settings, 'OSCAR_STATIC_BASE_URL', None) messages = {} + ctx = {} for name, template in templates.items(): - messages[name] = template.render(Context(ctx)) if template else '' + messages[name] = template.render(ctx) if template else '' # Ensure the email subject doesn't contain any newlines messages['subject'] = messages['subject'].replace("\n", "") diff --git a/src/oscar/apps/customer/alerts/utils.py b/src/oscar/apps/customer/alerts/utils.py index fc3d8bf..776d811 100644 --- a/src/oscar/apps/customer/alerts/utils.py +++ b/src/oscar/apps/customer/alerts/utils.py @@ -4,7 +4,7 @@ from django.conf import settings from django.contrib.sites.models import Site from django.core import mail from django.db.models import Max -from django.template import Context, loader +from django.template import loader from oscar.apps.customer.notifications import services from oscar.core.loading import get_class, get_model @@ -32,10 +32,10 @@ def send_alert_confirmation(alert): """ Send an alert confirmation email. """ - ctx = Context({ + ctx = { 'alert': alert, 'site': Site.objects.get_current(), - }) + } subject_tpl = loader.get_template('customer/alerts/emails/' 'confirmation_subject.txt') body_tpl = loader.get_template('customer/alerts/emails/' @@ -93,11 +93,11 @@ def send_product_alerts(product): if not data.availability.is_available_to_buy: continue - ctx = Context({ + ctx = { 'alert': alert, 'site': Site.objects.get_current(), 'hurry': hurry_mode, - }) + } if alert.user: # Send a site notification num_notifications += 1 diff --git a/src/oscar/forms/widgets.py b/src/oscar/forms/widgets.py index 85ba1b0..5b374cf 100644 --- a/src/oscar/forms/widgets.py +++ b/src/oscar/forms/widgets.py @@ -4,7 +4,6 @@ from django import forms from django.core.files.uploadedfile import InMemoryUploadedFile from django.forms.utils import flatatt from django.forms.widgets import FileInput -from django.template import Context from django.template.loader import render_to_string from django.utils import formats, six from django.utils.encoding import force_text @@ -42,11 +41,11 @@ class ImageInput(FileInput): image_url = final_attrs['value'] = force_text( self._format_value(value)) - return render_to_string(self.template_name, Context({ + return render_to_string(self.template_name, { 'input_attrs': flatatt(final_attrs), 'image_url': image_url, 'image_id': "%s-image" % final_attrs['id'], - })) + }) class WYSIWYGTextArea(forms.Textarea): diff --git a/src/oscar/templatetags/product_tags.py b/src/oscar/templatetags/product_tags.py index ea5c3f2..8fa6f66 100644 --- a/src/oscar/templatetags/product_tags.py +++ b/src/oscar/templatetags/product_tags.py @@ -23,6 +23,8 @@ def render_product(context, product): % product.get_product_class().slug, 'catalogue/partials/product.html'] template_ = select_template(names) + context = context.flatten() + # Ensure the passed product is in the context as 'product' context['product'] = product return template_.render(context) -- 2.9.4