2

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!

4

1 回答 1

3

根据文档,您不能将多个参数传递给模板过滤器,但您可以使用两个过滤器。

模板:

{% if ticket|owner:user or project|admin:user %}
    <!-- blah -->
{% endif %}

过滤器:

@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

(编辑:另外,过滤器代码中的布尔值应该大写)

于 2013-03-01T23:06:05.753 回答