# -*- coding: utf-8 -*- from localflavor import localflavor_tests form_tests = r""" >>> from django.newforms import * >>> import datetime >>> import re ########### # Widgets # ########### Each Widget class corresponds to an HTML form widget. A Widget knows how to render itself, given a field name and some data. Widgets don't perform validation. # TextInput Widget ############################################################ >>> w = TextInput() >>> w.render('email', '') u'' >>> w.render('email', None) u'' >>> w.render('email', 'test@example.com') u'' >>> w.render('email', 'some "quoted" & ampersanded value') u'' >>> w.render('email', 'test@example.com', attrs={'class': 'fun'}) u'' # Note that doctest in Python 2.4 (and maybe 2.5?) doesn't support non-ascii # characters in output, so we're displaying the repr() here. >>> w.render('email', 'ŠĐĆŽćžšđ', attrs={'class': 'fun'}) u'' You can also pass 'attrs' to the constructor: >>> w = TextInput(attrs={'class': 'fun'}) >>> w.render('email', '') u'' >>> w.render('email', 'foo@example.com') u'' 'attrs' passed to render() get precedence over those passed to the constructor: >>> w = TextInput(attrs={'class': 'pretty'}) >>> w.render('email', '', attrs={'class': 'special'}) u'' # PasswordInput Widget ############################################################ >>> w = PasswordInput() >>> w.render('email', '') u'' >>> w.render('email', None) u'' >>> w.render('email', 'test@example.com') u'' >>> w.render('email', 'some "quoted" & ampersanded value') u'' >>> w.render('email', 'test@example.com', attrs={'class': 'fun'}) u'' You can also pass 'attrs' to the constructor: >>> w = PasswordInput(attrs={'class': 'fun'}) >>> w.render('email', '') u'' >>> w.render('email', 'foo@example.com') u'' 'attrs' passed to render() get precedence over those passed to the constructor: >>> w = PasswordInput(attrs={'class': 'pretty'}) >>> w.render('email', '', attrs={'class': 'special'}) u'' >>> w.render('email', 'ŠĐĆŽćžšđ', attrs={'class': 'fun'}) u'' The render_value argument lets you specify whether the widget should render its value. You may want to do this for security reasons. >>> w = PasswordInput(render_value=True) >>> w.render('email', 'secret') u'' >>> w = PasswordInput(render_value=False) >>> w.render('email', '') u'' >>> w.render('email', None) u'' >>> w.render('email', 'secret') u'' >>> w = PasswordInput(attrs={'class': 'fun'}, render_value=False) >>> w.render('email', 'secret') u'' # HiddenInput Widget ############################################################ >>> w = HiddenInput() >>> w.render('email', '') u'' >>> w.render('email', None) u'' >>> w.render('email', 'test@example.com') u'' >>> w.render('email', 'some "quoted" & ampersanded value') u'' >>> w.render('email', 'test@example.com', attrs={'class': 'fun'}) u'' You can also pass 'attrs' to the constructor: >>> w = HiddenInput(attrs={'class': 'fun'}) >>> w.render('email', '') u'' >>> w.render('email', 'foo@example.com') u'' 'attrs' passed to render() get precedence over those passed to the constructor: >>> w = HiddenInput(attrs={'class': 'pretty'}) >>> w.render('email', '', attrs={'class': 'special'}) u'' >>> w.render('email', 'ŠĐĆŽćžšđ', attrs={'class': 'fun'}) u'' 'attrs' passed to render() get precedence over those passed to the constructor: >>> w = HiddenInput(attrs={'class': 'pretty'}) >>> w.render('email', '', attrs={'class': 'special'}) u'' # MultipleHiddenInput Widget ################################################## >>> w = MultipleHiddenInput() >>> w.render('email', []) u'' >>> w.render('email', None) u'' >>> w.render('email', ['test@example.com']) u'' >>> w.render('email', ['some "quoted" & ampersanded value']) u'' >>> w.render('email', ['test@example.com', 'foo@example.com']) u'\n' >>> w.render('email', ['test@example.com'], attrs={'class': 'fun'}) u'' >>> w.render('email', ['test@example.com', 'foo@example.com'], attrs={'class': 'fun'}) u'\n' You can also pass 'attrs' to the constructor: >>> w = MultipleHiddenInput(attrs={'class': 'fun'}) >>> w.render('email', []) u'' >>> w.render('email', ['foo@example.com']) u'' >>> w.render('email', ['foo@example.com', 'test@example.com']) u'\n' 'attrs' passed to render() get precedence over those passed to the constructor: >>> w = MultipleHiddenInput(attrs={'class': 'pretty'}) >>> w.render('email', ['foo@example.com'], attrs={'class': 'special'}) u'' >>> w.render('email', ['ŠĐĆŽćžšđ'], attrs={'class': 'fun'}) u'' 'attrs' passed to render() get precedence over those passed to the constructor: >>> w = MultipleHiddenInput(attrs={'class': 'pretty'}) >>> w.render('email', ['foo@example.com'], attrs={'class': 'special'}) u'' # FileInput Widget ############################################################ >>> w = FileInput() >>> w.render('email', '') u'' >>> w.render('email', None) u'' >>> w.render('email', 'test@example.com') u'' >>> w.render('email', 'some "quoted" & ampersanded value') u'' >>> w.render('email', 'test@example.com', attrs={'class': 'fun'}) u'' You can also pass 'attrs' to the constructor: >>> w = FileInput(attrs={'class': 'fun'}) >>> w.render('email', '') u'' >>> w.render('email', 'foo@example.com') u'' >>> w.render('email', 'ŠĐĆŽćžšđ', attrs={'class': 'fun'}) u'' # Textarea Widget ############################################################# >>> w = Textarea() >>> w.render('msg', '') u'' >>> w.render('msg', None) u'' >>> w.render('msg', 'value') u'' >>> w.render('msg', 'some "quoted" & ampersanded value') u'' >>> w.render('msg', 'value', attrs={'class': 'pretty'}) u'' You can also pass 'attrs' to the constructor: >>> w = Textarea(attrs={'class': 'pretty'}) >>> w.render('msg', '') u'' >>> w.render('msg', 'example') u'' 'attrs' passed to render() get precedence over those passed to the constructor: >>> w = Textarea(attrs={'class': 'pretty'}) >>> w.render('msg', '', attrs={'class': 'special'}) u'' >>> w.render('msg', 'ŠĐĆŽćžšđ', attrs={'class': 'fun'}) u'' # CheckboxInput Widget ######################################################## >>> w = CheckboxInput() >>> w.render('is_cool', '') u'' >>> w.render('is_cool', None) u'' >>> w.render('is_cool', False) u'' >>> w.render('is_cool', True) u'' Using any value that's not in ('', None, False, True) will check the checkbox and set the 'value' attribute. >>> w.render('is_cool', 'foo') u'' >>> w.render('is_cool', False, attrs={'class': 'pretty'}) u'' You can also pass 'attrs' to the constructor: >>> w = CheckboxInput(attrs={'class': 'pretty'}) >>> w.render('is_cool', '') u'' 'attrs' passed to render() get precedence over those passed to the constructor: >>> w = CheckboxInput(attrs={'class': 'pretty'}) >>> w.render('is_cool', '', attrs={'class': 'special'}) u'' You can pass 'check_test' to the constructor. This is a callable that takes the value and returns True if the box should be checked. >>> w = CheckboxInput(check_test=lambda value: value.startswith('hello')) >>> w.render('greeting', '') u'' >>> w.render('greeting', 'hello') u'' >>> w.render('greeting', 'hello there') u'' >>> w.render('greeting', 'hello & goodbye') u'' A subtlety: If the 'check_test' argument cannot handle a value and raises any exception during its __call__, then the exception will be swallowed and the box will not be checked. In this example, the 'check_test' assumes the value has a startswith() method, which fails for the values True, False and None. >>> w.render('greeting', True) u'' >>> w.render('greeting', False) u'' >>> w.render('greeting', None) u'' # Select Widget ############################################################### >>> w = Select() >>> print w.render('beatle', 'J', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))) If the value is None, none of the options are selected: >>> print w.render('beatle', None, choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))) If the value corresponds to a label (but not to an option value), none of the options are selected: >>> print w.render('beatle', 'John', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))) The value is compared to its str(): >>> print w.render('num', 2, choices=[('1', '1'), ('2', '2'), ('3', '3')]) >>> print w.render('num', '2', choices=[(1, 1), (2, 2), (3, 3)]) >>> print w.render('num', 2, choices=[(1, 1), (2, 2), (3, 3)]) The 'choices' argument can be any iterable: >>> from itertools import chain >>> def get_choices(): ... for i in range(5): ... yield (i, i) >>> print w.render('num', 2, choices=get_choices()) >>> things = ({'id': 1, 'name': 'And Boom'}, {'id': 2, 'name': 'One More Thing!'}) >>> class SomeForm(Form): ... somechoice = ChoiceField(choices=chain((('', '-'*9),), [(thing['id'], thing['name']) for thing in things])) >>> f = SomeForm() >>> f.as_table() u'' >>> f.as_table() u'' >>> f = SomeForm({'somechoice': 2}) >>> f.as_table() u'' You can also pass 'choices' to the constructor: >>> w = Select(choices=[(1, 1), (2, 2), (3, 3)]) >>> print w.render('num', 2) If 'choices' is passed to both the constructor and render(), then they'll both be in the output: >>> print w.render('num', 2, choices=[(4, 4), (5, 5)]) >>> w.render('email', 'ŠĐĆŽćžšđ', choices=[('ŠĐĆŽćžšđ', 'ŠĐabcĆŽćžšđ'), ('ćžšđ', 'abcćžšđ')]) u'' If choices is passed to the constructor and is a generator, it can be iterated over multiple times without getting consumed: >>> w = Select(choices=get_choices()) >>> print w.render('num', 2) >>> print w.render('num', 3) # NullBooleanSelect Widget #################################################### >>> w = NullBooleanSelect() >>> print w.render('is_cool', True) >>> print w.render('is_cool', False) >>> print w.render('is_cool', None) >>> print w.render('is_cool', '2') >>> print w.render('is_cool', '3') # SelectMultiple Widget ####################################################### >>> w = SelectMultiple() >>> print w.render('beatles', ['J'], choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))) >>> print w.render('beatles', ['J', 'P'], choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))) >>> print w.render('beatles', ['J', 'P', 'R'], choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))) If the value is None, none of the options are selected: >>> print w.render('beatles', None, choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))) If the value corresponds to a label (but not to an option value), none of the options are selected: >>> print w.render('beatles', ['John'], choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))) If multiple values are given, but some of them are not valid, the valid ones are selected: >>> print w.render('beatles', ['J', 'G', 'foo'], choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))) The value is compared to its str(): >>> print w.render('nums', [2], choices=[('1', '1'), ('2', '2'), ('3', '3')]) >>> print w.render('nums', ['2'], choices=[(1, 1), (2, 2), (3, 3)]) >>> print w.render('nums', [2], choices=[(1, 1), (2, 2), (3, 3)]) The 'choices' argument can be any iterable: >>> def get_choices(): ... for i in range(5): ... yield (i, i) >>> print w.render('nums', [2], choices=get_choices()) You can also pass 'choices' to the constructor: >>> w = SelectMultiple(choices=[(1, 1), (2, 2), (3, 3)]) >>> print w.render('nums', [2]) If 'choices' is passed to both the constructor and render(), then they'll both be in the output: >>> print w.render('nums', [2], choices=[(4, 4), (5, 5)]) >>> w.render('nums', ['ŠĐĆŽćžšđ'], choices=[('ŠĐĆŽćžšđ', 'ŠĐabcĆŽćžšđ'), ('ćžšđ', 'abcćžšđ')]) u'' # RadioSelect Widget ########################################################## >>> w = RadioSelect() >>> print w.render('beatle', 'J', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))) If the value is None, none of the options are checked: >>> print w.render('beatle', None, choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))) If the value corresponds to a label (but not to an option value), none of the options are checked: >>> print w.render('beatle', 'John', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))) The value is compared to its str(): >>> print w.render('num', 2, choices=[('1', '1'), ('2', '2'), ('3', '3')]) >>> print w.render('num', '2', choices=[(1, 1), (2, 2), (3, 3)]) >>> print w.render('num', 2, choices=[(1, 1), (2, 2), (3, 3)]) The 'choices' argument can be any iterable: >>> def get_choices(): ... for i in range(5): ... yield (i, i) >>> print w.render('num', 2, choices=get_choices()) You can also pass 'choices' to the constructor: >>> w = RadioSelect(choices=[(1, 1), (2, 2), (3, 3)]) >>> print w.render('num', 2) If 'choices' is passed to both the constructor and render(), then they'll both be in the output: >>> print w.render('num', 2, choices=[(4, 4), (5, 5)]) The render() method returns a RadioFieldRenderer object, whose str() is a