by_group_url.py 621 Bytes
Newer Older
1
2
3
from django import template
from django.core.urlresolvers import reverse

Arnaud Fabre's avatar
Arnaud Fabre committed
4
from representatives.models import Mandate, Group
Arnaud Fabre's avatar
Arnaud Fabre committed
5

6
7
8
9
10
register = template.Library()


@register.filter
def by_group_url(group):
Arnaud Fabre's avatar
Arnaud Fabre committed
11
12
    if isinstance(group, Mandate):
        group = group.group
Arnaud Fabre's avatar
Arnaud Fabre committed
13
14
15
16

    if not isinstance(group, Group):
        return ''
    
17
18
19
20
21
22
23
    kwargs = {'group_kind': group.kind}

    if group.abbreviation:
        kwargs['search'] = group.abbreviation
    else:
        kwargs['search'] = group.name

Arnaud Fabre's avatar
Arnaud Fabre committed
24
25
    kwargs['group_id'] = group.id
    
26
27
28
29
    return reverse(
        'legislature:representatives_by_group',
        kwargs=kwargs
    )