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-17 09:39:31 +00:00
|
|
|
from django.views.generic import (
|
2018-10-17 11:31:10 +00:00
|
|
|
TemplateView, ListView, CreateView, DetailView
|
2018-10-17 09:39:31 +00:00
|
|
|
)
|
2018-10-17 08:51:51 +00:00
|
|
|
|
|
|
|
from .models import Job
|
2018-10-17 11:18:34 +00:00
|
|
|
from .forms import JobForm, QuestionFormSet
|
2018-10-17 09:39:31 +00:00
|
|
|
|
2018-10-17 08:51:51 +00:00
|
|
|
|
|
|
|
class Index(TemplateView):
|
|
|
|
template_name = 'jobs/index.html'
|
|
|
|
|
|
|
|
|
|
|
|
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-17 09:39:31 +00:00
|
|
|
class JobCreate(CreateView):
|
|
|
|
model = Job
|
|
|
|
form_class = JobForm
|
|
|
|
success_url = reverse_lazy("jobs:list")
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
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))
|