Revision 8e2877e7

b/widget_tweaks/__init__.py
1

  
b/widget_tweaks/models.py
1

  
b/widget_tweaks/templatetags/__init__.py
1

  
b/widget_tweaks/templatetags/widget_tweaks.py
1
import re
2
from django.template import Library, Node, Variable, TemplateSyntaxError
3
register = Library()
4

  
5

  
6
def silence_without_field(fn):
7
    def wrapped(field, attr):
8
        if not field:
9
            return ""
10
        return fn(field, attr)
11
    return wrapped
12

  
13

  
14
def _process_field_attributes(field, attr, process):
15

  
16
    # split attribute name and value from 'attr:value' string
17
    params = attr.split(':', 1)
18
    attribute = params[0]
19
    value = params[1] if len(params) == 2 else ''
20

  
21
    # decorate field.as_widget method with updated attributes
22
    old_as_widget = field.as_widget
23

  
24
    def as_widget(self, widget=None, attrs=None, only_initial=False):
25
        attrs = attrs or {}
26
        process(widget or self.field.widget, attrs, attribute, value)
27
        return old_as_widget(widget, attrs, only_initial)
28

  
29
    bound_method = type(old_as_widget)
30
    try:
31
        field.as_widget = bound_method(as_widget, field, field.__class__)
32
    except TypeError:  # python 3
33
        field.as_widget = bound_method(as_widget, field)
34
    return field
35

  
36

  
37
@register.filter("attr")
38
@silence_without_field
39
def set_attr(field, attr):
40

  
41
    def process(widget, attrs, attribute, value):
42
        attrs[attribute] = value
43

  
44
    return _process_field_attributes(field, attr, process)
45

  
46

  
47
@register.filter("add_error_attr")
48
@silence_without_field
49
def add_error_attr(field, attr):
50
    if hasattr(field, 'errors') and field.errors:
51
        return set_attr(field, attr)
52
    return field
53

  
54

  
55
@register.filter("append_attr")
56
@silence_without_field
57
def append_attr(field, attr):
58
    def process(widget, attrs, attribute, value):
59
        if attrs.get(attribute):
60
            attrs[attribute] += ' ' + value
61
        elif widget.attrs.get(attribute):
62
            attrs[attribute] = widget.attrs[attribute] + ' ' + value
63
        else:
64
            attrs[attribute] = value
65
    return _process_field_attributes(field, attr, process)
66

  
67

  
68
@register.filter("add_class")
69
@silence_without_field
70
def add_class(field, css_class):
71
    return append_attr(field, 'class:' + css_class)
72

  
73

  
74
@register.filter("add_error_class")
75
@silence_without_field
76
def add_error_class(field, css_class):
77
    if hasattr(field, 'errors') and field.errors:
78
        return add_class(field, css_class)
79
    return field
80

  
81

  
82
@register.filter("set_data")
83
@silence_without_field
84
def set_data(field, data):
85
    return set_attr(field, 'data-' + data)
86

  
87

  
88
@register.filter(name='field_type')
89
def field_type(field):
90
    """
91
    Template filter that returns field class name (in lower case).
92
    E.g. if field is CharField then {{ field|field_type }} will
93
    return 'charfield'.
94
    """
95
    if hasattr(field, 'field') and field.field:
96
        return field.field.__class__.__name__.lower()
97
    return ''
98

  
99

  
100
@register.filter(name='widget_type')
101
def widget_type(field):
102
    """
103
    Template filter that returns field widget class name (in lower case).
104
    E.g. if field's widget is TextInput then {{ field|widget_type }} will
105
    return 'textinput'.
106
    """
107
    if hasattr(field, 'field') and hasattr(field.field, 'widget') and field.field.widget:
108
        return field.field.widget.__class__.__name__.lower()
109
    return ''
110

  
111

  
112
# ======================== render_field tag ==============================
113

  
114
ATTRIBUTE_RE = re.compile(r"""
115
    (?P<attr>
116
        [\w_-]+
117
    )
118
    (?P<sign>
119
        \+?=
120
    )
121
    (?P<value>
122
    ['"]? # start quote
123
        [^"']*
124
    ['"]? # end quote
125
    )
126
""", re.VERBOSE | re.UNICODE)
127

  
128
@register.tag
129
def render_field(parser, token):
130
    """
131
    Render a form field using given attribute-value pairs
132

  
133
    Takes form field as first argument and list of attribute-value pairs for
134
    all other arguments.  Attribute-value pairs should be in the form of
135
    attribute=value or attribute="a value" for assignment and attribute+=value
136
    or attribute+="value" for appending.
137
    """
