Skip to content

Commit b16e2b4

Browse files
authored
Merge pull request #77 from zodman/72
Fix the replace property for attr:"type:date"
2 parents 2b0c637 + 7d4b246 commit b16e2b4

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

widget_tweaks/templatetags/widget_tweaks.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,25 @@ def wrapped(field, attr):
1414

1515

1616
def _process_field_attributes(field, attr, process):
17-
1817
# split attribute name and value from 'attr:value' string
19-
#params = attr.split(':', 1)
20-
#attribute = params[0]
18+
# params = attr.split(':', 1)
19+
# attribute = params[0]
2120
params = re.split(r'(?<!:):(?!:)', attr, 1)
22-
#attribute = params[0]
21+
# attribute = params[0]
2322
attribute = params[0].replace('::', ':')
2423
value = params[1] if len(params) == 2 else True
25-
2624
field = copy(field)
27-
2825
# decorate field.as_widget method with updated attributes
2926
old_as_widget = field.as_widget
30-
3127
def as_widget(self, widget=None, attrs=None, only_initial=False):
3228
attrs = attrs or {}
3329
process(widget or self.field.widget, attrs, attribute, value)
30+
if attribute == "type": # change the Input type
31+
self.field.widget.input_type = value
32+
del attrs["type"]
3433
html = old_as_widget(widget, attrs, only_initial)
3534
self.as_widget = old_as_widget
3635
return html
37-
3836
field.as_widget = types.MethodType(as_widget, field)
3937
return field
4038

@@ -45,7 +43,6 @@ def set_attr(field, attr):
4543

4644
def process(widget, attrs, attribute, value):
4745
attrs[attribute] = value
48-
4946
return _process_field_attributes(field, attr, process)
5047

5148

widget_tweaks/tests.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,16 @@ def test_set_data(self):
119119
res = render_field('simple', 'set_data', 'key:value')
120120
assertIn('data-key="value"', res)
121121

122+
def test_replace_type(self):
123+
res = render_field('simple', 'attr', 'type:date')
124+
self.assertTrue(res.count("type=") == 1, (res, res.count("type=")))
125+
assertIn('type="date"', res)
126+
127+
def test_replace_hidden(self):
128+
res = render_field('simple', 'attr', 'type:hidden')
129+
self.assertTrue(res.count("type=") == 1, (res, res.count("type=")))
130+
assertIn('type="hidden"', res)
131+
122132

123133
class ErrorsTest(TestCase):
124134

0 commit comments

Comments
 (0)