theme_list.py 1.44 KB
Newer Older
1
2
# coding: utf-8

Nicolas Joyard's avatar
Nicolas Joyard committed
3
from core.views import PaginationMixin, SortMixin
4

Nicolas Joyard's avatar
Nicolas Joyard committed
5
from django.db.models import Count
6
7
8
9
10
11
from django.views import generic

from memopol_themes.models import Theme

from ..filters import ThemeFilter

Nicolas Joyard's avatar
Nicolas Joyard committed
12
from representatives_positions.views import PositionFormMixin
13

Nicolas Joyard's avatar
Nicolas Joyard committed
14
15
16

class ThemeList(PaginationMixin, SortMixin, PositionFormMixin,
                generic.ListView):
17
18

    current_filter = None
Nicolas Joyard's avatar
Nicolas Joyard committed
19
20
21
22
23
24
25
    queryset = Theme.objects.all().annotate(
        nb_links=Count('links', distinct=True),
        nb_dossiers=Count('dossiers', distinct=True),
        nb_proposals=Count('proposals', distinct=True),
        nb_positions=Count('positions', distinct=True)
    )

Nicolas Joyard's avatar
Nicolas Joyard committed
26
27
28
29
30
31
32
    sort_modes = {
        'name-asc': {
            'order': 0,
            'label': 'Name A-Z',
            'fields': ['name']
        },
        'name-desc': {
33
            'order': 1,
Nicolas Joyard's avatar
Nicolas Joyard committed
34
35
36
            'label': 'Name Z-A',
            'fields': ['-name']
        }
Nicolas Joyard's avatar
Nicolas Joyard committed
37
    }
Nicolas Joyard's avatar
Nicolas Joyard committed
38
    sort_default = 'name-asc'
39
    sort_session_prefix = 'theme_list'
40
41
42
43
44
45
46
47
48
49
50
51
52
53

    def theme_filter(self, qs):
        f = ThemeFilter(self.request.GET, queryset=qs)
        self.current_filter = f
        return f.qs

    def get_queryset(self):
        qs = super(ThemeList, self).get_queryset()
        qs = self.theme_filter(qs)
        return qs

    def get_context_data(self, **kwargs):
        c = super(ThemeList, self).get_context_data(**kwargs)
        c['filter'] = self.current_filter
Nicolas Joyard's avatar
Nicolas Joyard committed
54
        c['view'] = 'theme_list'
55
        return c