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

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


Jamesie Pic's avatar
Jamesie Pic committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
35
36
37
38
39
40
41
42
43

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


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

    def get_queryset(self):
44
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
70
71
        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),
                ).select_related('proposal__recommendation')
            )
        )
Jamesie Pic's avatar
Jamesie Pic committed
72
73
74
75
        return qs

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

Jamesie Pic's avatar
Jamesie Pic committed
80
81
82
83
84
        return c


class DossierList(PaginationMixin, representatives_votes_views.DossierList):
    queryset = Dossier.objects.exclude(proposals__recommendation=None)