Ticket #9122: generic_inline_with_tests.diff

File generic_inline_with_tests.diff, 6.6 KB (added by Alex Robbins, 16 years ago)
  • django/contrib/contenttypes/generic.py

     
    345345        raise Exception("fk_name '%s' is not a ForeignKey to ContentType" % ct_field)
    346346    fk_field = opts.get_field(fk_field) # let the exception propagate
    347347    if exclude is not None:
     348        exclude = list(exclude) # make sure we have list not tuple
    348349        exclude.extend([ct_field.name, fk_field.name])
    349350    else:
    350351        exclude = [ct_field.name, fk_field.name]
     
    377378            "can_delete": True,
    378379            "can_order": False,
    379380            "fields": fields,
     381            "max_num": self.max_num,
     382            "exclude": self.exclude
    380383        }
    381384        return generic_inlineformset_factory(self.model, **defaults)
    382385
  • tests/regressiontests/generic_inline_admin/tests.py

     
    44from django.conf import settings
    55
    66# local test models
    7 from models import Episode, Media
     7from models import Episode, EpisodeExtra, EpisodeMaxNum, EpisodeExclude, Media
    88
    99class GenericAdminViewTest(TestCase):
    1010    fixtures = ['users.xml']
     
    7171        }
    7272        response = self.client.post('/generic_inline_admin/admin/generic_inline_admin/episode/1/', post_data)
    7373        self.failUnlessEqual(response.status_code, 302) # redirect somewhere
     74
     75class 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

     
    2020
    2121class MediaInline(generic.GenericTabularInline):
    2222    model = Media
    23     extra = 1
    2423   
    2524class EpisodeAdmin(admin.ModelAdmin):
    2625    inlines = [
    2726        MediaInline,
    2827    ]
     28admin.site.register(Episode, EpisodeAdmin)
    2929
    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####################
     35class EpisodeExtra(Episode):
     36    pass
     37
     38class MediaExtraInline(generic.GenericTabularInline):
     39    model = Media
     40    extra = 0
     41
     42class EpisodeExtraAdmin(admin.ModelAdmin):
     43    inlines = [
     44        MediaExtraInline,
     45    ]
     46admin.site.register(EpisodeExtra, EpisodeExtraAdmin)
     47
     48class EpisodeMaxNum(Episode):
     49    pass
     50
     51class MediaMaxNumInline(generic.GenericTabularInline):
     52    model = Media
     53    extra = 5
     54    max_num = 2
     55
     56class EpisodeMaxNumAdmin(admin.ModelAdmin):
     57    inlines = [
     58        MediaMaxNumInline,
     59    ]
     60admin.site.register(EpisodeMaxNum, EpisodeMaxNumAdmin)
     61
     62class EpisodeExclude(Episode):
     63    pass
     64
     65class MediaExcludeInline(generic.GenericTabularInline):
     66    model = Media
     67    exclude = ['url']
     68
     69class EpisodeExcludeAdmin(admin.ModelAdmin):
     70    inlines = [
     71        MediaExcludeInline,
     72    ]
     73admin.site.register(EpisodeExclude, EpisodeExcludeAdmin)
     74
  • AUTHORS

     
    335335    ricardojbarrios@gmail.com
    336336    Mike Richardson
    337337    Matt Riggott
     338    Alex Robbins <alexander.j.robbins@gmail.com>
    338339    Henrique Romano <onaiort@gmail.com>
    339340    Armin Ronacher
    340341    Daniel Roseman <http://roseman.org.uk/>
Back to Top