theme_detail_dossiers.py 1.1 KB
Newer Older
Nicolas Joyard's avatar
Nicolas Joyard committed
1
2
# coding: utf-8

Nicolas Joyard's avatar
Nicolas Joyard committed
3
4
5
6
from django.db import models

from representatives_votes.models import Dossier

Nicolas Joyard's avatar
Nicolas Joyard committed
7
8
9
10
11
12
13
14
from .theme_detail_base import ThemeDetailBase


class ThemeDetailDossiers(ThemeDetailBase):
    template_name = 'memopol_themes/theme_detail_dossiers.html'

    def get_queryset(self):
        qs = super(ThemeDetailDossiers, self).get_queryset()
Nicolas Joyard's avatar
Nicolas Joyard committed
15
16
17
18
        qs = qs.prefetch_related(
            models.Prefetch(
                'dossiers',
                Dossier.objects.order_by('-pk')
Nicolas Joyard's avatar
Nicolas Joyard committed
19
                .prefetch_related('documents__chamber', 'themes')
Nicolas Joyard's avatar
Nicolas Joyard committed
20
21
22
23
24
25
                .annotate(
                    nb_proposals=models.Count('proposals', distinct=True),
                    nb_recommendations=models.Count(
                        'proposals__recommendation', distinct=True),
                    nb_documents=models.Count('documents', distinct=True)
                )
Nicolas Joyard's avatar
Nicolas Joyard committed
26
27
            )
        )
Nicolas Joyard's avatar
Nicolas Joyard committed
28
29
30
31
32
33
34
35
36
        return qs

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

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

        return c