Parcourir la source

Don't use django.template.Context for template rendering

Since Django 1.11 dicts should be passed to template rendering functions
master
Michael van Tellingen il y a 8 ans
Parent
révision
2aac20719e

+ 3
- 2
src/oscar/apps/customer/abstract_models.py Voir le fichier

@@ -6,7 +6,7 @@ from django.contrib.auth import models as auth_models
6 6
 from django.core.urlresolvers import reverse
7 7
 from django.core.validators import RegexValidator
8 8
 from django.db import models
9
-from django.template import Context, Template, TemplateDoesNotExist
9
+from django.template import Template, TemplateDoesNotExist
10 10
 from django.template.loader import get_template
11 11
 from django.utils import six, timezone
12 12
 from django.utils.encoding import python_2_unicode_compatible
@@ -244,8 +244,9 @@ class AbstractCommunicationEventType(models.Model):
244 244
             settings, 'OSCAR_STATIC_BASE_URL', None)
245 245
 
246 246
         messages = {}
247
+        ctx = {}
247 248
         for name, template in templates.items():
248
-            messages[name] = template.render(Context(ctx)) if template else ''
249
+            messages[name] = template.render(ctx) if template else ''
249 250
 
250 251
         # Ensure the email subject doesn't contain any newlines
251 252
         messages['subject'] = messages['subject'].replace("\n", "")

+ 5
- 5
src/oscar/apps/customer/alerts/utils.py Voir le fichier

@@ -4,7 +4,7 @@ from django.conf import settings
4 4
 from django.contrib.sites.models import Site
5 5
 from django.core import mail
6 6
 from django.db.models import Max
7
-from django.template import Context, loader
7
+from django.template import loader
8 8
 
9 9
 from oscar.apps.customer.notifications import services
10 10
 from oscar.core.loading import get_class, get_model
@@ -32,10 +32,10 @@ def send_alert_confirmation(alert):
32 32
     """
33 33
     Send an alert confirmation email.
34 34
     """
35
-    ctx = Context({
35
+    ctx = {
36 36
         'alert': alert,
37 37
         'site': Site.objects.get_current(),
38
-    })
38
+    }
39 39
     subject_tpl = loader.get_template('customer/alerts/emails/'
40 40
                                       'confirmation_subject.txt')
41 41
     body_tpl = loader.get_template('customer/alerts/emails/'
@@ -93,11 +93,11 @@ def send_product_alerts(product):
93 93
         if not data.availability.is_available_to_buy:
94 94
             continue
95 95
 
96
-        ctx = Context({
96
+        ctx = {
97 97
             'alert': alert,
98 98
             'site': Site.objects.get_current(),
99 99
             'hurry': hurry_mode,
100
-        })
100
+        }
101 101
         if alert.user:
102 102
             # Send a site notification
103 103
             num_notifications += 1

+ 2
- 3
src/oscar/forms/widgets.py Voir le fichier

@@ -4,7 +4,6 @@ from django import forms
4 4
 from django.core.files.uploadedfile import InMemoryUploadedFile
5 5
 from django.forms.utils import flatatt
6 6
 from django.forms.widgets import FileInput
7
-from django.template import Context
8 7
 from django.template.loader import render_to_string
9 8
 from django.utils import formats, six
10 9
 from django.utils.encoding import force_text
@@ -42,11 +41,11 @@ class ImageInput(FileInput):
42 41
             image_url = final_attrs['value'] = force_text(
43 42
                 self._format_value(value))
44 43
 
45
-        return render_to_string(self.template_name, Context({
44
+        return render_to_string(self.template_name, {
46 45
             'input_attrs': flatatt(final_attrs),
47 46
             'image_url': image_url,
48 47
             'image_id': "%s-image" % final_attrs['id'],
49
-        }))
48
+        })
50 49
 
51 50
 
52 51
 class WYSIWYGTextArea(forms.Textarea):

+ 2
- 0
src/oscar/templatetags/product_tags.py Voir le fichier

@@ -23,6 +23,8 @@ def render_product(context, product):
23 23
              % product.get_product_class().slug,
24 24
              'catalogue/partials/product.html']
25 25
     template_ = select_template(names)
26
+    context = context.flatten()
27
+
26 28
     # Ensure the passed product is in the context as 'product'
27 29
     context['product'] = product
28 30
     return template_.render(context)

+ 3
- 3
tests/integration/test_settings.py Voir le fichier

@@ -1,5 +1,5 @@
1 1
 from django.test import TestCase
2
-from django.template import loader, Context, TemplateDoesNotExist
2
+from django.template import loader, TemplateDoesNotExist
3 3
 
4 4
 import oscar
5 5
 
@@ -39,11 +39,11 @@ class TestOscarTemplateSettings(TestCase):
39 39
     def test_allows_a_template_to_be_customized(self):
40 40
         path = 'base.html'
41 41
         template = loader.get_template(path)
42
-        rendered_template = template.render(Context())
42
+        rendered_template = template.render({})
43 43
         self.assertIn('Oscar Test Shop', rendered_template)
44 44
 
45 45
     def test_default_oscar_templates_are_accessible(self):
46 46
         path = 'oscar/base.html'
47 47
         template = loader.get_template(path)
48
-        rendered_template = template.render(Context())
48
+        rendered_template = template.render({})
49 49
         self.assertNotIn('Oscar Test Shop', rendered_template)

Chargement…
Annuler
Enregistrer