views.py 2.52 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 representatives.models import Representative
luxcem's avatar
luxcem committed
6 7


8 9 10 11 12 13 14 15 16 17 18 19 20
def index(request):
    context = {}
    if request.GET.get('search'):
        search = request.GET.get('search')
        representative_list = Representative.objects.filter(
            Q(full_name__icontains=search)
        )
        queries_without_page = request.GET.copy()
        if 'page' in queries_without_page:
            del queries_without_page['page']
        context['queries'] = queries_without_page
    else:
        representative_list = Representative.objects.all()
luxcem's avatar
luxcem committed
21

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
    paginator = Paginator(representative_list, 50)

    page = request.GET.get('page')
    try:
        representatives = paginator.page(page)
    except PageNotAnInteger:
        representatives = paginator.page(1)
    except EmptyPage:
        representatives = paginator.page(paginator.num_pages)

    context['representatives'] = representatives
    return render(
        request,
        'memopol_representatives/list.html',
        context
    )


def committee(request, committee):
    context = {}
    if request.GET.get('search'):
        search = request.GET.get('search')
        representative_list = Representative.objects.filter(
            mandate__group__kind='committee',
            mandate__group__abbreviation=committee
            ).filter(
                Q(full_name__icontains=search)
            )
        queries_without_page = request.GET.copy()
        if 'page' in queries_without_page:
            del queries_without_page['page']
        context['queries'] = queries_without_page
    else:
        representative_list = list(set(Representative.objects.filter(
            mandate__group__kind='committee',
            mandate__group__abbreviation=committee
        ).all()))

    paginator = Paginator(representative_list, 50)
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85

    page = request.GET.get('page')
    try:
        representatives = paginator.page(page)
    except PageNotAnInteger:
        representatives = paginator.page(1)
    except EmptyPage:
        representatives = paginator.page(paginator.num_pages)

    context['representatives'] = representatives
    return render(
        request,
        'memopol_representatives/list.html',
        context
    )


def view(request, num):
    representative = get_object_or_404(Representative, pk=num)

    return render(
        request,
        'memopol_representatives/view.html',
        {'representative': representative}
    )