[2.1.x] Fixed #30097 -- Made 'obj' arg of InlineModelAdmin.has_add_permission() optional.
Restored backwards compatibility after refs #27991. Regression inbe6ca89396
. Backport of3c01fe30f3
from master.
This commit is contained in:
parent
7470595459
commit
3df13847d5
|
@ -2127,7 +2127,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
|
||||
|
|
|
@ -2417,7 +2417,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)
|
||||
|
||||
|
|
|
@ -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`).
|
||||
|
|
|
@ -712,6 +712,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'
|
||||
|
@ -771,6 +775,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
|
||||
|
|
Loading…
Reference in New Issue