views.py 1.31 KB
Newer Older
Jamesie Pic's avatar
Jamesie Pic committed
1
from django.views import generic
2
from django.views.decorators.csrf import csrf_exempt
Jamesie Pic's avatar
Jamesie Pic committed
3
4
5
6

from .forms import PositionForm


Nicolas Joyard's avatar
Nicolas Joyard committed
7
8
9
10
11
12
13
14
15
16
17
class PositionFormMixin(generic.View):
    """
    Mixin for class views that handle a position form (should be all full-page
    views, ie. all template views that use a templates that extends base.html).

    We don't use a FormView here to allow usage of this mixin in views that
    have their own form.
    """

    position_form = None
    position_created = False
Jamesie Pic's avatar
Jamesie Pic committed
18

19
    @csrf_exempt
20
21
22
23
    def dispatch(self, request, *args, **kwargs):
        return super(PositionFormMixin, self).dispatch(
                request, *args, **kwargs)

Nicolas Joyard's avatar
Nicolas Joyard committed
24
    def post(self, request, *args, **kwargs):
25
        if 'position-representatives' in request.POST:
Nicolas Joyard's avatar
Nicolas Joyard committed
26
27
28
29
30
            self.position_form = PositionForm(request.POST, prefix='position')
            if self.position_form.is_valid():
                self.position_form.save()
                self.position_form = None
                self.position_created = True
Jamesie Pic's avatar
Jamesie Pic committed
31

Nicolas Joyard's avatar
Nicolas Joyard committed
32
        return self.get(request, args, kwargs)
Jamesie Pic's avatar
Jamesie Pic committed
33

Nicolas Joyard's avatar
Nicolas Joyard committed
34
35
    def get_context_data(self, **kwargs):
        c = super(PositionFormMixin, self).get_context_data(**kwargs)
Jamesie Pic's avatar
Jamesie Pic committed
36

Nicolas Joyard's avatar
Nicolas Joyard committed
37
38
39
        c['position_form'] = \
            self.position_form or PositionForm(prefix='position')
        c['position_created'] = self.position_created
Jamesie Pic's avatar
Jamesie Pic committed
40

Nicolas Joyard's avatar
Nicolas Joyard committed
41
        return c