jobportal/jobs/views.py

155 lines
5.6 KiB
Python
Raw Normal View History

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-14 14:54:00 +01:00
from django.db.models import Q
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
2021-01-14 20:31:47 +01:00
from django.urls import reverse
2021-01-16 08:52:53 +01:00
from django.http import HttpResponseRedirect
2021-01-15 19:58:42 +01:00
from django.views.generic import TemplateView, ListView, CreateView, DetailView, UpdateView, DeleteView
2020-12-21 19:53:38 +01:00
2021-01-15 19:58:42 +01:00
from jobs.forms import CreateJobForm, ApplyJobForm, UpdateJobForm
2020-12-31 15:10:53 +01:00
from jobs.models import Job, Category
2021-01-16 09:20:59 +01:00
from users.models import Account, Profile
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()
2021-01-16 09:20:59 +01:00
context['all_jobs'] = Job.objects.all().count()
context['candidates'] = Account.objects.filter(is_employee=True).count()
context['resumes'] = Profile.objects.exclude(resume="").count()
context['employers'] = Account.objects.filter(is_employer=True).count()
2020-12-31 15:10:53 +01:00
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
2021-01-14 20:31:47 +01:00
class SingleJobView(SuccessMessageMixin, UpdateView):
2021-01-13 20:24:06 +01:00
template_name = 'jobs/single.html'
model = Job
context_object_name = 'job'
2021-01-14 20:31:47 +01:00
form_class = ApplyJobForm
success_message = "You applied this job!"
2021-01-13 20:24:06 +01:00
def get_context_data(self, **kwargs):
context = super(SingleJobView, self).get_context_data(**kwargs)
context['categories'] = Category.objects.all()
2021-01-14 20:31:47 +01:00
context['employee_applied'] = Job.objects.get(pk=self.kwargs['pk']).employee.all().filter(id=self.request.user.id)
2021-01-15 08:24:37 +01:00
try:
context['applied_employees'] = Job.objects.get(pk=self.kwargs['pk'], employer_id=self.request.user.id).employee.all()
2021-01-15 09:48:48 +01:00
context['employer_id'] = Job.objects.get(pk=self.kwargs['pk']).employer_id
2021-01-15 08:24:37 +01:00
except:
pass
2021-01-13 20:24:06 +01:00
return context
2021-01-13 21:11:38 +01:00
2021-01-14 20:31:47 +01:00
def form_valid(self, form):
employee = self.request.user
form.instance.employee.add(employee)
form.save()
return super(SingleJobView, self).form_valid(form)
def get_success_url(self):
return reverse('jobs:single_job', kwargs={'slug':self.object.slug, "pk":self.object.pk})
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
2021-01-14 14:54:00 +01:00
class SearchJobView(ListView):
model = Job
template_name = 'jobs/search.html'
paginate_by = 2
context_object_name = 'jobs'
def get_queryset(self):
q1 = self.request.GET.get("job_title")
q2 = self.request.GET.get("job_type")
q3 = self.request.GET.get("job_location")
if q1 or q2 or q3:
return Job.objects.filter(Q(title__icontains=q1) |
Q(description__icontains=q1),
job_type=q2,
location__icontains=q3
).order_by('-id')
return Job.objects.all().order_by('-id')
def get_context_data(self, *args, **kwargs):
context = super(SearchJobView, self).get_context_data(*args, **kwargs)
context['categories'] = Category.objects.all()
return context
2021-01-15 19:58:42 +01:00
class UpdateJobView(SuccessMessageMixin ,UpdateView):
model = Job
template_name = 'jobs/update.html'
form_class = UpdateJobForm
success_message = "You updated your job!"
2021-01-16 08:52:53 +01:00
def form_valid(self, form):
form.instance.employer = self.request.user
return super(UpdateJobView, self).form_valid(form)
def get(self, request, *args, **kwargs):
self.object = self.get_object()
if self.object.employer != request.user:
return HttpResponseRedirect('/')
return super(UpdateJobView, self).get(request, *args, **kwargs)
def get_success_url(self):
return reverse('jobs:single_job', kwargs={"pk":self.object.pk, "slug":self.object.slug})
2021-01-15 19:58:42 +01:00
class DeleteJobView(SuccessMessageMixin, DeleteView):
2021-01-16 08:52:53 +01:00
model = Job
success_url = '/'
template_name = 'jobs/delete.html'
def delete(self, request, *args, **kwargs):
self.object = self.get_object()
if self.object.employer == request.user:
self.object.delete()
return HttpResponseRedirect(self.success_url)
else:
return HttpResponseRedirect(self.success_url)
def get(self, request, *args, **kwargs):
self.object = self.get_object()
if self.object.employer != request.user:
return HttpResponseRedirect('/')
return super(DeleteJobView, self).get(request, *args, **kwargs)