Opened 7 years ago
Closed 7 years ago
#29711 closed Cleanup/optimization (fixed)
Add system check to ensure uniqueness of admin actions' __name__.
| Reported by: | Przemysław Buczkowski | Owned by: | Przemysław Buczkowski |
|---|---|---|---|
| Component: | contrib.admin | Version: | dev |
| Severity: | Normal | Keywords: | |
| Cc: | Triage Stage: | Accepted | |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | yes |
| Easy pickings: | no | UI/UX: | no |
Description
It might be that I don't understand something, but I wrote this piece of code yesterday:
# admin.py
def action(fun):
def immediately(modeladmin, request, queryset):
result = fun(queryset)
if result:
modeladmin.message_user(request, result)
immediately.short_description = fun.short_description + " (now)"
def queue(modeladmin, request, queryset):
fun.delay(queryset)
modeladmin.message_user(request, "Added to the queue.")
queue.short_description = fun.short_description + " (add to the queue)"
return immediately, queue
class CompanyAdmin(admin.ModelAdmin):
actions = (*action(telephone), *action(mail))
# (...)
And only functions generated by the first call to action() get added. (In the case above two actions from "telephone" only; when reversed, from "mail" only).
I suspect that Django might detect them as duplicates.
Change History (11)
comment:1 by , 7 years ago
| Resolution: | → invalid |
|---|---|
| Status: | new → closed |
comment:2 by , 7 years ago
Yes, you're right, the opposite happens - I got a bit confused :)
Do you think that a warning should be added to Django docs? I can provide a patch to the docs since I don't think that this behaviour is obvious, even if it's intended - but maybe that's just me.
comment:3 by , 7 years ago
A system check could be added to ensure that every action in actions has a unique __name__ - since the __name__ collapsing into an OrderedDict isn't done until runtime.
comment:5 by , 7 years ago
| Summary: | One can use as actions functions generated only by the first call to another function → Add system check to ensure uniqueness of admin actions' __name__. |
|---|---|
| Triage Stage: | Unreviewed → Accepted |
| Type: | Uncategorized → Cleanup/optimization |
| Version: | 2.1 → master |
Yes OK. Przemysław, if you'd like to code that up, that would be great.
comment:7 by , 7 years ago
Just a quick heads-up that I'll be getting down to it, I'm just reading on this and friends in before :)
comment:9 by , 7 years ago
| Patch needs improvement: | set |
|---|
The issue occurs here in the `get_actions()` call:
# Convert the actions into an OrderedDict keyed by name. return OrderedDict( (name, (func, name, desc)) for func, name, desc in actions )Here you've got two pairs of
(func, name, desc)tuples keyed by theimmediatelyandqueuenames. Thus when they're passed to theOrderedDictconstructor, the keys conflict.This doesn't seem right. We should expect the opposite:
The `name` comes from the `__name__` attribute of the action function.
You should be able to adjust this to ensure unique names.
Perhaps something like:
immediately.__name__ = 'immediately_{}'.format(fun.__name__)... and so on.