Skip to content

Commit

Permalink
Fixed #27991 -- Added 'obj' kwarg to InlineModelAdmin.has_add_permiss…
Browse files Browse the repository at this point in the history
…ion()
  • Loading branch information
icu0755 committed Apr 6, 2017
1 parent 12d0567 commit 99eba05
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
6 changes: 3 additions & 3 deletions django/contrib/admin/options.py
Expand Up @@ -534,11 +534,11 @@ def get_inline_instances(self, request, obj=None):
for inline_class in self.inlines:
inline = inline_class(self.model, self.admin_site)
if request:
if not (inline.has_add_permission(request) or
if not (inline.has_add_permission(request, obj) or
inline.has_change_permission(request, obj) or
inline.has_delete_permission(request, obj)):
continue
if not inline.has_add_permission(request):
if not inline.has_add_permission(request, obj):
inline.max_num = 0
inline_instances.append(inline)

Expand Down Expand Up @@ -1957,7 +1957,7 @@ def get_queryset(self, request):
queryset = queryset.none()
return queryset

def has_add_permission(self, request):
def has_add_permission(self, request, obj=None):
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
Expand Down
18 changes: 15 additions & 3 deletions docs/ref/contrib/admin/index.txt
Expand Up @@ -2053,9 +2053,6 @@ adds some of its own (the shared features are actually defined in the
- :meth:`~ModelAdmin.formfield_for_choice_field`
- :meth:`~ModelAdmin.formfield_for_foreignkey`
- :meth:`~ModelAdmin.formfield_for_manytomany`
- :meth:`~ModelAdmin.has_add_permission`
- :meth:`~ModelAdmin.has_change_permission`
- :meth:`~ModelAdmin.has_delete_permission`
- :meth:`~ModelAdmin.has_module_permission`

The ``InlineModelAdmin`` class adds:
Expand Down Expand Up @@ -2220,6 +2217,21 @@ The ``InlineModelAdmin`` class adds:
inline forms. For example, this may be based on the model instance
(passed as the keyword argument ``obj``).

.. method:: InlineModelAdmin.has_add_permission(self, request, obj=None)

The ``has_add_permission`` method is given the HttpRequest and the parent ``obj``.
Should return ``True`` if adding an instance is permitted, ``False`` otherwise.

.. method:: InlineModelAdmin.has_change_permission(self, request, obj=None)

The ``has_change_permission`` method is given the HttpRequest and the parent ``obj``.
Should return ``True`` if editing an instance is permitted, ``False`` otherwise.

.. method:: InlineModelAdmin.has_delete_permission(self, request, obj=None)

The ``has_delete_permission`` method is given the HttpRequest and the parent ``obj``.
Should return ``True`` if deleting an instance is permitted, ``False`` otherwise.

Working with a model with two or more foreign keys to the same parent model
---------------------------------------------------------------------------

Expand Down
26 changes: 25 additions & 1 deletion tests/modeladmin/tests.py
Expand Up @@ -617,7 +617,6 @@ def test_log_actions(self):


class ModelAdminPermissionTests(SimpleTestCase):

class MockUser:
def has_module_perms(self, app_label):
if app_label == "modeladmin":
Expand All @@ -642,6 +641,9 @@ def has_perm(self, perm):
return True
return False

def setUp(self):
self.site = AdminSite()

def test_has_add_permission(self):
"""
has_add_permission returns True for users who can add objects and
Expand Down Expand Up @@ -709,3 +711,25 @@ def test_has_module_permission(self):
self.assertFalse(ma.has_module_permission(request))
finally:
ma.opts.app_label = original_app_label

def test_inline_has_add_permission(self):
class InlineBandAdmin(TabularInline):
model = Concert
fk_name = 'main_band'
can_delete = False

def has_add_permission(self, request, obj=None):
return obj.name == 'The Doors'

ma = InlineBandAdmin(Band, self.site)
self.assertTrue(ma.has_add_permission(request, Band(
name='The Doors',
bio='',
sign_date=date(1965, 1, 1),
)))

self.assertFalse(ma.has_add_permission(request, Band(
name='The Beatles',
bio='',
sign_date=date(1965, 1, 1),
)))

0 comments on commit 99eba05

Please sign in to comment.