views.py 3.52 KB
Newer Older
1
from django.urls import reverse_lazy
2
from django.http import HttpResponseRedirect
3
from django.views.generic import (
4
    TemplateView, ListView, CreateView, DetailView
5
)
6

7 8
from .models import Job, Application, Question
from .forms import JobForm, QuestionFormSet, ApplicationForm, AnswerForm
9

10 11 12 13 14 15 16 17

class Index(TemplateView):
    template_name = 'jobs/index.html'


class JobList(ListView):
    context_object_name = 'jobs'
    model = Job
18

19

20 21 22 23 24
class JobDetail(DetailView):
    context_object_name = 'job'
    model = Job


25 26 27 28 29
class JobCreate(CreateView):
    model = Job
    form_class = JobForm
    success_url = reverse_lazy("jobs:list")

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
    def get(self, request, *args, **kwargs):
        self.object = None
        form = self.get_form(self.get_form_class())
        question_form = QuestionFormSet()
        return self.render_to_response(
            self.get_context_data(form=form, question_form=question_form))

    def post(self, request, *args, **kwargs):
        self.object = None
        form_class = self.get_form_class()
        form = self.get_form(form_class)
        question_form = QuestionFormSet(self.request.POST)
        if (form.is_valid() and question_form.is_valid()):
            return self.form_valid(form, question_form)
        else:
            return self.form_invalid(form, question_form)

    def form_valid(self, form, question_form):
        self.object = form.save(commit=False)
        self.object.posted_by = self.request.user
        self.object.save()

        question_form.instance = self.object
        question_form.save()

        return HttpResponseRedirect(self.get_success_url())

    def form_invalid(self, form, question_form):
        return self.render_to_response(
            self.get_context_data(
                form=form,
                question_form=question_form))
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110


class ApplicationCreate(CreateView):
    # TODO: restrict users from re-application
    model = Application
    form_class = ApplicationForm
    success_url = reverse_lazy("jobs:list")

    def get_question_queryset(self):
        return Question.objects.filter(job_id=self.kwargs['job_pk']).order_by('id')

    def get_answer_formset(self, *args):
        questions = self.get_question_queryset()
        return AnswerForm.inlineformset_factory(
            extra=questions.count())(initial=[{
                'question': q.id
            } for q in questions], *args)

    def get(self, request, *args, **kwargs):
        self.object = None
        form = self.get_form(self.get_form_class())
        answer_form = self.get_answer_formset()
        return self.render_to_response(
            self.get_context_data(form=form, answer_form=answer_form))

    def post(self, request, *args, **kwargs):
        self.object = None
        form_class = self.get_form_class()
        form = self.get_form(form_class)
        answer_form = self.get_answer_formset(self.request.POST)
        if (form.is_valid() and answer_form.is_valid()):
            return self.form_valid(form, answer_form)
        else:
            return self.form_invalid(form, answer_form)

    def form_valid(self, form, answer_form):
        self.object = form.save(commit=False)
        self.object.applicant = self.request.user
        self.object.job = Job.objects.get(id=self.kwargs['job_pk'])
        self.object.save()

        answer_form.instance = self.object
        answer_form.save()

        return HttpResponseRedirect(self.get_success_url())

    def form_invalid(self, form, answer_form):
        return self.render_to_response(
            self.get_context_data(form=form, answer_form=answer_form))