[Django] #32099: Admin action decorators

37 views
Skip to first unread message

Django

unread,
Oct 9, 2020, 12:28:50 PM10/9/20
to django-...@googlegroups.com
#32099: Admin action decorators
-------------------------------------+-------------------------------------
Reporter: Michal | Owner: nobody
Dabski |
Type: | Status: new
Cleanup/optimization |
Component: | Version: master
contrib.admin | Keywords:
Severity: Normal | admin,actions,decorator
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
-------------------------------------+-------------------------------------
Currently django admin actions can be customized by adding arbitrary
properties to the action method or function (allowed_permissions ,
short_description). This method is not very discoverable, and rather un-
puthonic.

Proposed solution:
Django should provide a built-in decorator that wrap action callable and
adds any properties passed to the decorator. Because decorator can define
available properties in it's signature, this method will be less error-
prone and will make it easier to discover properties available to admin
actions.
As an added bonus, a decorator could maybe even add the action to the
`actions` list without the user having to do it explicitly.

Example:

{{{
@admin.register(models.Document)
class DocumentAdmin(ModelAdmin):
actions = 'publish',

def publish(self, request, qs):
qs.update(publish=True)
publish.short_description = _('publish document')
publish.allowed_permissions =[ 'change']
}}}

could become:

{{{
@admin.register(models.Document)
class DocumentAdmin(ModelAdmin):
actions = 'publish',

@action(short_description=_('publish document'),
allowed_permissions=['change'])
def publish(self, request, qs):
qs.update(publish=True)
}}}

This functionality is implemented by a third-party library, but it has not
been updated in 9 years:
https://github.com/kmike/django-admin-decorators

--
Ticket URL: <https://code.djangoproject.com/ticket/32099>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Oct 9, 2020, 12:39:10 PM10/9/20
to django-...@googlegroups.com
#32099: Admin action decorators
-------------------------------------+-------------------------------------
Reporter: Michal Dabski | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: contrib.admin | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage:
admin,actions,decorator | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Michal Dabski):

Proposed patch:
https://github.com/django/django/pull/13520

--
Ticket URL: <https://code.djangoproject.com/ticket/32099#comment:1>

Django

unread,
Jan 13, 2021, 11:19:46 AM1/13/21
to django-...@googlegroups.com
#32099: Admin action decorators
-------------------------------------+-------------------------------------
Reporter: Michal Dabski | Owner: nobody
Type: | Status: closed

Cleanup/optimization |
Component: contrib.admin | Version: master
Severity: Normal | Resolution: duplicate

Keywords: | Triage Stage:
admin,actions,decorator | Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0

Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by GitHub <noreply@…>):

In [changeset:"920448539631b52dcee53bd32a880abbc9de18bd" 9204485]:
{{{
#!CommitTicketReference repository=""
revision="920448539631b52dcee53bd32a880abbc9de18bd"
Fixed #16117 -- Added decorators for admin action and display functions.

Refs #25134, #32099.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/32099#comment:4>

Reply all
Reply to author
Forward
0 new messages