representative_detail_votes.py 1.5 KB
Newer Older
1
2
3
4
# coding: utf-8

from django.db import models

5
from representatives_votes.models import Vote
6
7
8
9
10
11
12
13
14
15
16
17
18

from .representative_detail_base import RepresentativeDetailBase


class RepresentativeDetailVotes(RepresentativeDetailBase):
    template_name = 'representatives/representative_detail_votes.html'

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

        qs = qs.prefetch_related(
            models.Prefetch(
                'votes',
19
20
                queryset=Vote.objects.exclude(
                    proposal__recommendation=None
21
                ).select_related(
22
                    'vote_score',
23
24
                    'proposal__dossier',
                    'proposal__recommendation'
25
                ).order_by('-proposal__datetime', 'proposal__title')
26
            ),
27
            'dossier_scores'
28
29
30
31
32
33
34
        )

        return qs

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

35
        ds = c['object'].dossier_scores.all()
36
37
38
39
40
41
42
43
44
45
46
47
48
49

        dossiers = {}
        for vote in c['object'].votes.all():
            dossier = vote.proposal.dossier
            pk = dossier.pk
            if pk not in dossiers:
                dossiers[pk] = {
                    'dossier': dossier,
                    'votes': [],
                    'score': [s.score for s in ds if s.dossier_id == pk][0]
                }
            dossiers[pk]['votes'].append(vote)

        c['dossiers'] = dossiers
50
51
52
        c['tab'] = 'votes'

        return c