Reputation: 21288
In the project manage app I'm working on it should be possible to edit/delete a ticket if you are the owner of (i.e. the creator of) the ticket and/or the admin of the project the ticket belongs to.
In the template for showing a project I want to use a custom filter to determine this, used as seen here:
{% if ticket|owner_or_admin:user %}
<p>
<a href="{% url ticket_edit project.id %}">Edit</a>
<a id="delete_link" href="{% url ticket_delete ticket.id %}">Delete</a>
</p>
{% endif %}
Below is a try of creating this custom filter, but this throws an error ('owner_or_admin requires 2 arguments, 1 provided'):
@register.filter(name='owner_or_admin')
def ownership(ticket, project, user):
if ticket.user == user:
return true;
else:
if project.added_by_user == user:
return true
return false
models:
class Project(models.Model):
... fields ...
added_by_user = models.ForeignKey(User)
class Ticket(models.Model):
... fields ...
user = models.ForeignKey(User)
So, how do I provide two arguments? Is the custom filter correct otherwise?
Thanks in advance!
Upvotes: 2
Views: 3396
Reputation: 50205
You can't pass multiple arguments to a template filter according to the docs, but you could use two filters instead.
Template:
{% if ticket|owner:user or project|admin:user %}
<!-- blah -->
{% endif %}
Filters:
@register.filter(name='owner')
def ownership(ticket, user):
return ticket.user == user
@register.filter(name='admin')
def adminship(project, user):
return project.added_by_user == user
(EDIT: also, your booleans in the filter code should be capitalized)
Upvotes: 3