representative_detail_positions.py 1.09 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# coding: utf-8

from django.db import models

from representatives_positions.models import Position

from .representative_detail_base import RepresentativeDetailBase


class RepresentativeDetailPositions(RepresentativeDetailBase):
    template_name = 'representatives/representative_detail_positions.html'

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

16
17
18
19
20
        positions_qs = Position.objects.filter(published=True)
        theme = self.get_selected_theme()
        if theme:
            positions_qs = positions_qs.filter(themes__slug=theme)

21
22
23
        qs = qs.prefetch_related(
            models.Prefetch(
                'positions',
24
                queryset=positions_qs.order_by('-datetime', 'pk')
25
            ),
26
            'positions__themes',
27
            'positions__position_score'
28
29
30
31
32
33
34
35
36
37
38
39
        )

        return qs

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

        c['tab'] = 'positions'
        c['positions'] = c['object'].positions.all()

        return c