138
    error_msg = '%r tag requires a form field followed by a list of attributes and values in the form attr="value"' % token.split_contents()[0]
139
    try:
140
        bits = token.split_contents()
141
        tag_name = bits[0]
142
        form_field = bits[1]
143
        attr_list = bits[2:]
144
    except ValueError:
145
        raise TemplateSyntaxError(error_msg)
146

  
147
    form_field = parser.compile_filter(form_field)
148

  
149
    set_attrs = []
150
    append_attrs = []
151
    for pair in attr_list:
152
        match = ATTRIBUTE_RE.match(pair)
153
        if not match:
154
            raise TemplateSyntaxError(error_msg + ": %s" % pair)
155
        dct = match.groupdict()
156
        attr, sign, value = dct['attr'], dct['sign'], parser.compile_filter(dct['value'])
157
        if sign == "=":
158
            set_attrs.append((attr, value))
159
        else:
160
            append_attrs.append((attr, value))
161

  
162
    return FieldAttributeNode(form_field, set_attrs, append_attrs)
163

  
164

  
165
class FieldAttributeNode(Node):
166
    def __init__(self, field, set_attrs, append_attrs):
167
        self.field = field
168
        self.set_attrs = set_attrs
169
        self.append_attrs = append_attrs
170

  
171
    def render(self, context):
172
        bounded_field = self.field.resolve(context)
173
        field = getattr(bounded_field, 'field', None)
174
        if (getattr(bounded_field, 'errors', None) and
175
            'WIDGET_ERROR_CLASS' in context):
176
            bounded_field = append_attr(bounded_field, 'class:%s' %
177
                                        context['WIDGET_ERROR_CLASS'])
178
        if field and field.required and 'WIDGET_REQUIRED_CLASS' in context:
179
            bounded_field = append_attr(bounded_field, 'class:%s' %
180
                                        context['WIDGET_REQUIRED_CLASS'])
181
        for k, v in self.set_attrs:
182
            bounded_field = set_attr(bounded_field, '%s:%s' % (k,v.resolve(context)))
183
        for k, v in self.append_attrs:
184
            bounded_field = append_attr(bounded_field, '%s:%s' % (k,v.resolve(context)))
185
        return bounded_field
b/widget_tweaks/tests.py
1
import string
2
try:
3
    from django.utils.unittest import expectedFailure
4
except ImportError:
5
    def expectedFailure(func):
6
        return lambda *args, **kwargs: None
7

  
8
from django.test import TestCase
9
from django.forms import Form, CharField, TextInput
10
from django import forms
11
from django.template import Template, Context
12
from django.forms.extras.widgets import SelectDateWidget
13

  
14
# ==============================
15
#       Testing helpers
16
# ==============================
17

  
18
class MyForm(Form):
19
    """
20
    Test form. If you want to test rendering of a field,
21
    add it to this form and use one of 'render_...' functions
22
    from this module.
23
    """
24
    simple = CharField()
25
    with_attrs = CharField(widget=TextInput(attrs={
26
                    'foo': 'baz',
27
                    'egg': 'spam'
28
                 }))
29
    with_cls = CharField(widget=TextInput(attrs={'class':'class0'}))
30
    date = forms.DateField(widget=SelectDateWidget(attrs={'egg': 'spam'}))
31

  
32

  
33
def render_form(text, form=None, **context_args):
34
    """
35
    Renders template ``text`` with widget_tweaks library loaded
36
    and MyForm instance available in context as ``form``.
37
    """
38
    tpl = Template("{% load widget_tweaks %}" + text)
39
    context_args.update({'form': MyForm() if form is None else form})
40
    context = Context(context_args)
41
    return tpl.render(context)
