tread
tread

Reputation: 11088

Showing and hiding menu items in django based on a non-permissions criteria?

I have a field on a Company called leave_approvers which is a ManyToManyField to Users.

The leave_approvers can approve leave of other users in the company they are a leave approver for. They also receive an email when leave is requested.

I would now like to show or hide the Approve Leave tab in the main layout based on whether the user is a leave_approver.

  1. Is the decision to have a leave_approver field flawed as I should be using the built in authorisation or something like django-guardian. Note that I am sending an email to the leave_approvers and that would mean

  2. Can I just make a query in the base.html to check if a user is a leave_approver. How can this be done and surely there is a performance hit?

Upvotes: 2

Views: 1728

Answers (1)

tread
tread

Reputation: 11088

After consideration making use of django permissions (which are added to the context via a context processor automatically) is the best route in my opinion. Using something like this in the template:

{% if perms.foo.can_vote %}

However, in my case I cut corners and make a query for all the leave approvers in the base template which is much slower than the prebuilt perms in the context.

{% if user in user.company.leave_approvers.all %}
    <li role="separator" class="divider"></li>
    <li><a href="{% url 'leave:pending' %}">Pending Leave Approval</a></li>
{% endif %}

Upvotes: 1

Related Questions