""" HTML Widget classes """ __all__ = ( 'Widget', 'TextInput', 'PasswordInput', 'HiddenInput', 'FileInput', 'Textarea', 'CheckboxInput', 'Select', 'SelectMultiple', 'RadioSelect', 'CheckboxSelectMultiple', ) from util import StrAndUnicode, smart_unicode from django.utils.datastructures import MultiValueDict from django.utils.html import escape from itertools import chain try: set # Only available in Python 2.4+ except NameError: from sets import Set as set # Python 2.3 fallback # Converts a dictionary to a single string with key="value", XML-style with # a leading space. Assumes keys do not need to be XML-escaped. flatatt = lambda attrs: u''.join([u' %s="%s"' % (k, escape(v)) for k, v in attrs.items()]) class Widget(object): is_hidden = False # Determines whether this corresponds to an . def __init__(self, attrs=None): self.attrs = attrs or {} def render(self, name, value, attrs=None): """ Returns this Widget rendered as HTML, as a Unicode string. The 'value' given is not guaranteed to be valid input, so subclass implementations should program defensively. """ raise NotImplementedError def build_attrs(self, extra_attrs=None, **kwargs): "Helper function for building an attribute dictionary." attrs = dict(self.attrs, **kwargs) if extra_attrs: attrs.update(extra_attrs) return attrs def value_from_datadict(self, data, name): """ Given a dictionary of data and this widget's name, returns the value of this widget. Returns None if it's not provided. """ return data.get(name, None) def id_for_label(self, id_): """ Returns the HTML ID attribute of this Widget for use by a