Ticket #9122: generic_inline_with_tests.diff
File generic_inline_with_tests.diff, 6.6 KB (added by , 16 years ago) |
---|
-
django/contrib/contenttypes/generic.py
345 345 raise Exception("fk_name '%s' is not a ForeignKey to ContentType" % ct_field) 346 346 fk_field = opts.get_field(fk_field) # let the exception propagate 347 347 if exclude is not None: 348 exclude = list(exclude) # make sure we have list not tuple 348 349 exclude.extend([ct_field.name, fk_field.name]) 349 350 else: 350 351 exclude = [ct_field.name, fk_field.name] … … 377 378 "can_delete": True, 378 379 "can_order": False, 379 380 "fields": fields, 381 "max_num": self.max_num, 382 "exclude": self.exclude 380 383 } 381 384 return generic_inlineformset_factory(self.model, **defaults) 382 385 -
tests/regressiontests/generic_inline_admin/tests.py
4 4 from django.conf import settings 5 5 6 6 # local test models 7 from models import Episode, Media7 from models import Episode, EpisodeExtra, EpisodeMaxNum, EpisodeExclude, Media 8 8 9 9 class GenericAdminViewTest(TestCase): 10 10 fixtures = ['users.xml'] … … 71 71 } 72 72 response = self.client.post('/generic_inline_admin/admin/generic_inline_admin/episode/1/', post_data) 73 73 self.failUnlessEqual(response.status_code, 302) # redirect somewhere 74 75 class GenericInlineAdminParametersTest(TestCase): 76 fixtures = ['users.xml'] 77 78 def setUp(self): 79 self.client.login(username='super', password='secret') 80 81 # Can't load content via a fixture (since the GenericForeignKey 82 # relies on content type IDs, which will vary depending on what 83 # other tests have been run), thus we do it here. 84 test_classes = [ 85 Episode, 86 EpisodeExtra, 87 EpisodeMaxNum, 88 EpisodeExclude, 89 ] 90 for klass in test_classes: 91 e = klass.objects.create(name='This Week in Django') 92 m = Media(content_object=e, url='http://example.com/podcast.mp3') 93 m.save() 94 95 def tearDown(self): 96 self.client.logout() 97 98 def testNoParam(self): 99 # There is one INITIAL_FORM, and extra defaults to 3, so there 100 # should be 4 TOTAL_FORMS 101 inline_form_data = '<input type="hidden" name="generic_inline_admin-media-content_type-object_id-TOTAL_FORMS" value="4" id="id_generic_inline_admin-media-content_type-object_id-TOTAL_FORMS" /><input type="hidden" name="generic_inline_admin-media-content_type-object_id-INITIAL_FORMS" value="1" id="id_generic_inline_admin-media-content_type-object_id-INITIAL_FORMS" />' 102 response= self.client.get('/generic_inline_admin/admin/generic_inline_admin/episode/1/') 103 self.assertContains(response, inline_form_data) 104 105 def testExtraParam(self): 106 # Both TOTAL_FORMS and INITIAL_FORMS should be the same number 107 inline_form_data ='<input type="hidden" name="generic_inline_admin-media-content_type-object_id-TOTAL_FORMS" value="1" id="id_generic_inline_admin-media-content_type-object_id-TOTAL_FORMS" /><input type="hidden" name="generic_inline_admin-media-content_type-object_id-INITIAL_FORMS" value="1" id="id_generic_inline_admin-media-content_type-object_id-INITIAL_FORMS" />' 108 response= self.client.get('/generic_inline_admin/admin/generic_inline_admin/episodeextra/2/') 109 self.assertContains(response, inline_form_data) 110 111 def testMaxNumParam(self): 112 # Even though the extra param is set to 5, max_num should 113 # override that, keeping TOTAL_FORMS to 2. 114 inline_form_data = '<input type="hidden" name="generic_inline_admin-media-content_type-object_id-TOTAL_FORMS" value="2" id="id_generic_inline_admin-media-content_type-object_id-TOTAL_FORMS" /><input type="hidden" name="generic_inline_admin-media-content_type-object_id-INITIAL_FORMS" value="1" id="id_generic_inline_admin-media-content_type-object_id-INITIAL_FORMS" />' 115 response= self.client.get('/generic_inline_admin/admin/generic_inline_admin/episodemaxnum/3/') 116 self.assertContains(response, inline_form_data) 117 118 def testExcludeParam(self): 119 # Since we are excluding the url field, the form shouldn't have the 120 # url field. 121 response= self.client.get('/generic_inline_admin/admin/generic_inline_admin/episodeexclude/4/') 122 self.assertNotContains(response, 'url') -
tests/regressiontests/generic_inline_admin/models.py
20 20 21 21 class MediaInline(generic.GenericTabularInline): 22 22 model = Media 23 extra = 124 23 25 24 class EpisodeAdmin(admin.ModelAdmin): 26 25 inlines = [ 27 26 MediaInline, 28 27 ] 28 admin.site.register(Episode, EpisodeAdmin) 29 29 30 admin.site.register(Episode, EpisodeAdmin) 30 #################### 31 # These models let us test the different GenericInline settings at 32 # different urls in the admin site. There might be a better way to do this, 33 # but this works. 34 #################### 35 class EpisodeExtra(Episode): 36 pass 37 38 class MediaExtraInline(generic.GenericTabularInline): 39 model = Media 40 extra = 0 41 42 class EpisodeExtraAdmin(admin.ModelAdmin): 43 inlines = [ 44 MediaExtraInline, 45 ] 46 admin.site.register(EpisodeExtra, EpisodeExtraAdmin) 47 48 class EpisodeMaxNum(Episode): 49 pass 50 51 class MediaMaxNumInline(generic.GenericTabularInline): 52 model = Media 53 extra = 5 54 max_num = 2 55 56 class EpisodeMaxNumAdmin(admin.ModelAdmin): 57 inlines = [ 58 MediaMaxNumInline, 59 ] 60 admin.site.register(EpisodeMaxNum, EpisodeMaxNumAdmin) 61 62 class EpisodeExclude(Episode): 63 pass 64 65 class MediaExcludeInline(generic.GenericTabularInline): 66 model = Media 67 exclude = ['url'] 68 69 class EpisodeExcludeAdmin(admin.ModelAdmin): 70 inlines = [ 71 MediaExcludeInline, 72 ] 73 admin.site.register(EpisodeExclude, EpisodeExcludeAdmin) 74 -
AUTHORS
335 335 ricardojbarrios@gmail.com 336 336 Mike Richardson 337 337 Matt Riggott 338 Alex Robbins <alexander.j.robbins@gmail.com> 338 339 Henrique Romano <onaiort@gmail.com> 339 340 Armin Ronacher 340 341 Daniel Roseman <http://roseman.org.uk/>