2018-10-17 09:39:31 +00:00
|
|
|
from django.urls import reverse_lazy
|
2018-10-17 11:18:34 +00:00
|
|
|
from django.http import HttpResponseRedirect
|
2018-10-26 08:30:31 +00:00
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
2018-10-20 07:48:55 +00:00
|
|
|
from django.views.generic import (View, TemplateView, ListView, CreateView,
|
|
|
|
DetailView)
|
|
|
|
from django.views.generic.detail import SingleObjectMixin
|
2018-10-26 08:30:31 +00:00
|
|
|
from django.contrib import messages
|
|
|
|
from django.shortcuts import get_object_or_404
|
2018-10-20 07:48:55 +00:00
|
|
|
from rules.contrib.views import PermissionRequiredMixin
|
2018-10-17 08:51:51 +00:00
|
|
|
|
2018-10-20 06:59:28 +00:00
|
|
|
from .models import Job, Application, Question
|
|
|
|
from .forms import JobForm, QuestionFormSet, ApplicationForm, AnswerForm
|
2018-10-17 09:39:31 +00:00
|
|
|
|
2018-10-17 08:51:51 +00:00
|
|
|
|
|
|
|
class Index(TemplateView):
|
|
|
|
template_name = 'jobs/index.html'
|
|
|
|
|
2018-10-26 09:35:08 +00:00
|
|
|
def get_context_data(self, *args, **kwargs):
|
|
|
|
context = super().get_context_data(*args, **kwargs)
|
|
|
|
context['jobs'] = Job.objects.all().order_by('-updated')[:3]
|
|
|
|
return context
|
|
|
|
|
2018-10-17 08:51:51 +00:00
|
|
|
|
|
|
|
class JobList(ListView):
|
|
|
|
context_object_name = 'jobs'
|
|
|
|
model = Job
|
2018-10-17 07:35:54 +00:00
|
|
|
|
2018-10-17 09:39:31 +00:00
|
|
|
|
2018-10-17 11:31:10 +00:00
|
|
|
class JobDetail(DetailView):
|
|
|
|
context_object_name = 'job'
|
|
|
|
model = Job
|
|
|
|
|
|
|
|
|
2018-10-26 08:30:31 +00:00
|
|
|
class JobRenew(LoginRequiredMixin, PermissionRequiredMixin, SingleObjectMixin,
|
|
|
|
View):
|
2018-10-20 07:48:55 +00:00
|
|
|
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(
|
2018-10-26 08:30:31 +00:00
|
|
|
request, messages.SUCCESS, 'Job has been renewed until {}.'.format(
|
2018-10-20 07:48:55 +00:00
|
|
|
job.expires.isoformat(' ', 'seconds')))
|
|
|
|
return HttpResponseRedirect(job.get_absolute_url())
|
|
|
|
|
|
|
|
|
2018-10-26 08:30:31 +00:00
|
|
|
class JobCreate(LoginRequiredMixin, CreateView):
|
2018-10-17 09:39:31 +00:00
|
|
|
model = Job
|
|
|
|
form_class = JobForm
|
2018-10-26 08:30:31 +00:00
|
|
|
success_url = reverse_lazy("jobs:job_list")
|
2018-10-17 09:39:31 +00:00
|
|
|
|
2018-10-17 11:18:34 +00:00
|
|
|
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()
|
2018-10-26 08:30:31 +00:00
|
|
|
form.save_m2m()
|
2018-10-17 11:18:34 +00:00
|
|
|
|
|
|
|
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(
|
2018-10-20 07:48:55 +00:00
|
|
|
self.get_context_data(form=form, question_form=question_form))
|
2018-10-20 06:59:28 +00:00
|
|
|
|
|
|
|
|
2018-10-26 08:30:31 +00:00
|
|
|
class ApplicationCreate(LoginRequiredMixin, CreateView):
|
2018-10-20 06:59:28 +00:00
|
|
|
# TODO: restrict users from re-application
|
|
|
|
model = Application
|
|
|
|
form_class = ApplicationForm
|
2018-10-26 08:30:31 +00:00
|
|
|
success_url = reverse_lazy("jobs:job_list")
|
2018-10-20 06:59:28 +00:00
|
|
|
|
|
|
|
def get_question_queryset(self):
|
2018-10-20 07:48:55 +00:00
|
|
|
# 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')
|
2018-10-20 06:59:28 +00:00
|
|
|
|
|
|
|
def get_answer_formset(self, *args):
|
|
|
|
questions = self.get_question_queryset()
|
2018-10-20 07:48:55 +00:00
|
|
|
return AnswerForm.inlineformset_factory(extra=questions.count())(
|
|
|
|
initial=[{
|
2018-10-20 06:59:28 +00:00
|
|
|
'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
|
2018-10-26 08:30:31 +00:00
|
|
|
self.object.job = get_object_or_404(Job, id=self.kwargs['job_pk'])
|
2018-10-20 06:59:28 +00:00
|
|
|
self.object.save()
|
2018-10-26 08:30:31 +00:00
|
|
|
form.save_m2m()
|
2018-10-20 06:59:28 +00:00
|
|
|
|
|
|
|
answer_form.instance = self.object
|
|
|
|
answer_form.save()
|
|
|
|
|
2018-10-26 08:30:31 +00:00
|
|
|
messages.add_message(
|
|
|
|
self.request, messages.SUCCESS,
|
|
|
|
'Your application has been succesfully received.')
|
|
|
|
|
2018-10-20 06:59:28 +00:00
|
|
|
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))
|
2018-10-20 08:15:59 +00:00
|
|
|
|
|
|
|
|
2018-10-26 08:30:31 +00:00
|
|
|
class ApplicationList(LoginRequiredMixin, PermissionRequiredMixin, ListView):
|
2018-10-20 08:15:59 +00:00
|
|
|
model = Application
|
|
|
|
context_object_name = 'applications'
|
2018-10-26 08:30:31 +00:00
|
|
|
permission_required = 'jobs.change_job'
|
|
|
|
|
|
|
|
def get_permission_object(self):
|
|
|
|
return get_object_or_404(Job, id=self.kwargs['job_pk'])
|
2018-10-20 08:15:59 +00:00
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
return super().get_queryset().filter(job_id=self.kwargs['job_pk'])
|
|
|
|
|
2018-10-26 08:30:31 +00:00
|
|
|
|
|
|
|
class ApplicationDetail(LoginRequiredMixin, DetailView):
|
2018-10-20 08:15:59 +00:00
|
|
|
model = Application
|