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

from .forms import PositionForm


Nicolas Joyard's avatar
Nicolas Joyard committed
8 9 10 11 12 13 14 15 16 17 18
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
19

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

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