views.py 1.17 KB
Newer Older
1
from django.views import generic
2
from django.views.decorators.csrf import csrf_exempt
3 4 5 6

from .forms import PositionForm


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
18

19
    @csrf_exempt
20
    def post(self, request, *args, **kwargs):
21
        if 'position-representatives' in request.POST:
22 23 24 25 26
            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
27

28
        return self.get(request, args, kwargs)
29

30 31
    def get_context_data(self, **kwargs):
        c = super(PositionFormMixin, self).get_context_data(**kwargs)
32

33 34 35
        c['position_form'] = \
            self.position_form or PositionForm(prefix='position')
        c['position_created'] = self.position_created
36

37
        return c