views.py 5.34 KB
Newer Older
1
from django.urls import reverse_lazy
2
from django.http import HttpResponseRedirect
3
from django.contrib.auth.mixins import LoginRequiredMixin
4 5 6
from django.views.generic import (View, TemplateView, ListView, CreateView,
                                  DetailView)
from django.views.generic.detail import SingleObjectMixin
7 8
from django.contrib import messages
from django.shortcuts import get_object_or_404
9
from rules.contrib.views import PermissionRequiredMixin
10

11 12
from .models import Job, Application, Question
from .forms import JobForm, QuestionFormSet, ApplicationForm, AnswerForm
13

14 15 16 17

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

Aatish Neupane's avatar
Aatish Neupane committed
18 19
    def get_context_data(self, *args, **kwargs):
        context = super().get_context_data(*args, **kwargs)
Aatish Neupane's avatar
Aatish Neupane committed
20
        context['jobs'] = Job.objects.all().order_by('-updated')[:6]
Aatish Neupane's avatar
Aatish Neupane committed
21 22
        return context

23 24 25 26

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

28

29 30 31 32 33
class JobDetail(DetailView):
    context_object_name = 'job'
    model = Job


34 35
class JobRenew(LoginRequiredMixin, PermissionRequiredMixin, SingleObjectMixin,
               View):
36 37 38 39 40 41 42 43
    model = Job
    permission_required = 'jobs.change_job'
    http_method_names = ['post']

    def post(self, request, *args, **kwargs):
        job = self.get_object()
        job.renew()
        messages.add_message(
44
            request, messages.SUCCESS, 'Job has been renewed until {}.'.format(
45 46 47 48
                job.expires.isoformat(' ', 'seconds')))
        return HttpResponseRedirect(job.get_absolute_url())


49
class JobCreate(LoginRequiredMixin, CreateView):
50 51
    model = Job
    form_class = JobForm
52
    success_url = reverse_lazy("jobs:job_list")
53

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    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()
75
        form.save_m2m()
76 77 78 79 80 81 82 83

        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(
84
            self.get_context_data(form=form, question_form=question_form))
85 86


87
class ApplicationCreate(LoginRequiredMixin, CreateView):
88 89 90
    # TODO: restrict users from re-application
    model = Application
    form_class = ApplicationForm
91
    success_url = reverse_lazy("jobs:job_list")
92 93

    def get_question_queryset(self):
94 95 96 97
        # filter questions for particular job and order it so same queryset
        # can be used stably as initial params for GET and POST requests
        return Question.objects.filter(
            job_id=self.kwargs['job_pk']).order_by('id')
98 99 100

    def get_answer_formset(self, *args):
        questions = self.get_question_queryset()
101 102
        return AnswerForm.inlineformset_factory(extra=questions.count())(
            initial=[{
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
                '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
126
        self.object.job = get_object_or_404(Job, id=self.kwargs['job_pk'])
127
        self.object.save()
128
        form.save_m2m()
129 130 131 132

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

133 134 135 136
        messages.add_message(
            self.request, messages.SUCCESS,
            'Your application has been succesfully received.')

137 138 139 140 141
        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))
142 143


144
class ApplicationList(LoginRequiredMixin, PermissionRequiredMixin, ListView):
145 146
    model = Application
    context_object_name = 'applications'
147 148 149 150
    permission_required = 'jobs.change_job'

    def get_permission_object(self):
        return get_object_or_404(Job, id=self.kwargs['job_pk'])
151 152 153 154

    def get_queryset(self):
        return super().get_queryset().filter(job_id=self.kwargs['job_pk'])

Aatish Neupane's avatar
Aatish Neupane committed
155

156
class ApplicationDetail(LoginRequiredMixin, DetailView):
157
    model = Application