representative_list.py 2.78 KB
Newer Older
1 2
# coding: utf-8

3
from core.views import GridListMixin, PaginationMixin, CSVDownloadMixin, \
4
    ActiveLegislatureMixin, SortMixin
5

6
from django.db import models
7 8
from django.views import generic

9
from representatives.models import Representative, Email
10

11
from ..filters import RepresentativeFilter
12 13
from .representative_mixin import RepresentativeViewMixin

Nicolas Joyard's avatar
Nicolas Joyard committed
14 15
from representatives_positions.views import PositionFormMixin

16 17

class RepresentativeList(CSVDownloadMixin, GridListMixin, PaginationMixin,
18
                         RepresentativeViewMixin, ActiveLegislatureMixin,
Nicolas Joyard's avatar
Nicolas Joyard committed
19
                         SortMixin, PositionFormMixin, generic.ListView):
20

21
    csv_name = 'representatives'
22
    queryset = Representative.objects.select_related('representative_score')
23
    current_filter = None
Nicolas Joyard's avatar
Nicolas Joyard committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37
    sort_modes = {
        'name-asc': {
            'order': 0,
            'label': 'Name A-Z',
            'fields': ['last_name']
        },
        'name-desc': {
            'order': 1,
            'label': 'Name Z-A',
            'fields': ['-last_name']
        },
        'score-asc': {
            'order': 2,
            'label': 'Best score',
38
            'fields': ['-representative_score__score']
Nicolas Joyard's avatar
Nicolas Joyard committed
39 40
        },
        'score-desc': {
41
            'order': 3,
Nicolas Joyard's avatar
Nicolas Joyard committed
42
            'label': 'Worst score',
43
            'fields': ['representative_score__score']
Nicolas Joyard's avatar
Nicolas Joyard committed
44
        }
45
    }
Nicolas Joyard's avatar
Nicolas Joyard committed
46
    sort_default = 'name-asc'
47
    sort_session_prefix = 'representative_list'
48 49 50 51

    def get_context_data(self, **kwargs):
        c = super(RepresentativeList, self).get_context_data(**kwargs)

52
        c['filter'] = self.current_filter
53
        c['view'] = 'representative_list'
54 55 56 57 58 59 60
        c['object_list'] = [
            self.add_representative_country_and_main_mandate(r)
            for r in c['object_list']
        ]

        return c

61 62 63 64
    def rep_filter(self, qs):
        f = RepresentativeFilter(self.request.GET, queryset=qs)
        self.current_filter = f
        return f.qs
65 66 67

    def get_queryset(self):
        qs = super(RepresentativeList, self).get_queryset()
68 69
        if self.get_active_only():
            qs = qs.filter(active=True)
70
        qs = self.rep_filter(qs)
71
        qs = self.prefetch_for_representative_country_and_main_mandate(qs)
72
        return qs.distinct()
73 74

    def get_csv_results(self, context, **kwargs):
75
        qs = self.get_queryset()
76 77 78 79 80 81
        qs = qs.prefetch_related(
            models.Prefetch(
                'email_set',
                queryset=Email.objects.order_by('email')
            )
        )
82 83 84 85 86 87 88
        return [self.add_representative_country_and_main_mandate(r)
                for r in qs]

    def get_csv_row(self, obj):
        return (
            obj.full_name,
            u', '.join([e.email for e in obj.email_set.all()]),
Nicolas Joyard's avatar
Nicolas Joyard committed
89
            obj.main_mandate.group.abbreviation if obj.main_mandate else None,
90 91
            obj.country,
        )