views.py 4.15 KB
Newer Older
Arnaud Fabre's avatar
Arnaud Fabre committed
1
2
from datetime import datetime

3
4
5
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
6

Arnaud Fabre's avatar
Arnaud Fabre committed
7
8
from representatives.models import Group
from legislature.models import MemopolRepresentative
luxcem's avatar
luxcem committed
9
10


11
def representatives_index(request):
12
13
    representative_list = _filter_by_search(
        request,
Arnaud Fabre's avatar
Arnaud Fabre committed
14
15
16
        MemopolRepresentative.objects.filter(
            active=True
        )
17
    )
Arnaud Fabre's avatar
Arnaud Fabre committed
18
    
19
    return _render_list(request, representative_list)
20
21


22
23
def representative_by_name(request, name):
    representative = get_object_or_404(
Arnaud Fabre's avatar
Arnaud Fabre committed
24
25
26
        MemopolRepresentative,
        full_name=name
    )
Arnaud Fabre's avatar
Arnaud Fabre committed
27
    
28
29
30
31
32
33
34
    return render(
        request,
        'legislature/representative_view.html',
        {'representative': representative}
    )


Arnaud Fabre's avatar
Arnaud Fabre committed
35
36
def representative_view(request, id):
    representative = get_object_or_404(MemopolRepresentative, pk=id)
37
38
39
40
41
42
43
    return render(
        request,
        'legislature/representative_view.html',
        {'representative': representative}
    )


44
def representatives_by_group(request, group_kind, group_abbr=None,
Arnaud Fabre's avatar
Arnaud Fabre committed
45
46
47
48
49
50
51
52
53
54
55
                             group_name=None, search=None, group_id=None):
    if group_id:
        representative_list = MemopolRepresentative.objects.filter(
            mandates__group_id=group_id,
            mandates__end_date__gte=datetime.now()
        )
    elif group_abbr:
        representative_list = MemopolRepresentative.objects.filter(
            mandates__group__abbreviation=group_abbr,
            mandates__group__kind=group_kind,
            mandates__end_date__gte = datetime.now()
56
        )
luxcem's avatar
luxcem committed
57

58
    elif group_name:
Arnaud Fabre's avatar
Arnaud Fabre committed
59
60
61
62
        representative_list = MemopolRepresentative.objects.filter(
            Q(mandates__group__name__icontains=group_name),
            mandates__group__kind=group_kind,
            mandates__end_date__gte = datetime.now()
luxcem's avatar
luxcem committed
63
64
65
        )

    elif search:
luxcem's avatar
luxcem committed
66
        try:
Arnaud Fabre's avatar
Arnaud Fabre committed
67
68
69
70
71
            Group.objects.get(abbreviation=search, kind=group_kind)
            representative_list = MemopolRepresentative.objects.filter(
                mandates__group__abbreviation=search,
                mandates__group__kind=group_kind,
                mandates__end_date__gte = datetime.now()
luxcem's avatar
luxcem committed
72
            )
Arnaud Fabre's avatar
Arnaud Fabre committed
73
74
75
76
77
78
        except Group.DoesNotExist:
            representative_list = MemopolRepresentative.objects.filter(
                Q(mandates__group__abbreviation__icontains=search) |
                Q(mandates__group__name__icontains=search),
                mandates__group__kind=group_kind,
                mandates__end_date__gte = datetime.now()
luxcem's avatar
luxcem committed
79
            )
80

81
82
83
84
    # Select distinct representatives and filter by search
    representative_list = list(set(
        _filter_by_search(request, representative_list)
    ))
85

86
87
88
89
90
91
92
93
94
95
96
97
98
    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)
        )
99
    else:
100
        return representative_list
101

102

103
def _render_list(request, representative_list, num_by_page=30):
104
105
106
107
    """
    Render a paginated list of representatives
    """
    paginator = Paginator(representative_list, num_by_page)
108
109
110
111
112
113
114
115
    page = request.GET.get('page')
    try:
        representatives = paginator.page(page)
    except PageNotAnInteger:
        representatives = paginator.page(1)
    except EmptyPage:
        representatives = paginator.page(paginator.num_pages)

116
117
118
119
120
121
    context = {}
    queries_without_page = request.GET.copy()
    if 'page' in queries_without_page:
        del queries_without_page['page']
    context['queries'] = queries_without_page

122
    context['representatives'] = representatives
123
    context['representative_num'] = paginator.count
luxcem's avatar
luxcem committed
124

125
126
    return render(
        request,
Arnaud Fabre's avatar
Arnaud Fabre committed
127
        'legislature/representative_list.html',
128
129
130
131
        context
    )


132
def groups_by_kind(request, kind):
Arnaud Fabre's avatar
Arnaud Fabre committed
133
    groups = Group.objects.filter(
134
        kind=kind,
Arnaud Fabre's avatar
Arnaud Fabre committed
135
        memopolgroup__active=True
136
    )
137
138
139

    return render(
        request,
140
        'legislature/groups_list.html',
141
        {'groups': groups}
142
    )