views.py 1.1 KB
Newer Older
Jamesie Pic's avatar
Jamesie Pic committed
1 2 3 4 5
from django.views import generic

from .forms import PositionForm


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

Nicolas Joyard's avatar
Nicolas Joyard committed
18
    def post(self, request, *args, **kwargs):
19
        if 'position-representatives' in request.POST:
Nicolas Joyard's avatar
Nicolas Joyard committed
20 21 22 23 24
            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
25

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

Nicolas Joyard's avatar
Nicolas Joyard committed
28 29
    def get_context_data(self, **kwargs):
        c = super(PositionFormMixin, self).get_context_data(**kwargs)
Jamesie Pic's avatar
Jamesie Pic committed
30

Nicolas Joyard's avatar
Nicolas Joyard committed
31 32 33
        c['position_form'] = \
            self.position_form or PositionForm(prefix='position')
        c['position_created'] = self.position_created
Jamesie Pic's avatar
Jamesie Pic committed
34

Nicolas Joyard's avatar
Nicolas Joyard committed
35
        return c