representative_detail.py 2.67 KB
Newer Older
1
2
# coding: utf-8

Jamesie Pic's avatar
Jamesie Pic committed
3
from django.db import models
4
from django.views import generic
Jamesie Pic's avatar
Jamesie Pic committed
5

6
7
from representatives.models import Chamber, Representative, Address, Phone, \
    WebSite
Jamesie Pic's avatar
Jamesie Pic committed
8
from representatives_positions.forms import PositionForm
9
from representatives_recommendations.models import VoteScore
10
from representatives_votes.models import Proposal
Jamesie Pic's avatar
Jamesie Pic committed
11

12
from .representative_mixin import RepresentativeViewMixin
Jamesie Pic's avatar
Jamesie Pic committed
13
14


15
class RepresentativeDetail(RepresentativeViewMixin, generic.DetailView):
Jamesie Pic's avatar
Jamesie Pic committed
16
17
18
19

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

    def get_queryset(self):
20
21
22
23
        qs = super(RepresentativeDetail, self).get_queryset()

        qs = self.prefetch_for_representative_country_and_main_mandate(qs)

24
        social = ['twitter', 'facebook']
25
26
27
        chambers = [c['abbreviation']
                    for c in Chamber.objects.values('abbreviation')]

28
        qs = qs.prefetch_related(
29
30
31
32
33
34
35
36
            'email_set',
            models.Prefetch(
                'website_set',
                queryset=WebSite.objects.filter(kind__in=social),
                to_attr='social_websites'
            ),
            models.Prefetch(
                'website_set',
37
38
39
40
41
42
43
                queryset=WebSite.objects.filter(kind__in=chambers),
                to_attr='chamber_websites'
            ),
            models.Prefetch(
                'website_set',
                queryset=WebSite.objects.exclude(kind__in=social)
                                .exclude(kind__in=chambers),
44
45
46
47
48
49
50
51
52
53
54
55
56
57
                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),
58
59
                ).select_related('proposal__recommendation').order_by(
                    '-proposal__datetime')
60
61
            )
        )
62

Jamesie Pic's avatar
Jamesie Pic committed
63
64
65
66
        return qs

    def get_context_data(self, **kwargs):
        c = super(RepresentativeDetail, self).get_context_data(**kwargs)
67
68
69
70
71

        self.add_representative_country_and_main_mandate(c['object'])

        c['votes'] = c['object'].votes.all()
        c['mandates'] = c['object'].mandates.all()
72
73
        c['positions'] = c['object'].positions.filter(published=True) \
            .order_by('-datetime', 'pk')
74

75
76
        c['position_form'] = PositionForm(
            initial={'representative': self.object.pk})
Jamesie Pic's avatar
Jamesie Pic committed
77
        self.add_representative_country_and_main_mandate(c['object'])
78

Jamesie Pic's avatar
Jamesie Pic committed
79
        return c