summaryrefslogtreecommitdiff
path: root/django/newforms/widgets.py
blob: 2241a54c07e9bd25afc509d7a075d25d1b74f142 (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
"""
HTML Widget classes
"""

__all__ = ('Widget', 'TextInput', 'Textarea', 'CheckboxInput')

from django.utils.html import escape

# Converts a dictionary to a single string with key="value", XML-style.
# Assumes keys do not need to be XML-escaped.
flatatt = lambda attrs: ' '.join(['%s="%s"' % (k, escape(v)) for k, v in attrs.items()])

class Widget(object):
    def __init__(self, attrs=None):
        self.attrs = attrs or {}

    def render(self, name, value):
        raise NotImplementedError

class TextInput(Widget):
    def render(self, name, value, attrs=None):
        if value is None: value = ''
        final_attrs = dict(self.attrs, type='text', name=name)
        if attrs:
            final_attrs.update(attrs)
        if value != '': final_attrs['value'] = value # Only add the 'value' attribute if a value is non-empty.
        return u'<input %s />' % flatatt(final_attrs)

class Textarea(Widget):
    def render(self, name, value, attrs=None):
        if value is None: value = ''
        final_attrs = dict(self.attrs, name=name)
        if attrs:
            final_attrs.update(attrs)
        return u'<textarea %s>%s</textarea>' % (flatatt(final_attrs), escape(value))

class CheckboxInput(Widget):
    def render(self, name, value, attrs=None):
        final_attrs = dict(self.attrs, type='checkbox', name=name)
        if attrs:
            final_attrs.update(attrs)
        if value: final_attrs['checked'] = 'checked'
        return u'<input %s />' % flatatt(final_attrs)