home.py 2.49 KB
Newer Older
1 2
# coding: utf-8

Nicolas Joyard's avatar
Nicolas Joyard committed
3 4 5
import datetime
import random

Nicolas Joyard's avatar
Nicolas Joyard committed
6
from django.db.models import Q, Count
7
from django.views import generic
8
from django.utils.decorators import method_decorator
9
from django.views.decorators.csrf import csrf_exempt
10

11
from representatives.models import Chamber, Representative
12
from representatives_positions.views import PositionFormMixin
13
from representatives_votes.models import Proposal
14

15
from memopol_settings.models import Setting
Nicolas Joyard's avatar
Nicolas Joyard committed
16 17
from memopol_themes.models import Theme

Nicolas Joyard's avatar
Nicolas Joyard committed
18 19
from .representative_mixin import RepresentativeViewMixin

20

Nicolas Joyard's avatar
Nicolas Joyard committed
21 22
class HomeView(PositionFormMixin, RepresentativeViewMixin,
               generic.TemplateView):
23
    template_name = 'home.html'
Nicolas Joyard's avatar
Nicolas Joyard committed
24

25
    @method_decorator(csrf_exempt)
26 27 28
    def dispatch(self, *args, **kwargs):
        return super(HomeView, self).dispatch(*args, **kwargs)

Nicolas Joyard's avatar
Nicolas Joyard committed
29 30 31
    def get_context_data(self, **kwargs):
        c = super(HomeView, self).get_context_data(**kwargs)

Nicolas Joyard's avatar
Nicolas Joyard committed
32 33
        # Today's mep

Nicolas Joyard's avatar
Nicolas Joyard committed
34 35 36 37
        qs = Representative.objects
        qs = qs.filter(Q(representative_score__score__lt=0) |
                       Q(representative_score__score__gt=0))
        qs = self.prefetch_for_representative_country_and_main_mandate(qs)
38
        qs = qs.select_related('representative_score')
Nicolas Joyard's avatar
Nicolas Joyard committed
39 40 41 42 43 44 45

        random.seed(datetime.date.today().isoformat())
        index = random.randint(0, qs.count() - 1)
        c['todays_mep'] = qs.all()[index]

        self.add_representative_country_and_main_mandate(c['todays_mep'])

Nicolas Joyard's avatar
Nicolas Joyard committed
46 47 48 49 50 51 52 53
        # Featured themes

        c['featured_themes'] = Theme.objects \
            .filter(featured=True) \
            .annotate(
                nb_links=Count('links', distinct=True),
                nb_dossiers=Count('dossiers', distinct=True),
                nb_proposals=Count('proposals', distinct=True),
Nicolas Joyard's avatar
Nicolas Joyard committed
54
                nb_positions=Count('positions', distinct=True))
Nicolas Joyard's avatar
Nicolas Joyard committed
55

56 57 58 59 60
        # Last votes

        num = int(Setting.objects.get(pk='HOMEPAGE_LATEST_VOTES').value)
        c['latest_votes'] = Proposal.objects \
            .filter(recommendation__isnull=False) \
61 62 63
            .select_related('dossier', 'recommendation') \
            .prefetch_related('themes', 'dossier__themes',
                              'dossier__documents__chamber') \
64 65
            .order_by('-datetime')[0:num]

66 67 68 69 70 71 72 73
        # Chambers

        c['chambers'] = Chamber.objects.all() \
            .annotate(
                nb_dossiers=Count('documents__dossier', distinct=True),
                nb_proposals=Count('documents__dossier__proposals',
                                   distinct=True))

Nicolas Joyard's avatar
Nicolas Joyard committed
74
        return c