Ticket #14454: admin_widgets.diff
File admin_widgets.diff, 16.9 KB (added by , 14 years ago) |
---|
-
tests/regressiontests/admin_widgets/models.py
diff --git a/tests/regressiontests/admin_widgets/models.py b/tests/regressiontests/admin_widgets/models.py index 8945054..6d51746 100644
a b 1 2 from django.conf import settings3 1 from django.db import models 4 from django.core.files.storage import default_storage5 2 from django.contrib.auth.models import User 6 3 7 4 class MyFileField(models.FileField): … … class CarTire(models.Model): 69 66 A single car tire. This to test that a user can only select their own cars. 70 67 """ 71 68 car = models.ForeignKey(Car) 72 73 __test__ = {'WIDGETS_TESTS': """74 >>> from datetime import datetime75 >>> from django.utils.html import escape, conditional_escape76 >>> from django.core.files.uploadedfile import SimpleUploadedFile77 >>> from django.contrib.admin.widgets import FilteredSelectMultiple, AdminSplitDateTime78 >>> from django.contrib.admin.widgets import AdminFileWidget, ForeignKeyRawIdWidget, ManyToManyRawIdWidget79 >>> from django.contrib.admin.widgets import RelatedFieldWidgetWrapper80 >>> from django.utils.translation import activate, deactivate81 >>> from django.conf import settings82 83 84 Calling conditional_escape on the output of widget.render will simulate what85 happens in the template. This is easier than setting up a template and context86 for each test.87 88 Make sure that the Admin widgets render properly, that is, without their extra89 HTML escaped.90 91 >>> w = FilteredSelectMultiple('test', False)92 >>> print conditional_escape(w.render('test', 'test'))93 <select multiple="multiple" name="test" class="selectfilter">94 </select><script type="text/javascript">addEvent(window, "load", function(e) {SelectFilter.init("id_test", "test", 0, "%(ADMIN_MEDIA_PREFIX)s"); });</script>95 <BLANKLINE>96 97 >>> w = FilteredSelectMultiple('test', True)98 >>> print conditional_escape(w.render('test', 'test'))99 <select multiple="multiple" name="test" class="selectfilterstacked">100 </select><script type="text/javascript">addEvent(window, "load", function(e) {SelectFilter.init("id_test", "test", 1, "%(ADMIN_MEDIA_PREFIX)s"); });</script>101 <BLANKLINE>102 103 >>> w = AdminSplitDateTime()104 >>> print conditional_escape(w.render('test', datetime(2007, 12, 1, 9, 30)))105 <p class="datetime">Date: <input value="2007-12-01" type="text" class="vDateField" name="test_0" size="10" /><br />Time: <input value="09:30:00" type="text" class="vTimeField" name="test_1" size="8" /></p>106 >>> activate('de-at')107 >>> settings.USE_L10N = True108 >>> w.is_localized = True109 >>> print conditional_escape(w.render('test', datetime(2007, 12, 1, 9, 30)))110 <p class="datetime">Datum: <input value="01.12.2007" type="text" class="vDateField" name="test_0" size="10" /><br />Zeit: <input value="09:30:00" type="text" class="vTimeField" name="test_1" size="8" /></p>111 >>> deactivate()112 >>> settings.USE_L10N = False113 114 >>> band = Band.objects.create(pk=1, name='Linkin Park')115 >>> album = band.album_set.create(name='Hybrid Theory', cover_art=r'albums\hybrid_theory.jpg')116 117 >>> w = AdminFileWidget()118 >>> print conditional_escape(w.render('test', album.cover_art))119 <p class="file-upload">Currently: <a target="_blank" href="%(STORAGE_URL)salbums/hybrid_theory.jpg">albums\hybrid_theory.jpg</a> <span class="clearable-file-input"><input type="checkbox" name="test-clear" id="test-clear_id" /> <label for="test-clear_id">Clear</label></span><br />Change: <input type="file" name="test" /></p>120 >>> print conditional_escape(w.render('test', SimpleUploadedFile('test', 'content')))121 <input type="file" name="test" />122 123 >>> rel = Album._meta.get_field('band').rel124 >>> w = ForeignKeyRawIdWidget(rel)125 >>> print conditional_escape(w.render('test', band.pk, attrs={}))126 <input type="text" name="test" value="1" class="vForeignKeyRawIdAdminField" /><a href="../../../admin_widgets/band/?t=id" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a> <strong>Linkin Park</strong>127 128 >>> m1 = Member.objects.create(pk=1, name='Chester')129 >>> m2 = Member.objects.create(pk=2, name='Mike')130 >>> band.members.add(m1, m2)131 132 >>> rel = Band._meta.get_field('members').rel133 >>> w = ManyToManyRawIdWidget(rel)134 >>> print conditional_escape(w.render('test', [m1.pk, m2.pk], attrs={}))135 <input type="text" name="test" value="1,2" class="vManyToManyRawIdAdminField" /><a href="../../../admin_widgets/member/" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a>136 >>> print conditional_escape(w.render('test', [m1.pk]))137 <input type="text" name="test" value="1" class="vManyToManyRawIdAdminField" /><a href="../../../admin_widgets/member/" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a>138 >>> w._has_changed(None, None)139 False140 >>> w._has_changed([], None)141 False142 >>> w._has_changed(None, [u'1'])143 True144 >>> w._has_changed([1, 2], [u'1', u'2'])145 False146 >>> w._has_changed([1, 2], [u'1'])147 True148 >>> w._has_changed([1, 2], [u'1', u'3'])149 True150 151 # Check that ForeignKeyRawIdWidget works with fields which aren't related to152 # the model's primary key.153 >>> apple = Inventory.objects.create(barcode=86, name='Apple')154 >>> pear = Inventory.objects.create(barcode=22, name='Pear')155 >>> core = Inventory.objects.create(barcode=87, name='Core', parent=apple)156 >>> rel = Inventory._meta.get_field('parent').rel157 >>> w = ForeignKeyRawIdWidget(rel)158 >>> print w.render('test', core.parent_id, attrs={})159 <input type="text" name="test" value="86" class="vForeignKeyRawIdAdminField" /><a href="../../../admin_widgets/inventory/?t=barcode" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a> <strong>Apple</strong>160 161 # see #9258162 >>> hidden = Inventory.objects.create(barcode=93, name='Hidden', hidden=True)163 >>> child_of_hidden = Inventory.objects.create(barcode=94, name='Child of hidden', parent=hidden)164 >>> print w.render('test', child_of_hidden.parent_id, attrs={})165 <input type="text" name="test" value="93" class="vForeignKeyRawIdAdminField" /><a href="../../../admin_widgets/inventory/?t=barcode" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a> <strong>Hidden</strong>166 """ % {167 'ADMIN_MEDIA_PREFIX': settings.ADMIN_MEDIA_PREFIX,168 'STORAGE_URL': default_storage.url(''),169 }} -
tests/regressiontests/admin_widgets/tests.py
diff --git a/tests/regressiontests/admin_widgets/tests.py b/tests/regressiontests/admin_widgets/tests.py index c445644..5340aa2 100644
a b 1 1 # encoding: utf-8 2 2 3 from datetime import datetime 4 from unittest import TestCase 5 3 6 from django import forms 7 from django.conf import settings 4 8 from django.contrib import admin 5 9 from django.contrib.admin import widgets 6 from unittest import TestCase 7 from django.test import TestCase as DjangoTestCase 10 from django.contrib.admin.widgets import FilteredSelectMultiple, AdminSplitDateTime 11 from django.contrib.admin.widgets import AdminFileWidget, ForeignKeyRawIdWidget, ManyToManyRawIdWidget 12 from django.contrib.admin.widgets import RelatedFieldWidgetWrapper 13 from django.core.files.storage import default_storage 14 from django.core.files.uploadedfile import SimpleUploadedFile 8 15 from django.db.models import DateField 16 from django.utils.html import conditional_escape 17 from django.utils.translation import activate, deactivate 18 from django.test import TestCase as DjangoTestCase 19 9 20 import models 10 21 22 11 23 class AdminFormfieldForDBFieldTests(TestCase): 12 24 """ 13 25 Tests for correct behavior of ModelAdmin.formfield_for_dbfield … … class AdminFormfieldForDBFieldTests(TestCase): 104 116 def testInheritance(self): 105 117 self.assertFormfield(models.Album, 'backside_art', widgets.AdminFileWidget) 106 118 119 107 120 class AdminFormfieldForDBFieldWithRequestTests(DjangoTestCase): 108 121 fixtures = ["admin-widgets-users.xml"] 109 122 … … class AdminFormfieldForDBFieldWithRequestTests(DjangoTestCase): 116 129 self.assert_("BMW M3" not in response.content) 117 130 self.assert_("Volkswagon Passat" in response.content) 118 131 132 119 133 class AdminForeignKeyWidgetChangeList(DjangoTestCase): 120 134 fixtures = ["admin-widgets-users.xml"] 121 135 admin_root = '/widget_admin' … … class AdminForeignKeyWidgetChangeList(DjangoTestCase): 130 144 response = self.client.get('%s/admin_widgets/car/' % self.admin_root) 131 145 self.failUnless('%s/auth/user/add/' % self.admin_root in response.content) 132 146 147 133 148 class AdminForeignKeyRawIdWidget(DjangoTestCase): 134 149 fixtures = ["admin-widgets-users.xml"] 135 150 admin_root = '/widget_admin' … … class AdminForeignKeyRawIdWidget(DjangoTestCase): 163 178 164 179 self.assertContains(response, 165 180 'Select a valid choice. That choice is not one of the available choices.') 181 182 183 class FilteredSelectMultipleWidgetTest(DjangoTestCase): 184 185 def test_render(self): 186 w = FilteredSelectMultiple('test', False) 187 self.assertEqual(conditional_escape(w.render('test', 'test')), 188 '<select multiple="multiple" name="test" class="selectfilter">\n</select><script type="text/javascript">addEvent(window, "load", function(e) {SelectFilter.init("id_test", "test", 0, "%(ADMIN_MEDIA_PREFIX)s"); });</script>\n' % { "ADMIN_MEDIA_PREFIX": settings.ADMIN_MEDIA_PREFIX }) 189 190 def test_stacked_render(self): 191 w = FilteredSelectMultiple('test', True) 192 self.assertEqual(conditional_escape(w.render('test', 'test')), 193 '<select multiple="multiple" name="test" class="selectfilterstacked">\n</select><script type="text/javascript">addEvent(window, "load", function(e) {SelectFilter.init("id_test", "test", 1, "%(ADMIN_MEDIA_PREFIX)s"); });</script>\n' % { "ADMIN_MEDIA_PREFIX": settings.ADMIN_MEDIA_PREFIX }) 194 195 196 class AdminSplitDateTimeWidgetTest(DjangoTestCase): 197 198 def test_render(self): 199 w = AdminSplitDateTime() 200 self.assertEqual( 201 conditional_escape(w.render('test', datetime(2007, 12, 1, 9, 30))), 202 '<p class="datetime">Date: <input value="2007-12-01" type="text" class="vDateField" name="test_0" size="10" /><br />Time: <input value="09:30:00" type="text" class="vTimeField" name="test_1" size="8" /></p>', 203 ) 204 205 def test_localization(self): 206 w = AdminSplitDateTime() 207 208 activate('de-at') 209 settings.USE_L10N = True 210 w.is_localized = True 211 self.assertEqual( 212 conditional_escape(w.render('test', datetime(2007, 12, 1, 9, 30))), 213 '<p class="datetime">Datum: <input value="01.12.2007" type="text" class="vDateField" name="test_0" size="10" /><br />Zeit: <input value="09:30:00" type="text" class="vTimeField" name="test_1" size="8" /></p>', 214 ) 215 deactivate() 216 settings.USE_L10N = False 217 218 219 class AdminFileWidgetTest(DjangoTestCase): 220 221 def test_render(self): 222 band = models.Band.objects.create(name='Linkin Park') 223 album = band.album_set.create(name='Hybrid Theory', 224 cover_art=r'albums\hybrid_theory.jpg') 225 226 w = AdminFileWidget() 227 self.assertEqual( 228 conditional_escape(w.render('test', album.cover_art)), 229 '<p class="file-upload">Currently: <a target="_blank" href="%(STORAGE_URL)salbums/hybrid_theory.jpg">albums\hybrid_theory.jpg</a> <span class="clearable-file-input"><input type="checkbox" name="test-clear" id="test-clear_id" /> <label for="test-clear_id">Clear</label></span><br />Change: <input type="file" name="test" /></p>' % { 'STORAGE_URL': default_storage.url('') }, 230 ) 231 232 self.assertEqual( 233 conditional_escape(w.render('test', SimpleUploadedFile('test', 'content'))), 234 '<input type="file" name="test" />', 235 ) 236 237 238 class ForeignKeyRawIdWidgetTest(DjangoTestCase): 239 240 def test_render(self): 241 band = models.Band.objects.create(name='Linkin Park') 242 album = band.album_set.create(name='Hybrid Theory', 243 cover_art=r'albums\hybrid_theory.jpg') 244 rel = models.Album._meta.get_field('band').rel 245 246 w = ForeignKeyRawIdWidget(rel) 247 self.assertEqual( 248 conditional_escape(w.render('test', band.pk, attrs={})), 249 '<input type="text" name="test" value="%(bandpk)s" class="vForeignKeyRawIdAdminField" /><a href="../../../admin_widgets/band/?t=id" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a> <strong>Linkin Park</strong>' % { "ADMIN_MEDIA_PREFIX": settings.ADMIN_MEDIA_PREFIX, "bandpk": band.pk }, 250 ) 251 252 def test_relations_to_non_primary_key(self): 253 # Check that ForeignKeyRawIdWidget works with fields which aren't 254 # related to the model's primary key. 255 apple = models.Inventory.objects.create(barcode=86, name='Apple') 256 pear = models.Inventory.objects.create(barcode=22, name='Pear') 257 core = models.Inventory.objects.create( 258 barcode=87, name='Core', parent=apple) 259 rel = models.Inventory._meta.get_field('parent').rel 260 w = ForeignKeyRawIdWidget(rel) 261 self.assertEqual(w.render('test', core.parent_id, attrs={}), 262 '<input type="text" name="test" value="86" class="vForeignKeyRawIdAdminField" /><a href="../../../admin_widgets/inventory/?t=barcode" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a> <strong>Apple</strong>' % { "ADMIN_MEDIA_PREFIX": settings.ADMIN_MEDIA_PREFIX }, 263 ) 264 265 266 def test_proper_manager_for_label_lookup(self): 267 # see #9258 268 rel = models.Inventory._meta.get_field('parent').rel 269 w = ForeignKeyRawIdWidget(rel) 270 271 hidden = models.Inventory.objects.create( 272 barcode=93, name='Hidden', hidden=True) 273 child_of_hidden = models.Inventory.objects.create( 274 barcode=94, name='Child of hidden', parent=hidden) 275 self.assertEqual( 276 w.render('test', child_of_hidden.parent_id, attrs={}), 277 '<input type="text" name="test" value="93" class="vForeignKeyRawIdAdminField" /><a href="../../../admin_widgets/inventory/?t=barcode" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a> <strong>Hidden</strong>' % { "ADMIN_MEDIA_PREFIX": settings.ADMIN_MEDIA_PREFIX }, 278 ) 279 280 281 class ManyToManyRawIdWidgetTest(DjangoTestCase): 282 283 def test_render(self): 284 band = models.Band.objects.create(name='Linkin Park') 285 album = band.album_set.create(name='Hybrid Theory', 286 cover_art=r'albums\hybrid_theory.jpg') 287 288 m1 = models.Member.objects.create(name='Chester') 289 m2 = models.Member.objects.create(name='Mike') 290 band.members.add(m1, m2) 291 rel = models.Band._meta.get_field('members').rel 292 293 w = ManyToManyRawIdWidget(rel) 294 self.assertEqual( 295 conditional_escape(w.render('test', [m1.pk, m2.pk], attrs={})), 296 '<input type="text" name="test" value="%(m1pk)s,%(m2pk)s" class="vManyToManyRawIdAdminField" /><a href="../../../admin_widgets/member/" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a>' % { "ADMIN_MEDIA_PREFIX": settings.ADMIN_MEDIA_PREFIX, "m1pk": m1.pk, "m2pk": m2.pk }, 297 ) 298 299 self.assertEqual(conditional_escape(w.render('test', [m1.pk])), 300 '<input type="text" name="test" value="%(m1pk)s" class="vManyToManyRawIdAdminField" /><a href="../../../admin_widgets/member/" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a>' % { "ADMIN_MEDIA_PREFIX": settings.ADMIN_MEDIA_PREFIX, "m1pk": m1.pk }, 301 ) 302 303 self.assertEqual(w._has_changed(None, None), False) 304 self.assertEqual(w._has_changed([], None), False) 305 self.assertEqual(w._has_changed(None, [u'1']), True) 306 self.assertEqual(w._has_changed([1, 2], [u'1', u'2']), False) 307 self.assertEqual(w._has_changed([1, 2], [u'1']), True) 308 self.assertEqual(w._has_changed([1, 2], [u'1', u'3']), True)