42

  
43

  
44
def render_field(field, template_filter, params, *args, **kwargs):
45
    """
46
    Renders ``field`` of MyForm with filter ``template_filter`` applied.
47
    ``params`` are filter arguments.
48

  
49
    If you want to apply several filters (in a chain),
50
    pass extra ``template_filter`` and ``params`` as positional arguments.
51

  
52
    In order to use custom form, pass form instance as ``form``
53
    keyword argument.
54
    """
55
    filters = [(template_filter, params)]
56
    filters.extend(zip(args[::2], args[1::2]))
57
    filter_strings = ['|%s:"%s"' % (f[0], f[1],) for f in filters]
58
    render_field_str = '{{ form.%s%s }}' % (field, ''.join(filter_strings))
59
    return render_form(render_field_str, **kwargs)
60

  
61

  
62
def render_field_from_tag(field, *attributes):
63
    """
64
    Renders MyForm's field ``field`` with attributes passed
65
    as positional arguments.
66
    """
67
    attr_strings = [' %s' % f for f in attributes]
68
    tpl = string.Template('{% render_field form.$field$attrs %}')
69
    render_field_str = tpl.substitute(field=field, attrs=''.join(attr_strings))
70
    return render_form(render_field_str)
71

  
72

  
73
def assertIn(value, obj):
74
    assert value in obj, "%s not in %s" % (value, obj,)
75

  
76

  
77
def assertNotIn(value, obj):
78
    assert value not in obj, "%s in %s" % (value, obj,)
79

  
80

  
81
# ===============================
82
#           Test cases
83
# ===============================
84

  
85
class SimpleAttrTest(TestCase):
86
    def test_attr(self):
87
        res = render_field('simple', 'attr', 'foo:bar')
88
        assertIn('type="text"', res)
89
        assertIn('name="simple"', res)
90
        assertIn('id="id_simple"', res)
91
        assertIn('foo="bar"', res)
92

  
93
    def test_attr_chaining(self):
94
        res = render_field('simple', 'attr', 'foo:bar', 'attr', 'bar:baz')
95
        assertIn('type="text"', res)
96
        assertIn('name="simple"', res)
97
        assertIn('id="id_simple"', res)
98
        assertIn('foo="bar"', res)
99
        assertIn('bar="baz"', res)
100

  
101
    def test_add_class(self):
102
        res = render_field('simple', 'add_class', 'foo')
103
        assertIn('class="foo"', res)
104

  
105
    def test_add_multiple_classes(self):
106
        res = render_field('simple', 'add_class', 'foo bar')
107
        assertIn('class="foo bar"', res)
108

  
109
    def test_add_class_chaining(self):
110
        res = render_field('simple', 'add_class', 'foo', 'add_class', 'bar')
111
        assertIn('class="bar foo"', res)
112

  
113
    def test_set_data(self):
114
        res = render_field('simple', 'set_data', 'key:value')
115
        assertIn('data-key="value"', res)
116

  
117

  
118
class ErrorsTest(TestCase):
119

  
120
    def _err_form(self):
121
        form = MyForm({'foo': 'bar'})  # some random data
122
        form.is_valid()  # trigger form validation
123
        return form
124

  
125
    def test_error_class_no_error(self):
126
        res = render_field('simple', 'add_error_class', 'err')
127
        assertNotIn('class="err"', res)
128

  
129
    def test_error_class_error(self):
130
        form = self._err_form()
131
        res = render_field('simple', 'add_error_class', 'err', form=form)
132
        assertIn('class="err"', res)
133

  
134
    def test_error_attr_no_error(self):
135
        res = render_field('simple', 'add_error_attr', 'aria-invalid:true')
136
        assertNotIn('aria-invalid="true"', res)
137

  
138
    def test_error_attr_error(self):
139
        form = self._err_form()
140
        res = render_field('simple', 'add_error_attr', 'aria-invalid:true', form=form)
141
        assertIn('aria-invalid="true"', res)
142

  
143

  
144
class SilenceTest(TestCase):
145
    def test_silence_without_field(self):
146
        res = render_field("nothing", 'attr', 'foo:bar')
147
        self.assertEqual(res, "")
148
        res = render_field("nothing", 'add_class', 'some')
149
        self.assertEqual(res, "")
150

  
151

  
152
class CustomizedWidgetTest(TestCase):
153
    def test_attr(self):
154
        res = render_field('with_attrs', 'attr', 'foo:bar')
155
        assertIn('foo="bar"', res)
