home.py 995 Bytes
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
import datetime
import random

from django.db.models import Q
Nicolas Joyard's avatar
Nicolas Joyard committed
7
8
from django.views import generic

Nicolas Joyard's avatar
Nicolas Joyard committed
9
from representatives.models import Representative
Nicolas Joyard's avatar
Nicolas Joyard committed
10
11
from representatives_positions.views import PositionFormMixin

Nicolas Joyard's avatar
Nicolas Joyard committed
12
13
from .representative_mixin import RepresentativeViewMixin

Nicolas Joyard's avatar
Nicolas Joyard committed
14

Nicolas Joyard's avatar
Nicolas Joyard committed
15
16
class HomeView(PositionFormMixin, RepresentativeViewMixin,
               generic.TemplateView):
Nicolas Joyard's avatar
Nicolas Joyard committed
17
    template_name = 'home.html'
Nicolas Joyard's avatar
Nicolas Joyard committed
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

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

        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)

        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'])

        return c