2021-01-13 19:41:55 +01:00
|
|
|
from django.contrib.auth.decorators import login_required
|
|
|
|
from django.contrib.messages.views import SuccessMessageMixin
|
2021-01-13 21:11:38 +01:00
|
|
|
from django.shortcuts import get_object_or_404
|
2021-01-13 19:41:55 +01:00
|
|
|
from django.utils.decorators import method_decorator
|
2020-12-21 19:53:38 +01:00
|
|
|
|
2021-01-13 20:24:06 +01:00
|
|
|
from django.views.generic import TemplateView, ListView, CreateView, DetailView
|
2021-01-13 19:41:55 +01:00
|
|
|
|
|
|
|
from jobs.forms import CreateJobForm
|
2020-12-31 15:10:53 +01:00
|
|
|
from jobs.models import Job, Category
|
2020-12-28 20:28:52 +01:00
|
|
|
|
2020-12-31 10:36:51 +01:00
|
|
|
|
|
|
|
class HomeView(ListView):
|
|
|
|
template_name = 'jobs/index.html'
|
|
|
|
context_object_name = 'jobs'
|
|
|
|
model = Job
|
2021-01-13 21:11:38 +01:00
|
|
|
paginate_by = 3
|
2020-12-31 15:10:53 +01:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(HomeView, self).get_context_data(**kwargs)
|
|
|
|
context['categories'] = Category.objects.all()
|
|
|
|
return context
|
2021-01-13 19:41:55 +01:00
|
|
|
|
|
|
|
|
|
|
|
@method_decorator(login_required(login_url='/'), name='dispatch')
|
|
|
|
class CreateJobView(SuccessMessageMixin, CreateView):
|
|
|
|
model = Job
|
|
|
|
template_name = 'jobs/create-jobs.html'
|
|
|
|
form_class = CreateJobForm
|
|
|
|
success_url = '/'
|
|
|
|
success_message = "Job has been posted."
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
job = form.save(commit=False)
|
|
|
|
job.employer = self.request.user
|
|
|
|
job.save()
|
|
|
|
return super(CreateJobView, self).form_valid(form)
|
2021-01-13 20:24:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
class SingleJobView(DetailView):
|
|
|
|
template_name = 'jobs/single.html'
|
|
|
|
model = Job
|
|
|
|
context_object_name = 'job'
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(SingleJobView, self).get_context_data(**kwargs)
|
|
|
|
context['categories'] = Category.objects.all()
|
|
|
|
return context
|
2021-01-13 21:11:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
class CategoryDetailView(ListView):
|
|
|
|
model = Job
|
|
|
|
template_name = 'jobs/category-detail.html'
|
|
|
|
context_object_name = 'jobs'
|
|
|
|
paginate_by = 2
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
self.category = get_object_or_404(Category, pk=self.kwargs['pk'])
|
|
|
|
return Job.objects.filter(category=self.category)
|
|
|
|
|
|
|
|
def get_context_data(self, *args, **kwargs):
|
|
|
|
context = super(CategoryDetailView, self).get_context_data(*args, **kwargs)
|
|
|
|
self.category = get_object_or_404(Category, pk=self.kwargs['pk'])
|
|
|
|
context['categories'] = Category.objects.all()
|
|
|
|
context['category'] = self.category
|
|
|
|
return context
|
|
|
|
|