156
        assertNotIn('foo="baz"', res)
157
        assertIn('egg="spam"', res)
158

  
159
    # see https://code.djangoproject.com/ticket/16754
160
    @expectedFailure
161
    def test_selectdatewidget(self):
162
        res = render_field('date', 'attr', 'foo:bar')
163
        assertIn('egg="spam"', res)
164
        assertIn('foo="bar"', res)
165

  
166
    def test_attr_chaining(self):
167
        res = render_field('with_attrs', 'attr', 'foo:bar', 'attr', 'bar:baz')
168
        assertIn('foo="bar"', res)
169
        assertNotIn('foo="baz"', res)
170
        assertIn('egg="spam"', res)
171
        assertIn('bar="baz"', res)
172

  
173
    def test_attr_class(self):
174
        res = render_field('with_cls', 'attr', 'foo:bar')
175
        assertIn('foo="bar"', res)
176
        assertIn('class="class0"', res)
177

  
178
    def test_default_attr(self):
179
        res = render_field('with_cls', 'attr', 'type:search')
180
        assertIn('class="class0"', res)
181
        assertIn('type="search"', res)
182

  
183
    def test_add_class(self):
184
        res = render_field('with_cls', 'add_class', 'class1')
185
        assertIn('class0', res)
186
        assertIn('class1', res)
187

  
188
    def test_add_class_chaining(self):
189
        res = render_field('with_cls', 'add_class', 'class1', 'add_class', 'class2')
190
        assertIn('class0', res)
191
        assertIn('class1', res)
192
        assertIn('class2', res)
193

  
194

  
195
class FieldReuseTest(TestCase):
196

  
197
    def test_field_double_rendering_simple(self):
198
        res = render_form('{{ form.simple }}{{ form.simple|attr:"foo:bar" }}{{ form.simple }}')
199
        self.assertEqual(res.count("bar"), 1)
200

  
201
    def test_field_double_rendering_simple_css(self):
202
        res = render_form('{{ form.simple }}{{ form.simple|add_class:"bar" }}{{ form.simple|add_class:"baz" }}')
203
        self.assertEqual(res.count("baz"), 1)
204
        self.assertEqual(res.count("bar"), 1)
205

  
206
    def test_field_double_rendering_attrs(self):
207
        res = render_form('{{ form.with_cls }}{{ form.with_cls|add_class:"bar" }}{{ form.with_cls }}')
208
        self.assertEqual(res.count("class0"), 3)
209
        self.assertEqual(res.count("bar"), 1)
210

  
211

  
212
class SimpleRenderFieldTagTest(TestCase):
213
    def test_attr(self):
214
        res = render_field_from_tag('simple', 'foo="bar"')
215
        assertIn('type="text"', res)
216
        assertIn('name="simple"', res)
217
        assertIn('id="id_simple"', res)
218
        assertIn('foo="bar"', res)
219

  
220
    def test_multiple_attrs(self):
221
        res = render_field_from_tag('simple', 'foo="bar"', 'bar="baz"')
222
        assertIn('type="text"', res)
223
        assertIn('name="simple"', res)
224
        assertIn('id="id_simple"', res)
225
        assertIn('foo="bar"', res)
226
        assertIn('bar="baz"', res)
227

  
228

  
229
class RenderFieldTagSilenceTest(TestCase):
230
    def test_silence_without_field(self):
231
        res = render_field_from_tag("nothing", 'foo="bar"')
232
        self.assertEqual(res, "")
233
        res = render_field_from_tag("nothing", 'class+="some"')
234
        self.assertEqual(res, "")
235

  
236

  
237
class RenderFieldTagCustomizedWidgetTest(TestCase):
238
    def test_attr(self):
239
        res = render_field_from_tag('with_attrs', 'foo="bar"')
240
        assertIn('foo="bar"', res)
241
        assertNotIn('foo="baz"', res)
242
        assertIn('egg="spam"', res)
243

  
244
    # see https://code.djangoproject.com/ticket/16754
245
    @expectedFailure
246
    def test_selectdatewidget(self):
247
        res = render_field_from_tag('date', 'foo="bar"')
248
        assertIn('egg="spam"', res)
249
        assertIn('foo="bar"', res)
