representative_detail_base.py 2.46 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 (Address, Chamber, Representative, Phone,
                                    WebSite)
Jamesie Pic's avatar
Jamesie Pic committed
8

9
from .representative_mixin import RepresentativeViewMixin
Jamesie Pic's avatar
Jamesie Pic committed
10

Nicolas Joyard's avatar
Nicolas Joyard committed
11
from representatives_positions.views import PositionFormMixin
Jamesie Pic's avatar
Jamesie Pic committed
12

13
14
from core.views import ThemeSelectionMixin

Nicolas Joyard's avatar
Nicolas Joyard committed
15
16

class RepresentativeDetailBase(RepresentativeViewMixin, PositionFormMixin,
17
                               ThemeSelectionMixin, generic.DetailView):
Jamesie Pic's avatar
Jamesie Pic committed
18

19
    queryset = Representative.objects.select_related('representative_score')
Jamesie Pic's avatar
Jamesie Pic committed
20
21

    def get_queryset(self):
22
        qs = super(RepresentativeDetailBase, self).get_queryset()
23
24
25

        qs = self.prefetch_for_representative_country_and_main_mandate(qs)

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

30
        qs = qs.prefetch_related(
31
32
33
            'email_set',
            models.Prefetch(
                'website_set',
34
35
                queryset=WebSite.objects.filter(kind__in=social)
                                        .order_by('id'),
36
37
38
39
                to_attr='social_websites'
            ),
            models.Prefetch(
                'website_set',
40
41
                queryset=WebSite.objects.filter(kind__in=chambers)
                                        .order_by('id'),
42
43
44
45
46
                to_attr='chamber_websites'
            ),
            models.Prefetch(
                'website_set',
                queryset=WebSite.objects.exclude(kind__in=social)
47
48
                                        .exclude(kind__in=chambers)
                                        .order_by('id'),
49
50
                to_attr='other_websites'
            ),
51
52
53
54
55
56
            models.Prefetch(
                'address_set',
                queryset=Address.objects.select_related('country')
                                        .prefetch_related('phones')
                                        .order_by('id')
            ),
57
58
            models.Prefetch(
                'phone_set',
59
                queryset=Phone.objects.filter(address__isnull=True)
60
61
            ),
            'theme_scores__theme'
62
        )
63

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

    def get_context_data(self, **kwargs):
67
        c = super(RepresentativeDetailBase, self).get_context_data(**kwargs)
68

Jamesie Pic's avatar
Jamesie Pic committed
69
        self.add_representative_country_and_main_mandate(c['object'])
70
        c['position_form'].fields['representatives'].initial = [c['object'].pk]
71

Jamesie Pic's avatar
Jamesie Pic committed
72
        return c