# -*- coding: utf-8 -*- tests = r""" >>> from django.forms import * >>> from django.utils.encoding import force_unicode >>> import datetime >>> import time >>> import re >>> from decimal import Decimal ############### # Extra stuff # ############### The forms library comes with some extra, higher-level Field and Widget classes that demonstrate some of the library's abilities. # SelectDateWidget ############################################################ >>> from django.forms.extras import SelectDateWidget >>> w = SelectDateWidget(years=('2007','2008','2009','2010','2011','2012','2013','2014','2015','2016')) >>> print w.render('mydate', '') >>> w.render('mydate', None) == w.render('mydate', '') True >>> print w.render('mydate', '2010-04-15') Accepts a datetime or a string: >>> w.render('mydate', datetime.date(2010, 4, 15)) == w.render('mydate', '2010-04-15') True Invalid dates still render the failed date: >>> print w.render('mydate', '2010-02-31') Using a SelectDateWidget in a form: >>> w = SelectDateWidget(years=('2007','2008','2009','2010','2011','2012','2013','2014','2015','2016'), required=False) >>> print w.render('mydate', '') >>> print w.render('mydate', '2010-04-15') >>> class GetDate(Form): ... mydate = DateField(widget=SelectDateWidget) >>> a = GetDate({'mydate_month':'4', 'mydate_day':'1', 'mydate_year':'2008'}) >>> print a.is_valid() True >>> print a.cleaned_data['mydate'] 2008-04-01 As with any widget that implements get_value_from_datadict, we must be prepared to accept the input from the "as_hidden" rendering as well. >>> print a['mydate'].as_hidden() >>> b=GetDate({'mydate':'2008-4-1'}) >>> print b.is_valid() True >>> print b.cleaned_data['mydate'] 2008-04-01 USE_L10N tests >>> from django.utils import translation >>> translation.activate('nl') >>> from django.conf import settings >>> settings.USE_L10N=True >>> w.value_from_datadict({'date_year': '2010', 'date_month': '8', 'date_day': '13'}, {}, 'date') '13-08-2010' >>> print w.render('date', '13-08-2010') >>> translation.deactivate() # MultiWidget and MultiValueField ############################################# # MultiWidgets are widgets composed of other widgets. They are usually # combined with MultiValueFields - a field that is composed of other fields. # MulitWidgets can themselved be composed of other MultiWidgets. # SplitDateTimeWidget is one example of a MultiWidget. >>> class ComplexMultiWidget(MultiWidget): ... def __init__(self, attrs=None): ... widgets = ( ... TextInput(), ... SelectMultiple(choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))), ... SplitDateTimeWidget(), ... ) ... super(ComplexMultiWidget, self).__init__(widgets, attrs) ... ... def decompress(self, value): ... if value: ... data = value.split(',') ... return [data[0], data[1], datetime.datetime(*time.strptime(data[2], "%Y-%m-%d %H:%M:%S")[0:6])] ... return [None, None, None] ... def format_output(self, rendered_widgets): ... return u'\n'.join(rendered_widgets) >>> w = ComplexMultiWidget() >>> print w.render('name', 'some text,JP,2007-04-25 06:24:00') >>> class ComplexField(MultiValueField): ... def __init__(self, required=True, widget=None, label=None, initial=None): ... fields = ( ... CharField(), ... MultipleChoiceField(choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))), ... SplitDateTimeField() ... ) ... super(ComplexField, self).__init__(fields, required, widget, label, initial) ... ... def compress(self, data_list): ... if data_list: ... return '%s,%s,%s' % (data_list[0],''.join(data_list[1]),data_list[2]) ... return None >>> f = ComplexField(widget=w) >>> f.clean(['some text', ['J','P'], ['2007-04-25','6:24:00']]) u'some text,JP,2007-04-25 06:24:00' >>> f.clean(['some text',['X'], ['2007-04-25','6:24:00']]) Traceback (most recent call last): ... ValidationError: [u'Select a valid choice. X is not one of the available choices.'] # If insufficient data is provided, None is substituted >>> f.clean(['some text',['JP']]) Traceback (most recent call last): ... ValidationError: [u'This field is required.'] >>> class ComplexFieldForm(Form): ... field1 = ComplexField(widget=w) >>> f = ComplexFieldForm() >>> print f >>> f = ComplexFieldForm({'field1_0':'some text','field1_1':['J','P'], 'field1_2_0':'2007-04-25', 'field1_2_1':'06:24:00'}) >>> print f >>> f.cleaned_data['field1'] u'some text,JP,2007-04-25 06:24:00' # IPAddressField ################################################################## >>> f = IPAddressField() >>> f.clean('') Traceback (most recent call last): ... ValidationError: [u'This field is required.'] >>> f.clean(None) Traceback (most recent call last): ... ValidationError: [u'This field is required.'] >>> f.clean('127.0.0.1') u'127.0.0.1' >>> f.clean('foo') Traceback (most recent call last): ... ValidationError: [u'Enter a valid IPv4 address.'] >>> f.clean('127.0.0.') Traceback (most recent call last): ... ValidationError: [u'Enter a valid IPv4 address.'] >>> f.clean('1.2.3.4.5') Traceback (most recent call last): ... ValidationError: [u'Enter a valid IPv4 address.'] >>> f.clean('256.125.1.5') Traceback (most recent call last): ... ValidationError: [u'Enter a valid IPv4 address.'] >>> f = IPAddressField(required=False) >>> f.clean('') u'' >>> f.clean(None) u'' >>> f.clean('127.0.0.1') u'127.0.0.1' >>> f.clean('foo') Traceback (most recent call last): ... ValidationError: [u'Enter a valid IPv4 address.'] >>> f.clean('127.0.0.') Traceback (most recent call last): ... ValidationError: [u'Enter a valid IPv4 address.'] >>> f.clean('1.2.3.4.5') Traceback (most recent call last): ... ValidationError: [u'Enter a valid IPv4 address.'] >>> f.clean('256.125.1.5') Traceback (most recent call last): ... ValidationError: [u'Enter a valid IPv4 address.'] ################################# # Tests of underlying functions # ################################# # smart_unicode tests >>> from django.utils.encoding import smart_unicode >>> class Test: ... def __str__(self): ... return 'ŠĐĆŽćžšđ' >>> class TestU: ... def __str__(self): ... return 'Foo' ... def __unicode__(self): ... return u'\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111' >>> smart_unicode(Test()) u'\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111' >>> smart_unicode(TestU()) u'\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111' >>> smart_unicode(1) u'1' >>> smart_unicode('foo') u'foo' #################################### # Test accessing errors in clean() # #################################### >>> class UserForm(Form): ... username = CharField(max_length=10) ... password = CharField(widget=PasswordInput) ... def clean(self): ... data = self.cleaned_data ... if not self.errors: ... data['username'] = data['username'].lower() ... return data >>> f = UserForm({'username': 'SirRobin', 'password': 'blue'}) >>> f.is_valid() True >>> f.cleaned_data['username'] u'sirrobin' ####################################### # Test overriding ErrorList in a form # ####################################### >>> from django.forms.util import ErrorList >>> class DivErrorList(ErrorList): ... def __unicode__(self): ... return self.as_divs() ... def as_divs(self): ... if not self: return u'' ... return u'
%s
' % ''.join([u'
%s
' % force_unicode(e) for e in self]) >>> class CommentForm(Form): ... name = CharField(max_length=50, required=False) ... email = EmailField() ... comment = CharField() >>> data = dict(email='invalid') >>> f = CommentForm(data, auto_id=False, error_class=DivErrorList) >>> print f.as_p()

Name:

Enter a valid e-mail address.

Email:

This field is required.

Comment:

################################# # Test multipart-encoded form # ################################# >>> class FormWithoutFile(Form): ... username = CharField() >>> class FormWithFile(Form): ... username = CharField() ... file = FileField() >>> class FormWithImage(Form): ... image = ImageField() >>> FormWithoutFile().is_multipart() False >>> FormWithFile().is_multipart() True >>> FormWithImage().is_multipart() True """