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

5
from legislature.models import MRepresentative, MGroup
luxcem's avatar
luxcem committed
6
7


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

14
    return _render_list(request, representative_list)
15
16


17
18
def representative_by_name(request, name):
    representative = get_object_or_404(
19
        MRepresentative, full_name=name)
20
21
22
23
24
25
26
27
    return render(
        request,
        'legislature/representative_view.html',
        {'representative': representative}
    )


def representative_view(request, num):
28
    representative = get_object_or_404(MRepresentative, pk=num)
29
30
31
32
33
34
35
36

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


37
38
39
40
41
42
43
def representatives_by_group(request, group_kind, group_abbr=None,
                             group_name=None, search=None):
    if group_abbr:
        representative_list = MRepresentative.objects.filter(
            mmandate__mgroup__abbreviation=group_abbr,
            mmandate__mgroup__kind=group_kind,
            mmandate__active=True
44
        )
luxcem's avatar
luxcem committed
45

46
47
48
49
50
    elif group_name:
        representative_list = MRepresentative.objects.filter(
            Q(mmandate__mgroup__name__icontains=group_name),
            mmandate__mgroup__kind=group_kind,
            mmandate__active=True
luxcem's avatar
luxcem committed
51
52
53
        )

    elif search:
luxcem's avatar
luxcem committed
54
        try:
55
56
57
58
59
            MGroup.objects.get(abbreviation=search, kind=group_kind)
            representative_list = MRepresentative.objects.filter(
                mmandate__mgroup__abbreviation=search,
                mmandate__mgroup__kind=group_kind,
                mmandate__active=True
luxcem's avatar
luxcem committed
60
            )
61
62
63
64
65
66
        except MGroup.DoesNotExist:
            representative_list = MRepresentative.objects.filter(
                Q(mmandate__mgroup__abbreviation__icontains=search) |
                Q(mmandate__mgroup__name__icontains=search),
                mmandate__mgroup__kind=group_kind,
                mmandate__active=True
luxcem's avatar
luxcem committed
67
            )
68

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

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

90

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

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

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

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


120
121
122
123
def groups_by_kind(request, kind):
    groups = MGroup.objects.filter(
        kind=kind,
        active=True
124
    )
125
126
127

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