From 0a9b5d7adee86def16972debf6441deb96135ce2 Mon Sep 17 00:00:00 2001 From: Ramiro Morales Date: Sat, 26 Feb 2011 01:44:41 +0000 Subject: Fixed #15424 -- Corrected lookup of callables listed in admin inlines' `readonly_fields` by passing the right ModelAdmin (sub)class instance when instantiating inline forms admin wrappers. Also, added early validation of its elements. Thanks kmike for the report and Karen for the patch fixing the issue. git-svn-id: http://code.djangoproject.com/svn/django/trunk@15650 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- tests/regressiontests/admin_inlines/models.py | 40 ++++++++++++++++++++++++ tests/regressiontests/admin_inlines/tests.py | 19 +++++++++++ tests/regressiontests/admin_validation/models.py | 8 +++++ tests/regressiontests/admin_validation/tests.py | 16 +++++++--- 4 files changed, 78 insertions(+), 5 deletions(-) (limited to 'tests') diff --git a/tests/regressiontests/admin_inlines/models.py b/tests/regressiontests/admin_inlines/models.py index bb299f32b8..ee0abd1d3d 100644 --- a/tests/regressiontests/admin_inlines/models.py +++ b/tests/regressiontests/admin_inlines/models.py @@ -151,3 +151,43 @@ class TitleInline(admin.TabularInline): extra = 1 admin.site.register(TitleCollection, inlines=[TitleInline]) + +# Models for #15424 + +class Poll(models.Model): + name = models.CharField(max_length=40) + +class Question(models.Model): + poll = models.ForeignKey(Poll) + +class QuestionInline(admin.TabularInline): + model = Question + readonly_fields=['call_me'] + + def call_me(self, obj): + return 'Callable in QuestionInline' + +class PollAdmin(admin.ModelAdmin): + inlines = [QuestionInline] + + def call_me(self, obj): + return 'Callable in PollAdmin' + +class Novel(models.Model): + name = models.CharField(max_length=40) + +class Chapter(models.Model): + novel = models.ForeignKey(Novel) + +class ChapterInline(admin.TabularInline): + model = Chapter + readonly_fields=['call_me'] + + def call_me(self, obj): + return 'Callable in ChapterInline' + +class NovelAdmin(admin.ModelAdmin): + inlines = [ChapterInline] + +admin.site.register(Poll, PollAdmin) +admin.site.register(Novel, NovelAdmin) diff --git a/tests/regressiontests/admin_inlines/tests.py b/tests/regressiontests/admin_inlines/tests.py index 915c6fac8d..067b3c5eaf 100644 --- a/tests/regressiontests/admin_inlines/tests.py +++ b/tests/regressiontests/admin_inlines/tests.py @@ -84,6 +84,25 @@ class TestInline(TestCase): # Here colspan is "4": two fields (title1 and title2), one hidden field and the delete checkbock. self.assertContains(response, '') + def test_no_parent_callable_lookup(self): + """Admin inline `readonly_field` shouldn't invoke parent ModelAdmin callable""" + # Identically named callable isn't present in the parent ModelAdmin, + # rendering of the add view shouldn't explode + response = self.client.get('/test_admin/admin/admin_inlines/novel/add/') + self.assertEqual(response.status_code, 200) + # View should have the child inlines section + self.assertContains(response, '
') + + def test_callable_lookup(self): + """Admin inline should invoke local callable when its name is listed in readonly_fields""" + response = self.client.get('/test_admin/admin/admin_inlines/poll/add/') + self.assertEqual(response.status_code, 200) + # Add parent object view should have the child inlines section + self.assertContains(response, '
') + # The right callabe should be used for the inline readonly_fields + # column cells + self.assertContains(response, '

Callable in QuestionInline

') + class TestInlineMedia(TestCase): fixtures = ['admin-views-users.xml'] diff --git a/tests/regressiontests/admin_validation/models.py b/tests/regressiontests/admin_validation/models.py index 24387cc363..5e080a9232 100644 --- a/tests/regressiontests/admin_validation/models.py +++ b/tests/regressiontests/admin_validation/models.py @@ -45,3 +45,11 @@ class Book(models.Model): class AuthorsBooks(models.Model): author = models.ForeignKey(Author) book = models.ForeignKey(Book) + + +class State(models.Model): + name = models.CharField(max_length=15) + + +class City(models.Model): + state = models.ForeignKey(State) diff --git a/tests/regressiontests/admin_validation/tests.py b/tests/regressiontests/admin_validation/tests.py index 1872ca55e2..6fbdc8040e 100644 --- a/tests/regressiontests/admin_validation/tests.py +++ b/tests/regressiontests/admin_validation/tests.py @@ -4,7 +4,7 @@ from django.contrib.admin.validation import validate, validate_inline, \ ImproperlyConfigured from django.test import TestCase -from models import Song, Book, Album, TwoAlbumFKAndAnE +from models import Song, Book, Album, TwoAlbumFKAndAnE, State, City class SongForm(forms.ModelForm): pass @@ -162,6 +162,16 @@ class ValidationTestCase(TestCase): validate, SongAdmin, Song) + def test_nonexistant_field_on_inline(self): + class CityInline(admin.TabularInline): + model = City + readonly_fields=['i_dont_exist'] # Missing attribute + + self.assertRaisesMessage(ImproperlyConfigured, + "CityInline.readonly_fields[0], 'i_dont_exist' is not a callable or an attribute of 'CityInline' or found in the model 'City'.", + validate_inline, + CityInline, None, State) + def test_extra(self): class SongAdmin(admin.ModelAdmin): def awesome_song(self, instance): @@ -241,7 +251,3 @@ class ValidationTestCase(TestCase): fields = ['title', 'extra_data'] validate(FieldsOnFormOnlyAdmin, Song) - - - - -- cgit v1.3