views.py 3.14 KB
Newer Older
Jamesie Pic's avatar
Jamesie Pic committed
1
2
# Project specific "glue" coupling of all apps
from django.db import models
Nicolas Joyard's avatar
Nicolas Joyard committed
3
from django.db.models import Count
Jamesie Pic's avatar
Jamesie Pic committed
4

Jamesie Pic's avatar
Jamesie Pic committed
5
from core.views import GridListMixin, PaginationMixin, CSVDownloadMixin
Jamesie Pic's avatar
Jamesie Pic committed
6
from representatives import views as representatives_views
7
from representatives.models import (Representative, Address, Phone, WebSite)
Jamesie Pic's avatar
Jamesie Pic committed
8
from representatives_votes import views as representatives_votes_views
9
from representatives_votes.models import Dossier, Proposal
Jamesie Pic's avatar
Jamesie Pic committed
10
from representatives_positions.forms import PositionForm
11
from representatives_recommendations.models import VoteScore
Jamesie Pic's avatar
Jamesie Pic committed
12
13


Jamesie Pic's avatar
Jamesie Pic committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class RepresentativeList(
    CSVDownloadMixin,
    GridListMixin,
    PaginationMixin,
    representatives_views.RepresentativeList
):

    csv_name = 'meps.csv'

    def get_csv_results(self, context, **kwargs):
        qs = super(RepresentativeList, self).get_queryset()
        qs = qs.prefetch_related('email_set')
        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()]),
            obj.main_mandate.group.abbreviation,
            obj.country,
        )
Jamesie Pic's avatar
Jamesie Pic committed
36
37
38
39
40
41
42
43
44

    queryset = Representative.objects.filter(
        active=True).select_related('score')


class RepresentativeDetail(representatives_views.RepresentativeDetail):
    queryset = Representative.objects.select_related('score')

    def get_queryset(self):
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
        social = ['twitter', 'facebook']
        qs = super(RepresentativeDetail, self).get_queryset().prefetch_related(
            'email_set',
            models.Prefetch(
                'website_set',
                queryset=WebSite.objects.filter(kind__in=social),
                to_attr='social_websites'
            ),
            models.Prefetch(
                'website_set',
                queryset=WebSite.objects.exclude(kind__in=social),
                to_attr='other_websites'
            ),
            models.Prefetch(
                'address_set',
                queryset=Address.objects.select_related('country')
            ),
            models.Prefetch(
                'phone_set',
                queryset=Phone.objects.select_related('address__country')
            ),
            models.Prefetch(
                'votes',
                queryset=VoteScore.objects.filter(
                    proposal__in=Proposal.objects.exclude(recommendation=None),
70
71
                ).select_related('proposal__recommendation').order_by(
                    '-proposal__datetime')
72
73
            )
        )
Jamesie Pic's avatar
Jamesie Pic committed
74
75
76
77
        return qs

    def get_context_data(self, **kwargs):
        c = super(RepresentativeDetail, self).get_context_data(**kwargs)
78
79
        c['position_form'] = PositionForm(
            initial={'representative': self.object.pk})
Jamesie Pic's avatar
Jamesie Pic committed
80
        self.add_representative_country_and_main_mandate(c['object'])
81

Jamesie Pic's avatar
Jamesie Pic committed
82
83
84
85
        return c


class DossierList(PaginationMixin, representatives_votes_views.DossierList):
Nicolas Joyard's avatar
Nicolas Joyard committed
86
87
88
89
90
    queryset = Dossier.objects.prefetch_related(
        'proposals',
        'proposals__recommendation'
    ).annotate(
        nb_recomm=Count('proposals__recommendation')
Nicolas Joyard's avatar
Nicolas Joyard committed
91
    ).order_by('-nb_recomm', '-reference')