views.py 3.72 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

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


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

15
    return _render_list(request, representative_list)
16
17


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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):
40
    if mandate_abbr:
luxcem's avatar
luxcem committed
41
        representative_list = Representative.objects.filter(
42
            mandate__group__abbreviation=mandate_abbr,
luxcem's avatar
luxcem committed
43
            mandate__group__kind=mandate_kind,
44
45
            mandate__active=True
        )
luxcem's avatar
luxcem committed
46
47
48

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

    elif search:
luxcem's avatar
luxcem committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
        try:
            Group.objects.get(abbreviation=search, kind=mandate_kind)
            representative_list = Representative.objects.filter(
                mandate__group__abbreviation=search,
                mandate__group__kind=mandate_kind,
                mandate__active=True
            )
        except Group.DoesNotExist:
            representative_list = Representative.objects.filter(
                Q(mandate__group__abbreviation__icontains=search) |
                Q(mandate__group__name__icontains=search),
                mandate__group__kind=mandate_kind,
                mandate__active=True
            )
69

70
71
72
73
    # Select distinct representatives and filter by search
    representative_list = list(set(
        _filter_by_search(request, representative_list)
    ))
74

75
76
77
78
79
80
81
82
83
84
85
86
87
    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)
        )
88
    else:
89
        return representative_list
90

91
92
93
94
95
96

def _render_list(request, representative_list, num_by_page=50):
    """
    Render a paginated list of representatives
    """
    paginator = Paginator(representative_list, num_by_page)
97
98
99
100
101
102
103
104
    page = request.GET.get('page')
    try:
        representatives = paginator.page(page)
    except PageNotAnInteger:
        representatives = paginator.page(1)
    except EmptyPage:
        representatives = paginator.page(paginator.num_pages)

105
106
107
108
109
110
    context = {}
    queries_without_page = request.GET.copy()
    if 'page' in queries_without_page:
        del queries_without_page['page']
    context['queries'] = queries_without_page

111
    context['representatives'] = representatives
112
    context['representative_num'] = paginator.count
luxcem's avatar
luxcem committed
113

114
115
    return render(
        request,
116
        'legislature/representatives_list.html',
117
118
119
120
        context
    )


121
122
123
124
def group_by_kind(request, kind):
    groups = Group.objects.filter(
        kind=kind
    )
125
126
127

    return render(
        request,
128
129
        'legislature/group_list.html',
        {'groups': groups}
130
    )