representative.py 3.31 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# coding: utf-8

# This file is part of memopol.
#
# memopol is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of
# the License, or any later version.
#
# memopol is distributed in the hope that it will
# be useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU General Affero Public
# License along with django-representatives.
# If not, see <http://www.gnu.org/licenses/>.
#
# Copyright (C) 2015 Arnaud Fabre <af@laquadrature.net>

from __future__ import absolute_import
from datetime import datetime

Arnaud Fabre's avatar
Arnaud Fabre committed
24
from django.shortcuts import render
25
26
27
28
from django.db.models import Q
from django.http import Http404

from ..models import MemopolRepresentative
29
from core.utils import render_paginate_list
Arnaud Fabre's avatar
Arnaud Fabre committed
30
from positions.forms import PositionForm
31

Arnaud Fabre's avatar
Arnaud Fabre committed
32

33
34
def index(request, group_kind=None, group=None):
    # Fetch active representatives
Arnaud Fabre's avatar
Arnaud Fabre committed
35
36
37
38
39
    representative_list = MemopolRepresentative.objects.select_related(
        'country',
        'main_mandate',
        'main_mandate__group',
    ).filter(
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
        active=True
    )
    # Filter the list by group if group information is provided
    if group_kind:
        if group.isnumeric():
            representative_list = representative_list.filter(
                mandates__group_id=int(group),
                mandates__end_date__gte=datetime.now()
            )
        else:
            # Search group based on abbreviation or name
            representative_list = representative_list.filter(
                Q(mandates__group__abbreviation=group) |
                Q(mandates__group__name=group),
                mandates__group__kind=group_kind,
                mandates__end_date__gte=datetime.now()
            )
Arnaud Fabre's avatar
Arnaud Fabre committed
57

58
59
60
61
62
63
64
65
66
67
    # Filter the list by search
    representative_list = _filter_by_search(
        request,
        representative_list
    ).order_by('-score', 'last_name')

    # Render the paginated template
    return render_paginate_list(
        request,
        representative_list,
Arnaud Fabre's avatar
Arnaud Fabre committed
68
        'legislature/representative_list.html'
69
70
71
    )

def detail(request, pk=None, name=None):
Arnaud Fabre's avatar
Arnaud Fabre committed
72
73
74
75
    try:
        query_set = MemopolRepresentative.objects.select_related(
            'country',
            'main_mandate'
76
        )
Arnaud Fabre's avatar
Arnaud Fabre committed
77
78
79
80
81
82
83
84
85
86
87
        if pk:
            representative = query_set.get(
                id=pk
            )
        elif name:
            representative = query_set.get(
                slug=name
            )
        else:
            return Http404()
    except MemopolRepresentative.DoesNotExist:
88
        return Http404()
Arnaud Fabre's avatar
Arnaud Fabre committed
89

Arnaud Fabre's avatar
Arnaud Fabre committed
90
    position_form = PositionForm()
91
92
93
    return render(
        request,
        'legislature/representative_detail.html',
Arnaud Fabre's avatar
Arnaud Fabre committed
94
95
96
97
        {
            'representative': representative,
            'position_form': position_form
        }
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
    )


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)
        )
    else:
        return representative_list