aboutsummaryrefslogtreecommitdiffstats
path: root/main/py-django-oscar/0003-Update-the-custom-form-widgets-for-Django-1.11.patch
blob: 0dbef929ff411571a8fdcc0e93f43ba427e4f24c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
From bf41f12337554af742148e1d04427974c3ed33fc Mon Sep 17 00:00:00 2001
From: Michael van Tellingen <michael@mvantellingen.nl>
Date: Thu, 6 Apr 2017 17:04:19 +0200
Subject: [PATCH 3/8] Update the custom form widgets for Django 1.11

In Django 1.11 the widgets are rendered with templates instead of
python code. Update the code for this change
---
 src/oscar/apps/customer/abstract_models.py |  5 ++---
 src/oscar/forms/widgets.py                 | 28 ++++++++++++++++++++--------
 2 files changed, 22 insertions(+), 11 deletions(-)

diff --git a/src/oscar/apps/customer/abstract_models.py b/src/oscar/apps/customer/abstract_models.py
index 9a6c5ee..055fda3 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 Template, TemplateDoesNotExist
+from django.template import Context, TemplateDoesNotExist, engines
 from django.template.loader import get_template
 from django.utils import six, timezone
 from django.utils.encoding import python_2_unicode_compatible
@@ -222,7 +222,7 @@ class AbstractCommunicationEventType(models.Model):
             field = getattr(self, attr_name, None)
             if field is not None:
                 # Template content is in a model field
-                templates[name] = Template(field)
+                templates[name] = engines['django'].from_string(field)
             else:
                 # Model field is empty - look for a file template
                 template_name = getattr(self, "%s_file" % attr_name) % code
@@ -238,7 +238,6 @@ class AbstractCommunicationEventType(models.Model):
             settings, 'OSCAR_STATIC_BASE_URL', None)
 
         messages = {}
-        ctx = {}
         for name, template in templates.items():
             messages[name] = template.render(ctx) if template else ''
 
diff --git a/src/oscar/forms/widgets.py b/src/oscar/forms/widgets.py
index 5b374cf..cdeb6b9 100644
--- a/src/oscar/forms/widgets.py
+++ b/src/oscar/forms/widgets.py
@@ -1,5 +1,6 @@
 import re
 
+import django
 from django import forms
 from django.core.files.uploadedfile import InMemoryUploadedFile
 from django.forms.utils import flatatt
@@ -23,7 +24,7 @@ class ImageInput(FileInput):
     template_name = 'partials/image_input_widget.html'
     attrs = {'accept': 'image/*'}
 
-    def render(self, name, value, attrs=None):
+    def render(self, name, value, attrs=None, renderer=None):
         """
         Render the ``input`` field based on the defined ``template_name``. The
         image URL is take from *value* and is provided to the template as
@@ -33,7 +34,15 @@ class ImageInput(FileInput):
         If *value* contains no valid image URL an empty string will be provided
         in the context.
         """
-        final_attrs = self.build_attrs(attrs, type=self.input_type, name=name)
+        extra_attrs = {
+            'type': self.input_type,
+            'name': name,
+        }
+        if django.VERSION < (1, 11):
+            final_attrs = self.build_attrs(attrs, **extra_attrs)
+        else:
+            final_attrs = self.build_attrs(attrs, extra_attrs=extra_attrs)
+
         if not value or isinstance(value, InMemoryUploadedFile):
             # can't display images that aren't stored
             image_url = ''
@@ -163,7 +172,7 @@ class TimePickerInput(DateTimeWidgetMixin, forms.TimeInput):
     """
     format_key = 'TIME_INPUT_FORMATS'
 
-    def render(self, name, value, attrs=None):
+    def render(self, name, value, attrs=None, renderer=None):
         format = self.get_format()
         input = super(TimePickerInput, self).render(
             name, value, self.gett_attrs(attrs, format))
@@ -192,7 +201,7 @@ class DatePickerInput(DateTimeWidgetMixin, forms.DateInput):
     """
     format_key = 'DATE_INPUT_FORMATS'
 
-    def render(self, name, value, attrs=None):
+    def render(self, name, value, attrs=None, renderer=None):
         format = self.get_format()
         input = super(DatePickerInput, self).render(
             name, value, self.gett_attrs(attrs, format))
@@ -235,7 +244,7 @@ class DateTimePickerInput(DateTimeWidgetMixin, forms.DateTimeInput):
         if not include_seconds and self.format:
             self.format = re.sub(':?%S', '', self.format)
 
-    def render(self, name, value, attrs=None):
+    def render(self, name, value, attrs=None, renderer=None):
         format = self.get_format()
         input = super(DateTimePickerInput, self).render(
             name, value, self.gett_attrs(attrs, format))
@@ -314,15 +323,18 @@ class RemoteSelect(forms.Widget):
         else:
             return six.text_type(value)
 
-    def render(self, name, value, attrs=None, choices=()):
-        attrs = self.build_attrs(attrs, **{
+    def render(self, name, value, attrs=None, renderer=None):
+        attrs = {} if attrs is None else attrs
+
+        extra_attrs = {
             'type': 'hidden',
             'name': name,
             'data-ajax-url': self.lookup_url,
             'data-multiple': 'multiple' if self.is_multiple else '',
             'value': self.format_value(value),
             'data-required': 'required' if self.is_required else '',
-        })
+        }
+        attrs = self.build_attrs(attrs, extra_attrs=extra_attrs)
         return mark_safe(u'<input %s>' % flatatt(attrs))
 
 
-- 
2.9.4