Skip to content

Commit 01c3926

Browse files
committed
Fixed django#10057 -- Ensured that the 'show_delete' context variable in the admin's change view actually controls the display of the delete button. Thanks to rajeesh for the report, to patcoll for the patch, and to David Gouldin for the test.
1 parent ea667ee commit 01c3926

File tree

6 files changed

+28
-4
lines changed

6 files changed

+28
-4
lines changed

django/contrib/admin/options.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -998,7 +998,6 @@ def add_view(self, request, form_url='', extra_context=None):
998998
'title': _('Add %s') % force_unicode(opts.verbose_name),
999999
'adminform': adminForm,
10001000
'is_popup': "_popup" in request.REQUEST,
1001-
'show_delete': False,
10021001
'media': media,
10031002
'inline_admin_formsets': inline_admin_formsets,
10041003
'errors': helpers.AdminErrorList(form, formsets),

django/contrib/admin/templatetags/admin_modify.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def submit_row(context):
3232
'onclick_attrib': (opts.get_ordered_objects() and change
3333
and 'onclick="submitOrderForm();"' or ''),
3434
'show_delete_link': (not is_popup and context['has_delete_permission']
35-
and (change or context['show_delete'])),
35+
and change and context.get('show_delete', True)),
3636
'show_save_as_new': not is_popup and change and save_as,
3737
'show_save_and_add_another': context['has_add_permission'] and
3838
not is_popup and (not save_as or context['add']),

tests/regressiontests/admin_views/admin.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
Album, Question, Answer, ComplexSortedPerson, PrePopulatedPostLargeSlug,
2828
AdminOrderedField, AdminOrderedModelMethod, AdminOrderedAdminMethod,
2929
AdminOrderedCallable, Report, Color2, UnorderedObject, MainPrepopulated,
30-
RelatedPrepopulated)
30+
RelatedPrepopulated, UndeletableObject)
3131

3232

3333
def callable_year(dt_value):
@@ -569,6 +569,11 @@ class UnorderedObjectAdmin(admin.ModelAdmin):
569569
list_per_page = 2
570570

571571

572+
class UndeletableObjectAdmin(admin.ModelAdmin):
573+
def change_view(self, *args, **kwargs):
574+
kwargs['extra_context'] = {'show_delete': False}
575+
return super(UndeletableObjectAdmin, self).change_view(*args, **kwargs)
576+
572577

573578
site = admin.AdminSite(name="admin")
574579
site.register(Article, ArticleAdmin)
@@ -616,6 +621,7 @@ class UnorderedObjectAdmin(admin.ModelAdmin):
616621
site.register(Report, ReportAdmin)
617622
site.register(MainPrepopulated, MainPrepopulatedAdmin)
618623
site.register(UnorderedObject, UnorderedObjectAdmin)
624+
site.register(UndeletableObject, UndeletableObjectAdmin)
619625

620626
# We intentionally register Promo and ChapterXtra1 but not Chapter nor ChapterXtra2.
621627
# That way we cover all four cases:

tests/regressiontests/admin_views/customadmin.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,4 @@ def queryset(self, request):
4848
site.register(models.Fabric, base_admin.FabricAdmin)
4949
site.register(models.ChapterXtra1, base_admin.ChapterXtra1Admin)
5050
site.register(User, UserLimitedAdmin)
51+
site.register(models.UndeletableObject, base_admin.UndeletableObjectAdmin)

tests/regressiontests/admin_views/models.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,3 +611,10 @@ class UnorderedObject(models.Model):
611611
"""
612612
name = models.CharField(max_length=255)
613613
bool = models.BooleanField(default=True)
614+
615+
class UndeletableObject(models.Model):
616+
"""
617+
Model whose show_delete in admin change_view has been disabled
618+
Refs #10057.
619+
"""
620+
name = models.CharField(max_length=255)

tests/regressiontests/admin_views/tests.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
FoodDelivery, RowLevelChangePermissionModel, Paper, CoverLetter, Story,
4242
OtherStory, ComplexSortedPerson, Parent, Child, AdminOrderedField,
4343
AdminOrderedModelMethod, AdminOrderedAdminMethod, AdminOrderedCallable,
44-
Report, MainPrepopulated, RelatedPrepopulated, UnorderedObject)
44+
Report, MainPrepopulated, RelatedPrepopulated, UnorderedObject,
45+
UndeletableObject)
4546

4647

4748
ERROR_MESSAGE = "Please enter the correct username and password \
@@ -588,6 +589,16 @@ def test_hide_change_password(self):
588589
self.assertFalse(reverse('admin:password_change') in response.content,
589590
msg='The "change password" link should not be displayed if a user does not have a usable password.')
590591

592+
def test_change_view_with_show_delete_extra_context(self):
593+
"""
594+
Ensured that the 'show_delete' context variable in the admin's change
595+
view actually controls the display of the delete button.
596+
Refs #10057.
597+
"""
598+
instance = UndeletableObject.objects.create(name='foo')
599+
response = self.client.get('/test_admin/%s/admin_views/undeletableobject/%d/' %
600+
(self.urlbit, instance.pk))
601+
self.assertNotContains(response, 'deletelink')
591602

592603
@override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',))
593604
class AdminViewFormUrlTest(TestCase):

0 commit comments

Comments
 (0)