has_delete_permission not called in an admin template?

97 views
Skip to first unread message

rd-london

unread,
Nov 20, 2009, 10:18:50 AM11/20/09
to Django users
Hi,
Wonder if anyone can help.

Have following chunk in my own change_list.html template:

{% block object-tools %}
<ul class="object-tools">
{% if has_add_permission %}
<li>
<a href="add/{% if is_popup %}?_popup=1{% endif %}"
title="Add Comment" class="addlink">
{% blocktrans with cl.opts.verbose_name as name %}Add
{{ name }}{% endblocktrans %}
</a>
</li>
{% endif %}
{% if has_delete_permission %}
<li>
My thing goes here</li>
</li>
{% endif %}
</ul>

{% endblock %}

Created a user that has delete permissions. Got my own ModelAdmin and
have checked using the debugger that the user does indeed have delete
permissions.

The "def has_add_permission" fn in contrib/admin/options.py *is* being
called (I've put "import pdb;pdb.set_trace()" there to check), whereas
def has_delete_permission is not - and I can't work out for the life
of me why not.

Anyone help at all?

Thanks,
R






rd-london

unread,
Nov 20, 2009, 12:02:17 PM11/20/09
to Django users
Right, I've worked out why this is:

django/contrib/admin/options.py - def changelist_view(self, request,
extra_context=None): contains the line:

context = {
'title': cl.title,
'is_popup': cl.is_popup,
'cl': cl,
'media': media,
'has_add_permission': self.has_add_permission(request),
'root_path': self.admin_site.root_path,
'app_label': app_label,
'action_form': action_form,
'actions_on_top': self.actions_on_top,
'actions_on_bottom': self.actions_on_bottom,
}

So, it doesn't add "has_delete_permission" to the context (nor
"has_change_permission") for that matter. Simple fix:

context = {
'title': cl.title,
'is_popup': cl.is_popup,
'cl': cl,
'media': media,
'has_add_permission': self.has_add_permission(request),
addedhere ---> 'has_delete_permission':
self.has_delete_permission(request), <----- added here
'root_path': self.admin_site.root_path,
'app_label': app_label,
'action_form': action_form,
'actions_on_top': self.actions_on_top,
'actions_on_bottom': self.actions_on_bottom,
}

... but ... but ... surely that comes from the land of Harry the
Hacker?
Isn't there a better way to do this, or is this a genuine bug?

Any help or thoughts very welcome,

Cheers
R

rd-london

unread,
Nov 20, 2009, 12:22:50 PM11/20/09
to Django users
And ... yes there does seem to be a better way.

Define own version of "changelist_view" e.g.:

def changelist_view(self, request, extra_context=None):
extra_context={ 'has_delete_permission': self.has_delete_permission
(request) }
return super(MyCommentsAdmin, self).changelist_view(request,
extra_context)

However - as this post suggests - http://bit.ly/7ZrKHl - is this
thread safe?
Thanks for any thoughts anyone.

R

rd-london

unread,
Nov 23, 2009, 5:15:18 AM11/23/09
to Django users
OK, so my conclusion having examined the code listed at http://bit.ly/7ZrKHl
is that my solution is thread-safe. There's no if...else... type logic
happening in my solution so should be fine.

R


On Nov 20, 5:22 pm, rd-london <roland.d...@gmail.com> wrote:
> And ... yes there does seem to be a better way.
>
> Define own version of "changelist_view" e.g.:
>
>     def changelist_view(self, request, extra_context=None):
>         extra_context={ 'has_delete_permission': self.has_delete_permission
> (request) }
>         return super(MyCommentsAdmin, self).changelist_view(request,
> extra_context)
>
> However - as this post suggests -http://bit.ly/7ZrKHl- is this
Reply all
Reply to author
Forward
0 new messages