filters.py 3.02 KB
Newer Older
1 2
# coding: utf-8

3 4 5
import datetime

from django.db.models import Q
6 7
from django.utils.text import slugify

8
from django_filters import FilterSet, MethodFilter
9

10
from representatives.models import Representative
Nicolas Joyard's avatar
Nicolas Joyard committed
11
from representatives_votes.models import Dossier
12
from memopol_themes.models import Theme
13 14 15 16 17


class RepresentativeFilter(FilterSet):

    search = MethodFilter(action='search_filter')
Nicolas Joyard's avatar
Nicolas Joyard committed
18 19
    scoremin = MethodFilter(action='score_min_filter')
    scoremax = MethodFilter(action='score_max_filter')
20 21 22 23 24
    chamber = MethodFilter(action='chamber_filter')
    country = MethodFilter(action='group_filter')
    party = MethodFilter(action='group_filter')
    delegation = MethodFilter(action='group_filter')
    committee = MethodFilter(action='group_filter')
25

26 27
    class Meta:
        model = Representative
Nicolas Joyard's avatar
Nicolas Joyard committed
28 29
        fields = ['search', 'chamber', 'country', 'party', 'delegation',
                  'committee']
30 31 32 33 34 35

    def search_filter(self, qs, value):
        if len(value) == 0:
            return qs

        return qs.filter(slug__icontains=slugify(value))
Nicolas Joyard's avatar
Nicolas Joyard committed
36

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
    def chamber_filter(self, qs, value):
        if len(value) == 0:
            return qs

        today = datetime.date.today()
        return qs.filter(
            Q(mandates__end_date__gte=today) |
            Q(mandates__end_date__isnull=True),
            mandates__group__chamber=value
        )

    def group_filter(self, qs, value):
        if len(value) == 0:
            return qs

        today = datetime.date.today()
        return qs.filter(
            Q(mandates__end_date__gte=today) |
            Q(mandates__end_date__isnull=True),
            mandates__group=value
        )

Nicolas Joyard's avatar
Nicolas Joyard committed
59 60 61 62 63
    def score_min_filter(self, qs, value):
        if len(value) == 0:
            return qs

        try:
64
            return qs.filter(representative_score__score__gte=int(value))
Nicolas Joyard's avatar
Nicolas Joyard committed
65 66 67 68 69 70 71 72
        except ValueError:
            return qs

    def score_max_filter(self, qs, value):
        if len(value) == 0:
            return qs

        try:
73
            return qs.filter(representative_score__score__lte=int(value))
Nicolas Joyard's avatar
Nicolas Joyard committed
74 75 76
        except ValueError:
            return qs

Nicolas Joyard's avatar
Nicolas Joyard committed
77 78 79 80

class DossierFilter(FilterSet):

    search = MethodFilter(action='search_filter')
81
    chamber = MethodFilter(action='chamber_filter')
82

Nicolas Joyard's avatar
Nicolas Joyard committed
83 84
    class Meta:
        model = Dossier
85
        fields = ['search', 'chamber']
Nicolas Joyard's avatar
Nicolas Joyard committed
86 87 88 89 90 91

    def search_filter(self, qs, value):
        if len(value) == 0:
            return qs

        return qs.filter(Q(title__icontains=value) |
92 93
                         Q(reference__icontains=value) |
                         Q(documents__link__icontains=value))
94

95 96 97 98 99 100
    def chamber_filter(self, qs, value):
        if len(value) == 0:
            return qs

        return qs.filter(documents__chamber=value)

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115

class ThemeFilter(FilterSet):

    search = MethodFilter(action='search_filter')

    class Meta:
        model = Theme
        fields = ['search']

    def search_filter(self, qs, value):
        if len(value) == 0:
            return qs

        return qs.filter(Q(name__icontains=value) |
                         Q(description__icontains=value))