# -*- coding: utf-8 -*-
tests = r"""
>>> from django.forms import *
>>> from django.forms.widgets import RadioFieldRenderer
>>> from django.utils.safestring import mark_safe
>>> from django.utils import formats
>>> import datetime
>>> import time
>>> import re
>>> from decimal import Decimal
>>> from django.utils.translation import activate, deactivate
>>> from django.conf import settings
###########
# 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''
'attrs' can be safe-strings if needed
>>> w = TextInput(attrs={'onBlur': mark_safe("function('foo')")})
>>> print w.render('email', '')
# PasswordInput Widget ############################################################
>>> w = PasswordInput()
>>> w.render('email', '')
u''
>>> w.render('email', None)
u''
>>> w.render('email', 'secret')
u''
The render_value argument lets you specify whether the widget should render
its value. For security reasons, this is off by default.
>>> w = PasswordInput(render_value=True)
>>> 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'}, render_value=True)
>>> 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'}, render_value=True)
>>> w.render('email', '', attrs={'class': 'special'})
u''
>>> w.render('email', 'ŠĐĆŽćžšđ', attrs={'class': 'fun'})
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''
Boolean values are rendered to their string forms ("True" and "False").
>>> w = HiddenInput()
>>> w.render('get_spam', False)
u''
>>> w.render('get_spam', True)
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''
Each input gets a separate ID.
>>> w = MultipleHiddenInput()
>>> w.render('letters', list('abc'), attrs={'id': 'hideme'})
u'\n\n'
# FileInput Widget ############################################################
FileInput widgets don't ever show the value, because the old value is of no use
if you are updating the form or if the provided file generated an error.
>>> 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''
Test for the behavior of _has_changed for FileInput. The value of data will
more than likely come from request.FILES. The value of initial data will
likely be a filename stored in the database. Since its value is of no use to
a FileInput it is ignored.
>>> w = FileInput()
# No file was uploaded and no initial data.
>>> w._has_changed(u'', None)
False
# A file was uploaded and no initial data.
>>> w._has_changed(u'', {'filename': 'resume.txt', 'content': 'My resume'})
True
# A file was not uploaded, but there is initial data
>>> w._has_changed(u'resume.txt', None)
False
# A file was uploaded and there is initial data (file identity is not dealt
# with here)
>>> w._has_changed('resume.txt', {'filename': 'resume.txt', 'content': 'My resume'})
True
# 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', mark_safe('pre "quoted" value'))
u''
>>> w.render('msg', 'value', attrs={'class': 'pretty', 'rows': 20})
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''
The CheckboxInput widget will return False if the key is not found in the data
dictionary (because HTML form submission doesn't send any result for unchecked
checkboxes).
>>> w.value_from_datadict({}, {}, 'testing')
False
>>> w._has_changed(None, None)
False
>>> w._has_changed(None, u'')
False
>>> w._has_changed(u'', None)
False
>>> w._has_changed(u'', u'')
False
>>> w._has_changed(False, u'on')
True
>>> w._has_changed(True, u'on')
False
>>> w._has_changed(True, u'')
True
# 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)])
# Choices are escaped correctly
>>> print w.render('escape', None, choices=(('bad', 'you & me'), ('good', mark_safe('you > me'))))
# Unicode choices are correctly rendered as HTML
>>> 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)
Choices can be nested one level in order to create HTML optgroups:
>>> w.choices=(('outer1', 'Outer 1'), ('Group "1"', (('inner1', 'Inner 1'), ('inner2', 'Inner 2'))))
>>> print w.render('nestchoice', None)
>>> print w.render('nestchoice', 'outer1')
>>> print w.render('nestchoice', 'inner1')
# 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')
>>> w._has_changed(False, None)
True
>>> w._has_changed(None, False)
True
>>> w._has_changed(None, None)
False
>>> w._has_changed(False, False)
False
>>> w._has_changed(True, False)
True
>>> w._has_changed(True, None)
True
>>> w._has_changed(True, True)
False
""" + \
r""" # [This concatenation is to keep the string below the jython's 32K limit].
# 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)])
# Choices are escaped correctly
>>> print w.render('escape', None, choices=(('bad', 'you & me'), ('good', mark_safe('you > me'))))
# Unicode choices are correctly rendered as HTML
>>> w.render('nums', ['ŠĐĆŽćžšđ'], choices=[('ŠĐĆŽćžšđ', 'ŠĐabcĆŽćžšđ'), ('ćžšđ', 'abcćžšđ')])
u''
# Test the usage of _has_changed
>>> w._has_changed(None, None)
False
>>> w._has_changed([], None)
False
>>> w._has_changed(None, [u'1'])
True
>>> w._has_changed([1, 2], [u'1', u'2'])
False
>>> w._has_changed([1, 2], [u'1'])
True
>>> w._has_changed([1, 2], [u'1', u'3'])
True
# Choices can be nested one level in order to create HTML optgroups:
>>> w.choices = (('outer1', 'Outer 1'), ('Group "1"', (('inner1', 'Inner 1'), ('inner2', 'Inner 2'))))
>>> print w.render('nestchoice', None)
>>> print w.render('nestchoice', ['outer1'])
>>> print w.render('nestchoice', ['inner1'])
>>> print w.render('nestchoice', ['outer1', 'inner2'])
# 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')])
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)])
RadioSelect uses a RadioFieldRenderer to render the individual radio inputs.
You can manipulate that object directly to customize the way the RadioSelect
is rendered.
>>> w = RadioSelect()
>>> r = w.get_renderer('beatle', 'J', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')))
>>> for inp in r:
... print inp
>>> for inp in r:
... print '%s ' % inp
>>> for inp in r:
... print '
%s %s
' % (inp.tag(), inp.choice_label)
John
Paul
George
Ringo
>>> for inp in r:
... print '%s %s %s %s %s' % (inp.name, inp.value, inp.choice_value, inp.choice_label, inp.is_checked())
beatle J J John True
beatle J P Paul False
beatle J G George False
beatle J R Ringo False
You can create your own custom renderers for RadioSelect to use.
>>> class MyRenderer(RadioFieldRenderer):
... def render(self):
... return u' \n'.join([unicode(choice) for choice in self])
>>> w = RadioSelect(renderer=MyRenderer)
>>> print w.render('beatle', 'G', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')))
Or you can use custom RadioSelect fields that use your custom renderer.
>>> class CustomRadioSelect(RadioSelect):
... renderer = MyRenderer
>>> w = CustomRadioSelect()
>>> print w.render('beatle', 'G', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')))
A RadioFieldRenderer object also allows index access to individual RadioInput
objects.
>>> w = RadioSelect()
>>> r = w.get_renderer('beatle', 'J', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')))
>>> print r[1]
>>> print r[0]
>>> r[0].is_checked()
True
>>> r[1].is_checked()
False
>>> r[1].name, r[1].value, r[1].choice_value, r[1].choice_label
('beatle', u'J', u'P', u'Paul')
>>> r[10] # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
IndexError: list index out of range
# Choices are escaped correctly
>>> w = RadioSelect()
>>> print w.render('escape', None, choices=(('bad', 'you & me'), ('good', mark_safe('you > me'))))
# Unicode choices are correctly rendered as HTML
>>> w = RadioSelect()
>>> unicode(w.render('email', 'ŠĐĆŽćžšđ', choices=[('ŠĐĆŽćžšđ', 'ŠĐabcĆŽćžšđ'), ('ćžšđ', 'abcćžšđ')]))
u'
\n\n\n
'
# Attributes provided at instantiation are passed to the constituent inputs
>>> w = RadioSelect(attrs={'id':'foo'})
>>> print w.render('beatle', 'J', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')))
# Attributes provided at render-time are passed to the constituent inputs
>>> w = RadioSelect()
>>> print w.render('beatle', 'J', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')), attrs={'id':'bar'})
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')])
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 = CheckboxSelectMultiple(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)])
# Test the usage of _has_changed
>>> w._has_changed(None, None)
False
>>> w._has_changed([], None)
False
>>> w._has_changed(None, [u'1'])
True
>>> w._has_changed([1, 2], [u'1', u'2'])
False
>>> w._has_changed([1, 2], [u'1'])
True
>>> w._has_changed([1, 2], [u'1', u'3'])
True
# Unicode choices are correctly rendered as HTML
>>> w.render('nums', ['ŠĐĆŽćžšđ'], choices=[('ŠĐĆŽćžšđ', 'ŠĐabcĆŽćžšđ'), ('ćžšđ', 'abcćžšđ')])
u'
\n\n\n\n\n\n
'
# Each input gets a separate ID
>>> print CheckboxSelectMultiple().render('letters', list('ac'), choices=zip(list('abc'), list('ABC')), attrs={'id': 'abc'})
# MultiWidget #################################################################
>>> class MyMultiWidget(MultiWidget):
... def decompress(self, value):
... if value:
... return value.split('__')
... return ['', '']
... def format_output(self, rendered_widgets):
... return u' '.join(rendered_widgets)
>>> w = MyMultiWidget(widgets=(TextInput(attrs={'class': 'big'}), TextInput(attrs={'class': 'small'})))
>>> w.render('name', ['john', 'lennon'])
u' '
>>> w.render('name', 'john__lennon')
u' '
>>> w.render('name', 'john__lennon', attrs={'id':'foo'})
u' '
>>> w = MyMultiWidget(widgets=(TextInput(attrs={'class': 'big'}), TextInput(attrs={'class': 'small'})), attrs={'id': 'bar'})
>>> w.render('name', ['john', 'lennon'])
u' '
>>> w = MyMultiWidget(widgets=(TextInput(), TextInput()))
# test with no initial data
>>> w._has_changed(None, [u'john', u'lennon'])
True
# test when the data is the same as initial
>>> w._has_changed(u'john__lennon', [u'john', u'lennon'])
False
# test when the first widget's data has changed
>>> w._has_changed(u'john__lennon', [u'alfred', u'lennon'])
True
# test when the last widget's data has changed. this ensures that it is not
# short circuiting while testing the widgets.
>>> w._has_changed(u'john__lennon', [u'john', u'denver'])
True
# SplitDateTimeWidget #########################################################
>>> w = SplitDateTimeWidget()
>>> w.render('date', '')
u''
>>> w.render('date', None)
u''
>>> w.render('date', datetime.datetime(2006, 1, 10, 7, 30))
u''
>>> w.render('date', [datetime.date(2006, 1, 10), datetime.time(7, 30)])
u''
You can also pass 'attrs' to the constructor. In this case, the attrs will be
included on both widgets.
>>> w = SplitDateTimeWidget(attrs={'class': 'pretty'})
>>> w.render('date', datetime.datetime(2006, 1, 10, 7, 30))
u''
Use 'date_format' and 'time_format' to change the way a value is displayed.
>>> w = SplitDateTimeWidget(date_format='%d/%m/%Y', time_format='%H:%M')
>>> w.render('date', datetime.datetime(2006, 1, 10, 7, 30))
u''
>>> w._has_changed(datetime.datetime(2008, 5, 6, 12, 40, 00), [u'2008-05-06', u'12:40:00'])
True
>>> w._has_changed(datetime.datetime(2008, 5, 6, 12, 40, 00), [u'06/05/2008', u'12:40'])
False
>>> w._has_changed(datetime.datetime(2008, 5, 6, 12, 40, 00), [u'06/05/2008', u'12:41'])
True
>>> activate('de-at')
>>> settings.USE_L10N = True
>>> w._has_changed(datetime.datetime(2008, 5, 6, 12, 40, 00), [u'06.05.2008', u'12:41'])
True
>>> deactivate()
>>> settings.USE_L10N = False
# DateTimeInput ###############################################################
>>> w = DateTimeInput()
>>> w.render('date', None)
u''
>>> d = datetime.datetime(2007, 9, 17, 12, 51, 34, 482548)
>>> print d
2007-09-17 12:51:34.482548
The microseconds are trimmed on display, by default.
>>> w.render('date', d)
u''
>>> w.render('date', datetime.datetime(2007, 9, 17, 12, 51, 34))
u''
>>> w.render('date', datetime.datetime(2007, 9, 17, 12, 51))
u''
>>> activate('de-at')
>>> settings.USE_L10N = True
>>> w.is_localized = True
>>> w.render('date', d)
u''
>>> deactivate()
>>> settings.USE_L10N = False
Use 'format' to change the way a value is displayed.
>>> w = DateTimeInput(format='%d/%m/%Y %H:%M')
>>> w.render('date', d)
u''
>>> w._has_changed(d, '17/09/2007 12:51')
False
Make sure a custom format works with _has_changed. The hidden input will use
format.localize_input to display the initial value.
>>> data = datetime.datetime(2010, 3, 6, 12, 0, 0)
>>> custom_format = '%d.%m.%Y %H:%M'
>>> w = DateTimeInput(format=custom_format)
>>> w._has_changed(formats.localize_input(data), data.strftime(custom_format))
False
# DateInput ###################################################################
>>> w = DateInput()
>>> w.render('date', None)
u''
>>> d = datetime.date(2007, 9, 17)
>>> print d
2007-09-17
>>> w.render('date', d)
u''
>>> w.render('date', datetime.date(2007, 9, 17))
u''
We should be able to initialize from a unicode value.
>>> w.render('date', u'2007-09-17')
u''
>>> activate('de-at')
>>> settings.USE_L10N = True
>>> w.is_localized = True
>>> w.render('date', d)
u''
>>> deactivate()
>>> settings.USE_L10N = False
Use 'format' to change the way a value is displayed.
>>> w = DateInput(format='%d/%m/%Y')
>>> w.render('date', d)
u''
>>> w._has_changed(d, '17/09/2007')
False
Make sure a custom format works with _has_changed. The hidden input will use
format.localize_input to display the initial value.
>>> data = datetime.date(2010, 3, 6)
>>> custom_format = '%d.%m.%Y'
>>> w = DateInput(format=custom_format)
>>> w._has_changed(formats.localize_input(data), data.strftime(custom_format))
False
# TimeInput ###################################################################
>>> w = TimeInput()
>>> w.render('time', None)
u''
>>> t = datetime.time(12, 51, 34, 482548)
>>> print t
12:51:34.482548
The microseconds are trimmed on display, by default.
>>> w.render('time', t)
u''
>>> w.render('time', datetime.time(12, 51, 34))
u''
>>> w.render('time', datetime.time(12, 51))
u''
We should be able to initialize from a unicode value.
>>> w.render('time', u'13:12:11')
u''
>>> activate('de-at')
>>> settings.USE_L10N = True
>>> w.is_localized = True
>>> w.render('date', d)
u''
>>> deactivate()
>>> settings.USE_L10N = False
Use 'format' to change the way a value is displayed.
>>> w = TimeInput(format='%H:%M')
>>> w.render('time', t)
u''
>>> w._has_changed(t, '12:51')
False
Make sure a custom format works with _has_changed. The hidden input will use
format.localize_input to display the initial value.
>>> data = datetime.time(13, 0)
>>> custom_format = '%I:%M %p'
>>> w = TimeInput(format=custom_format)
>>> w._has_changed(formats.localize_input(data), data.strftime(custom_format))
False
# SplitHiddenDateTimeWidget ###################################################
>>> from django.forms.widgets import SplitHiddenDateTimeWidget
>>> w = SplitHiddenDateTimeWidget()
>>> w.render('date', '')
u''
>>> d = datetime.datetime(2007, 9, 17, 12, 51, 34, 482548)
>>> print d
2007-09-17 12:51:34.482548
>>> w.render('date', d)
u''
>>> w.render('date', datetime.datetime(2007, 9, 17, 12, 51, 34))
u''
>>> w.render('date', datetime.datetime(2007, 9, 17, 12, 51))
u''
>>> activate('de-at')
>>> settings.USE_L10N = True
>>> w.is_localized = True
>>> w.render('date', datetime.datetime(2007, 9, 17, 12, 51))
u''
>>> deactivate()
>>> settings.USE_L10N = False
"""
from django.utils import copycompat as copy
from unittest import TestCase
from django import forms
class SelectAndTextWidget(forms.MultiWidget):
"""
MultiWidget subclass
"""
def __init__(self, choices=[]):
widgets = [
forms.RadioSelect(choices=choices),
forms.TextInput
]
super(SelectAndTextWidget, self).__init__(widgets)
def _set_choices(self, choices):
"""
When choices are set for this widget, we want to pass those along to the Select widget
"""
self.widgets[0].choices = choices
def _get_choices(self):
"""
The choices for this widget are the Select widget's choices
"""
return self.widgets[0].choices
choices = property(_get_choices, _set_choices)
class WidgetTests(TestCase):
def test_12048(self):
# See ticket #12048.
w1 = SelectAndTextWidget(choices=[1,2,3])
w2 = copy.deepcopy(w1)
w2.choices = [4,5,6]
# w2 ought to be independent of w1, since MultiWidget ought
# to make a copy of its sub-widgets when it is copied.
self.assertEqual(w1.choices, [1,2,3])