views.py 1.12 KB
Newer Older
Jamesie Pic's avatar
Jamesie Pic committed
1
2
from django.views import generic
from django.db import models
3
from django.core.urlresolvers import reverse
Jamesie Pic's avatar
Jamesie Pic committed
4

5
from memopol.views.representative_mixin import RepresentativeViewMixin
Jamesie Pic's avatar
Jamesie Pic committed
6
7
8
9
10
11
12
13
from representatives.models import Mandate

from .models import Position
from .forms import PositionForm


class PositionCreate(generic.CreateView):
    model = Position
14
    form_class = PositionForm
Jamesie Pic's avatar
Jamesie Pic committed
15
16

    def get_success_url(self):
17
18
        return reverse('representative-detail',
            args=(self.object.representative.slug,))
Jamesie Pic's avatar
Jamesie Pic committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36


class PositionDetail(RepresentativeViewMixin, generic.DetailView):
    queryset = Position.objects.filter(published=True).select_related(
        'representative__score')

    def get_queryset(self):
        qs = super(PositionDetail, self).get_queryset()
        qs = qs.prefetch_related(models.Prefetch(
            'representative__mandates',
            Mandate.objects.select_related('constituency__country', 'group')
        ))
        return qs

    def get_object(self):
        obj = super(PositionDetail, self).get_object()
        self.add_representative_country_and_main_mandate(obj.representative)
        return obj