representative_votes.py 1.26 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# coding: utf-8

from django.db import models
from django.views import generic

from representatives.models import Representative
from representatives_recommendations.models import VoteScore
from representatives_votes.models import Proposal

from .representative_mixin import RepresentativeViewMixin


class RepresentativeVotes(RepresentativeViewMixin, generic.DetailView):
    template_name = 'representatives/representative_votes'

    queryset = Representative.objects.select_related('score')

    def get_queryset(self):
        qs = super(RepresentativeVotes, self).get_queryset()

        qs = self.prefetch_for_representative_country_and_main_mandate(qs)

        qs = qs.prefetch_related(
            models.Prefetch(
                'votes',
                queryset=VoteScore.objects.filter(
                    proposal__in=Proposal.objects.exclude(recommendation=None),
                ).select_related('proposal__recommendation').order_by(
                    '-proposal__datetime')
            )
        )

        return qs

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

        self.add_representative_country_and_main_mandate(c['object'])

        c['votes'] = c['object'].votes.all()

        return c