views.py 3.34 KB
Newer Older
1
2
3
from django.shortcuts import render, get_object_or_404
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.db.models import Q
luxcem's avatar
luxcem committed
4

5
from representatives.models import Representative, Group
luxcem's avatar
luxcem committed
6
7


8
def representatives_index(request):
9
10
11
12
    representative_list = _filter_by_search(
        request,
        Representative.objects.all()
    )
luxcem's avatar
luxcem committed
13

14
    return _render_list(request, representative_list)
15
16


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
def representative_by_name(request, name):
    representative = get_object_or_404(
        Representative, full_name=name)
    return render(
        request,
        'legislature/representative_view.html',
        {'representative': representative}
    )


def representative_view(request, num):
    representative = get_object_or_404(Representative, pk=num)

    return render(
        request,
        'legislature/representative_view.html',
        {'representative': representative}
    )


def representatives_by_mandate(request, mandate_kind, mandate_abbr=None,
                               mandate_name=None, search=None):
39
    if mandate_abbr:
luxcem's avatar
luxcem committed
40
        representative_list = Representative.objects.filter(
41
            mandate__group__abbreviation=mandate_abbr,
luxcem's avatar
luxcem committed
42
            mandate__group__kind=mandate_kind,
43
44
            mandate__active=True
        )
luxcem's avatar
luxcem committed
45
46
47

    elif mandate_name:
        representative_list = Representative.objects.filter(
48
            Q(mandate__group__name__icontains=mandate_name),
luxcem's avatar
luxcem committed
49
50
51
52
53
54
55
56
57
            mandate__group__kind=mandate_kind,
            mandate__active=True
        )

    elif search:
        representative_list = Representative.objects.filter(
            Q(mandate__group__abbreviation__icontains=search) |
            Q(mandate__group__name__icontains=search),
            mandate__group__kind=mandate_kind,
58
59
            mandate__active=True
        )
60

61
62
63
64
    # Select distinct representatives and filter by search
    representative_list = list(set(
        _filter_by_search(request, representative_list)
    ))
65

66
67
68
69
70
71
72
73
74
75
76
77
78
    return _render_list(request, representative_list)


def _filter_by_search(request, representative_list):
    """
    Return a representative_list filtered by
    the representative name provided in search form
    """
    search = request.GET.get('search')
    if search:
        return representative_list.filter(
            Q(full_name__icontains=search)
        )
79
    else:
80
        return representative_list
81

82
83
84
85
86
87

def _render_list(request, representative_list, num_by_page=50):
    """
    Render a paginated list of representatives
    """
    paginator = Paginator(representative_list, num_by_page)
88
89
90
91
92
93
94
95
    page = request.GET.get('page')
    try:
        representatives = paginator.page(page)
    except PageNotAnInteger:
        representatives = paginator.page(1)
    except EmptyPage:
        representatives = paginator.page(paginator.num_pages)

96
97
98
99
100
101
    context = {}
    queries_without_page = request.GET.copy()
    if 'page' in queries_without_page:
        del queries_without_page['page']
    context['queries'] = queries_without_page

102
    context['representatives'] = representatives
103
    context['representative_num'] = paginator.count
104
105
    return render(
        request,
106
        'legislature/representatives_list.html',
107
108
109
110
        context
    )


111
112
113
114
def group_by_kind(request, kind):
    groups = Group.objects.filter(
        kind=kind
    )
115
116
117

    return render(
        request,
118
119
        'legislature/group_list.html',
        {'groups': groups}
120
    )