250

  
251
    def test_multiple_attrs(self):
252
        res = render_field_from_tag('with_attrs', 'foo="bar"', 'bar="baz"')
253
        assertIn('foo="bar"', res)
254
        assertNotIn('foo="baz"', res)
255
        assertIn('egg="spam"', res)
256
        assertIn('bar="baz"', res)
257

  
258
    def test_attr_class(self):
259
        res = render_field_from_tag('with_cls', 'foo="bar"')
260
        assertIn('foo="bar"', res)
261
        assertIn('class="class0"', res)
262

  
263
    def test_default_attr(self):
264
        res = render_field_from_tag('with_cls', 'type="search"')
265
        assertIn('class="class0"', res)
266
        assertIn('type="search"', res)
267

  
268
    def test_append_attr(self):
269
        res = render_field_from_tag('with_cls', 'class+="class1"')
270
        assertIn('class0', res)
271
        assertIn('class1', res)
272

  
273
    def test_duplicate_append_attr(self):
274
        res = render_field_from_tag('with_cls', 'class+="class1"', 'class+="class2"')
275
        assertIn('class0', res)
276
        assertIn('class1', res)
277
        assertIn('class2', res)
278

  
279
    def test_hyphenated_attributes(self):
280
        res = render_field_from_tag('with_cls', 'data-foo="bar"')
281
        assertIn('data-foo="bar"', res)
282
        assertIn('class="class0"', res)
283

  
284

  
285
class RenderFieldWidgetClassesTest(TestCase):
286
    def test_use_widget_required_class(self):
287
        res = render_form('{% render_field form.simple %}',
288
                          WIDGET_REQUIRED_CLASS='required_class')
289
        assertIn('class="required_class"', res)
290

  
291
    def test_use_widget_error_class(self):
292
        res = render_form('{% render_field form.simple %}', form=MyForm({}),
293
                          WIDGET_ERROR_CLASS='error_class')
294
        assertIn('class="error_class"', res)
295

  
296
    def test_use_widget_error_class_with_other_classes(self):
297
        res = render_form('{% render_field form.simple class="blue" %}',
298
                          form=MyForm({}), WIDGET_ERROR_CLASS='error_class')
299
        assertIn('class="blue error_class"', res)
300

  
301
    def test_use_widget_required_class_with_other_classes(self):
302
        res = render_form('{% render_field form.simple class="blue" %}',
303
                          form=MyForm({}), WIDGET_REQUIRED_CLASS='required_class')
304
        assertIn('class="blue required_class"', res)
305

  
306

  
307
class RenderFieldTagFieldReuseTest(TestCase):
308
    def test_field_double_rendering_simple(self):
309
        res = render_form('{{ form.simple }}{% render_field form.simple foo="bar" %}{% render_field form.simple %}')
310
        self.assertEqual(res.count("bar"), 1)
311

  
312
    def test_field_double_rendering_simple_css(self):
313
        res = render_form('{% render_field form.simple %}{% render_field form.simple class+="bar" %}{% render_field form.simple class+="baz" %}')
314
        self.assertEqual(res.count("baz"), 1)
315
        self.assertEqual(res.count("bar"), 1)
316

  
317
    def test_field_double_rendering_attrs(self):
318
        res = render_form('{% render_field form.with_cls %}{% render_field form.with_cls class+="bar" %}{% render_field form.with_cls %}')
319
        self.assertEqual(res.count("class0"), 3)
320
        self.assertEqual(res.count("bar"), 1)
321

  
322

  
323
class RenderFieldTagUseTemplateVariableTest(TestCase):
324
    def test_use_template_variable_in_parametrs(self):
325
        res = render_form('{% render_field form.with_attrs egg+="pahaz" placeholder=form.with_attrs.label %}')
326
        assertIn('egg="spam pahaz"', res)
327
        assertIn('placeholder="With attrs"', res)
328

  
329

  
330
class RenderFieldFilter_field_type_widget_type_Test(TestCase):
331
    def test_field_type_widget_type_rendering_simple(self):
332
        res = render_form('<div class="{{ form.simple|field_type }} {{ form.simple|widget_type }} {{ form.simple.html_name }}">{{ form.simple }}</div>')
333
        assertIn('class="charfield textinput simple"', res)

Also available in: Unified diff