oldviews.py 4.65 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# 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>

Arnaud Fabre's avatar
Arnaud Fabre committed
20 21
from datetime import datetime

Jamesie Pic's avatar
Jamesie Pic committed
22
from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator
23
from django.db.models import Q
Jamesie Pic's avatar
Jamesie Pic committed
24 25
from django.http import Http404
from django.shortcuts import get_object_or_404, render
luxcem's avatar
luxcem committed
26

Arnaud Fabre's avatar
Arnaud Fabre committed
27
from legislature.models import MemopolRepresentative
Jamesie Pic's avatar
Jamesie Pic committed
28 29
from representatives.models import Group

luxcem's avatar
luxcem committed
30

31 32 33 34 35
def retrieve(request, pk=None, name=None):
    if pk:
        representative = get_object_or_404(
            MemopolRepresentative,
            id=pk
Arnaud Fabre's avatar
Arnaud Fabre committed
36
        )
37 38 39 40 41 42 43
    elif name:
        representative = get_object_or_404(
            MemopolRepresentative,
            full_name=name
        )
    else:
        return Http404()
Jamesie Pic's avatar
Jamesie Pic committed
44

45 46 47 48 49 50
    return render(
        request,
        'legislature/representative_view.html',
        {'representative': representative}
    )

Jamesie Pic's avatar
Jamesie Pic committed
51

52
def representatives_by_group(request, group_kind, group_abbr=None,
Arnaud Fabre's avatar
Arnaud Fabre committed
53 54 55 56 57 58 59 60 61 62
                             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,
Jamesie Pic's avatar
Jamesie Pic committed
63
            mandates__end_date__gte=datetime.now()
64
        )
luxcem's avatar
luxcem committed
65

66
    elif group_name:
Arnaud Fabre's avatar
Arnaud Fabre committed
67 68 69
        representative_list = MemopolRepresentative.objects.filter(
            Q(mandates__group__name__icontains=group_name),
            mandates__group__kind=group_kind,
Jamesie Pic's avatar
Jamesie Pic committed
70
            mandates__end_date__gte=datetime.now()
luxcem's avatar
luxcem committed
71 72 73
        )

    elif search:
74
        try:
Arnaud Fabre's avatar
Arnaud Fabre committed
75 76 77 78
            Group.objects.get(abbreviation=search, kind=group_kind)
            representative_list = MemopolRepresentative.objects.filter(
                mandates__group__abbreviation=search,
                mandates__group__kind=group_kind,
Jamesie Pic's avatar
Jamesie Pic committed
79
                mandates__end_date__gte=datetime.now()
80
            )
Arnaud Fabre's avatar
Arnaud Fabre committed
81 82 83 84 85
        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,
Jamesie Pic's avatar
Jamesie Pic committed
86
                mandates__end_date__gte=datetime.now()
87
            )
88

89 90 91 92
    # Select distinct representatives and filter by search
    representative_list = list(set(
        _filter_by_search(request, representative_list)
    ))
93

94 95 96 97 98 99 100 101 102 103 104 105 106
    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)
        )
107
    else:
108
        return representative_list
109

110

111
def _render_list(request, representative_list, num_by_page=30):
112 113 114 115
    """
    Render a paginated list of representatives
    """
    paginator = Paginator(representative_list, num_by_page)
116 117 118 119 120 121 122 123
    page = request.GET.get('page')
    try:
        representatives = paginator.page(page)
    except PageNotAnInteger:
        representatives = paginator.page(1)
    except EmptyPage:
        representatives = paginator.page(paginator.num_pages)

124 125 126 127 128 129
    context = {}
    queries_without_page = request.GET.copy()
    if 'page' in queries_without_page:
        del queries_without_page['page']
    context['queries'] = queries_without_page

130
    context['representatives'] = representatives
131
    context['representative_num'] = paginator.count
132

133 134
    return render(
        request,
Arnaud Fabre's avatar
Arnaud Fabre committed
135
        'legislature/representative_list.html',
136 137 138 139
        context
    )


140
def groups_by_kind(request, kind):
Arnaud Fabre's avatar
Arnaud Fabre committed
141
    groups = Group.objects.filter(
142
        kind=kind,
143 144
        mandates__end_date__gte=datetime.now()
    ).distinct().order_by('name')
145 146 147

    return render(
        request,
148
        'legislature/groups_list.html',
149
        {'groups': groups}
150
    )