Django

Code

Show
Ignore:
Timestamp:
07/18/08 18:54:34 (6 months ago)
Author:
brosner
Message:

Merged the newforms-admin branch into trunk.

This is a backward incompatible change. The admin contrib app has been
refactored. The newforms module has several improvements including FormSets?
and Media definitions.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/tests/regressiontests/forms/forms.py

    r7814 r7967  
    16681668<input type="submit" /> 
    16691669</form> 
     1670 
     1671 
     1672# The empty_permitted attribute ############################################## 
     1673 
     1674Sometimes (pretty much in formsets) we want to allow a form to pass validation 
     1675if it is completely empty. We can accomplish this by using the empty_permitted 
     1676agrument to a form constructor. 
     1677 
     1678>>> class SongForm(Form): 
     1679...     artist = CharField() 
     1680...     name = CharField() 
     1681 
     1682First let's show what happens id empty_permitted=False (the default): 
     1683 
     1684>>> data = {'artist': '', 'song': ''} 
     1685 
     1686>>> form = SongForm(data, empty_permitted=False) 
     1687>>> form.is_valid() 
     1688False 
     1689>>> form.errors 
     1690{'name': [u'This field is required.'], 'artist': [u'This field is required.']} 
     1691>>> form.cleaned_data 
     1692Traceback (most recent call last): 
     1693... 
     1694AttributeError: 'SongForm' object has no attribute 'cleaned_data' 
     1695 
     1696 
     1697Now let's show what happens when empty_permitted=True and the form is empty. 
     1698 
     1699>>> form = SongForm(data, empty_permitted=True) 
     1700>>> form.is_valid() 
     1701True 
     1702>>> form.errors 
     1703{} 
     1704>>> form.cleaned_data 
     1705{} 
     1706 
     1707But if we fill in data for one of the fields, the form is no longer empty and 
     1708the whole thing must pass validation. 
     1709 
     1710>>> data = {'artist': 'The Doors', 'song': ''} 
     1711>>> form = SongForm(data, empty_permitted=False) 
     1712>>> form.is_valid() 
     1713False 
     1714>>> form.errors 
     1715{'name': [u'This field is required.']} 
     1716>>> form.cleaned_data 
     1717Traceback (most recent call last): 
     1718... 
     1719AttributeError: 'SongForm' object has no attribute 'cleaned_data' 
     1720 
     1721If a field is not given in the data then None is returned for its data. Lets 
     1722make sure that when checking for empty_permitted that None is treated 
     1723accordingly. 
     1724 
     1725>>> data = {'artist': None, 'song': ''} 
     1726>>> form = SongForm(data, empty_permitted=True) 
     1727>>> form.is_valid() 
     1728True 
     1729 
     1730However, we *really* need to be sure we are checking for None as any data in 
     1731initial that returns False on a boolean call needs to be treated literally. 
     1732 
     1733>>> class PriceForm(Form): 
     1734...     amount = FloatField() 
     1735...     qty = IntegerField() 
     1736 
     1737>>> data = {'amount': '0.0', 'qty': ''} 
     1738>>> form = PriceForm(data, initial={'amount': 0.0}, empty_permitted=True) 
     1739>>> form.is_valid() 
     1740True 
     1741 
    16701742""" 
  • django/trunk/tests/regressiontests/forms/tests.py

    r6843 r7967  
    2727from util import tests as util_tests 
    2828from widgets import tests as widgets_tests 
     29from formsets import tests as formset_tests 
     30from media import media_tests 
    2931 
    3032__test__ = { 
     
    5456    'localflavor_za_tests': localflavor_za_tests, 
    5557    'regression_tests': regression_tests, 
     58    'formset_tests': formset_tests, 
     59    'media_tests': media_tests, 
    5660    'util_tests': util_tests, 
    5761    'widgets_tests': widgets_tests, 
  • django/trunk/tests/regressiontests/forms/widgets.py

    r7693 r7967  
    203203u'<input type="file" class="fun" name="email" />' 
    204204 
     205Test for the behavior of _has_changed for FileInput. The value of data will 
     206more than likely come from request.FILES. The value of initial data will 
     207likely be a filename stored in the database. Since its value is of no use to 
     208a FileInput it is ignored. 
     209 
     210>>> w = FileInput() 
     211 
     212# No file was uploaded and no initial data. 
     213>>> w._has_changed(u'', None) 
     214False 
     215 
     216# A file was uploaded and no initial data. 
     217>>> w._has_changed(u'', {'filename': 'resume.txt', 'content': 'My resume'}) 
     218True 
     219 
     220# A file was not uploaded, but there is initial data 
     221>>> w._has_changed(u'resume.txt', None) 
     222False 
     223 
     224# A file was uploaded and there is initial data (file identity is not dealt 
     225# with here) 
     226>>> w._has_changed('resume.txt', {'filename': 'resume.txt', 'content': 'My resume'}) 
     227True 
     228 
    205229# Textarea Widget ############################################################# 
    206230 
     
    293317False 
    294318 
     319>>> w._has_changed(None, None) 
     320False 
     321>>> w._has_changed(None, u'') 
     322False 
     323>>> w._has_changed(u'', None) 
     324False 
     325>>> w._has_changed(u'', u'') 
     326False 
     327>>> w._has_changed(False, u'on') 
     328True 
     329>>> w._has_changed(True, u'on') 
     330False 
     331>>> w._has_changed(True, u'') 
     332True 
     333 
    295334# Select Widget ############################################################### 
    296335 
     
    573612>>> w.render('nums', ['ŠĐĆŽćžšđ'], choices=[('ŠĐĆŽćžšđ', 'ŠĐabcĆŽćžšđ'), ('ćžšđ', 'abcćžšđ')]) 
    574613u'<select multiple="multiple" name="nums">\n<option value="1">1</option>\n<option value="2">2</option>\n<option value="3">3</option>\n<option value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" selected="selected">\u0160\u0110abc\u0106\u017d\u0107\u017e\u0161\u0111</option>\n<option value="\u0107\u017e\u0161\u0111">abc\u0107\u017e\u0161\u0111</option>\n</select>' 
     614 
     615# Test the usage of _has_changed 
     616>>> w._has_changed(None, None) 
     617False 
     618>>> w._has_changed([], None) 
     619False 
     620>>> w._has_changed(None, [u'1']) 
     621True 
     622>>> w._has_changed([1, 2], [u'1', u'2']) 
     623False 
     624>>> w._has_changed([1, 2], [u'1']) 
     625True 
     626>>> w._has_changed([1, 2], [u'1', u'3']) 
     627True 
    575628 
    576629# RadioSelect Widget ########################################################## 
     
    872925</ul> 
    873926 
     927# Test the usage of _has_changed 
     928>>> w._has_changed(None, None) 
     929False 
     930>>> w._has_changed([], None) 
     931False 
     932>>> w._has_changed(None, [u'1']) 
     933True 
     934>>> w._has_changed([1, 2], [u'1', u'2']) 
     935False 
     936>>> w._has_changed([1, 2], [u'1']) 
     937True 
     938>>> w._has_changed([1, 2], [u'1', u'3']) 
     939True 
     940 
    874941# Unicode choices are correctly rendered as HTML 
    875942>>> w.render('nums', ['ŠĐĆŽćžšđ'], choices=[('ŠĐĆŽćžšđ', 'ŠĐabcĆŽćžšđ'), ('ćžšđ', 'abcćžšđ')]) 
     
    896963u'<input id="bar_0" type="text" class="big" value="john" name="name_0" /><br /><input id="bar_1" type="text" class="small" value="lennon" name="name_1" />' 
    897964 
     965>>> w = MyMultiWidget(widgets=(TextInput(), TextInput())) 
     966 
     967# test with no initial data 
     968>>> w._has_changed(None, [u'john', u'lennon']) 
     969True 
     970 
     971# test when the data is the same as initial 
     972>>> w._has_changed(u'john__lennon', [u'john', u'lennon']) 
     973False 
     974 
     975# test when the first widget's data has changed 
     976>>> w._has_changed(u'john__lennon', [u'alfred', u'lennon']) 
     977True 
     978 
     979# test when the last widget's data has changed. this ensures that it is not 
     980# short circuiting while testing the widgets. 
     981>>> w._has_changed(u'john__lennon', [u'john', u'denver']) 
     982True 
     983 
    898984# SplitDateTimeWidget ######################################################### 
    899985 
     
    9141000u'<input type="text" class="pretty" value="2006-01-10" name="date_0" /><input type="text" class="pretty" value="07:30:00" name="date_1" />' 
    9151001 
     1002>>> w._has_changed(datetime.datetime(2008, 5, 5, 12, 40, 00), [u'2008-05-05', u'12:40:00']) 
     1003False 
     1004>>> w._has_changed(datetime.datetime(2008, 5, 5, 12, 40, 00), [u'2008-05-05', u'12:41:00']) 
     1005True 
     1006 
    9161007# DateTimeInput ############################################################### 
    9171008