views.py 2.13 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 6 7
from representatives import views as representatives_views
from representatives.models import Representative
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 10 11 12
from representatives_positions.forms import PositionForm
from representatives_recommendations.models import ScoredVote


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 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):
        qs = super(RepresentativeDetail, self).get_queryset()
45 46 47
        votes = ScoredVote.objects.filter(
            proposal__in=Proposal.objects.exclude(recommendation=None),
        ).select_related('proposal__recommendation')
Jamesie Pic's avatar
Jamesie Pic committed
48 49 50 51 52
        qs = qs.prefetch_related(models.Prefetch('votes', queryset=votes))
        return qs

    def get_context_data(self, **kwargs):
        c = super(RepresentativeDetail, self).get_context_data(**kwargs)
53 54
        c['position_form'] = PositionForm(
            initial={'representative': self.object.pk})
Jamesie Pic's avatar
Jamesie Pic committed
55 56 57 58 59 60
        self.add_representative_country_and_main_mandate(c['object'])
        return c


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