Fixed #30097 -- Made 'obj' arg of InlineModelAdmin.has_add_permission() optional.

Restored backwards compatibility after refs #27991.
Regression in be6ca89396.
This commit is contained in:
MaximZemskov 2019-01-11 13:13:31 +03:00 committed by Tim Graham
parent 8f4eee1777
commit 3c01fe30f3
4 changed files with 32 additions and 3 deletions

View File

@ -2126,7 +2126,8 @@ class InlineModelAdmin(BaseModelAdmin):
queryset = queryset.none()
return queryset
def has_add_permission(self, request, obj):
def has_add_permission(self, request, obj=None):
# RemovedInDjango31Warning: obj becomes a mandatory argument.
if self.opts.auto_created:
# We're checking the rights to an auto-created intermediate model,
# which doesn't have its own individual permissions. The user needs

View File

@ -2413,7 +2413,9 @@ The ``InlineModelAdmin`` class adds or customizes:
.. versionchanged:: 2.1
The ``obj`` argument was added.
The ``obj`` argument was added. During the deprecation period, it may
also be ``None`` if third-party calls to ``has_add_permission()`` don't
provide it.
.. method:: InlineModelAdmin.has_change_permission(request, obj=None)

View File

@ -9,4 +9,6 @@ Django 2.1.6 several bugs in 2.1.5.
Bugfixes
========
* ...
* Made the ``obj`` argument of ``InlineModelAdmin.has_add_permission()``
optional to restore backwards compatibility with third-party code that
doesn't provide it (:ticket:`30097`).

View File

@ -734,6 +734,10 @@ class ModelAdminPermissionTests(SimpleTestCase):
def has_perm(self, perm):
return perm == 'modeladmin.add_band'
class MockAddUserWithInline(MockUser):
def has_perm(self, perm):
return perm == 'modeladmin.add_concert'
class MockChangeUser(MockUser):
def has_perm(self, perm):
return perm == 'modeladmin.change_band'
@ -793,6 +797,26 @@ class ModelAdminPermissionTests(SimpleTestCase):
self.assertEqual(len(inline_instances), 1)
self.assertIsInstance(inline_instances[0], ConcertInline)
def test_inline_has_add_permission_without_obj(self):
# This test will be removed in Django 3.1 when `obj` becomes a required
# argument of has_add_permission() (#27991).
class ConcertInline(TabularInline):
model = Concert
def has_add_permission(self, request):
return super().has_add_permission(request)
class BandAdmin(ModelAdmin):
inlines = [ConcertInline]
ma = BandAdmin(Band, AdminSite())
request = MockRequest()
request.user = self.MockAddUserWithInline()
band = Band(name='The Doors', bio='', sign_date=date(1965, 1, 1))
inline_instances = ma.get_inline_instances(request, band)
self.assertEqual(len(inline_instances), 1)
self.assertIsInstance(inline_instances[0], ConcertInline)
def test_has_change_permission(self):
"""
has_change_permission returns True for users who can edit objects and