jobportal/users/views.py

172 lines
5.9 KiB
Python
Raw Normal View History

2021-01-11 21:01:47 +01:00
from django.contrib.auth.decorators import login_required
2020-12-31 15:39:01 +01:00
from django.contrib.auth.views import LoginView, LogoutView
2020-12-31 15:10:53 +01:00
from django.contrib.messages.views import SuccessMessageMixin
2021-01-11 21:01:47 +01:00
from django.http import HttpResponseRedirect
2020-12-31 15:10:53 +01:00
from django.shortcuts import redirect
2021-01-11 21:01:47 +01:00
from django.urls import reverse
from django.utils.decorators import method_decorator
from django.views.generic import CreateView, UpdateView, DetailView, ListView
2020-12-28 20:28:52 +01:00
from jobs.models import Category, Job
2021-01-15 12:27:14 +01:00
from users.forms import AccountRegisterForm, UserUpdateForm, InviteEmployeeForm
2021-01-15 18:10:05 +01:00
from users.models import Profile, Account, Invite
2020-12-31 15:10:53 +01:00
class UserRegisterView(SuccessMessageMixin, CreateView):
template_name = 'users/user-register.html'
form_class = AccountRegisterForm
success_url = '/'
success_message = "Your user account has been created."
def form_valid(self, form):
user = form.save(commit=False)
user_type = form.cleaned_data['user_types']
if user_type == 'is_employee':
user.is_employee = True
elif user_type == 'is_employer':
user.is_employer = True
user.save()
return redirect(self.success_url)
2020-12-31 15:39:01 +01:00
class UserLoginView(LoginView):
template_name = 'users/login.html'
class UserLogoutView(LogoutView):
template_name = 'users/login.html'
2021-01-11 20:40:44 +01:00
2021-01-11 21:01:47 +01:00
@method_decorator(login_required(login_url='/users/login'), name='dispatch')
2021-01-11 20:40:44 +01:00
class UserUpdateView(SuccessMessageMixin, UpdateView):
model = Profile
success_message = "You updated your profile!"
template_name = 'users/update.html'
form_class = UserUpdateForm
2021-01-11 21:01:47 +01:00
def form_valid(self, form):
form.instance.user = self.request.user
return super(UserUpdateView, self).form_valid(form)
def get(self, request, *args, **kwargs):
self.object = self.get_object()
if self.object.user != request.user:
return HttpResponseRedirect('/')
return super(UserUpdateView, self).get(request, *args, **kwargs)
def get_success_url(self):
return reverse('users: update_profile', kwargs={'pk': self.object.pk})
2021-01-15 09:48:48 +01:00
2021-01-15 12:27:14 +01:00
class EmployeeProfileView(CreateView):
2021-01-15 09:48:48 +01:00
template_name = 'users/employee-profile.html'
model = Account
2021-01-15 12:27:14 +01:00
form_class = InviteEmployeeForm
2021-01-15 09:48:48 +01:00
def get_context_data(self, **kwargs):
context = super(EmployeeProfileView, self).get_context_data(**kwargs)
2021-01-15 15:19:10 +01:00
context['account'] = Account.objects.get(pk=self.kwargs['employee_id'])
context['profile'] = Profile.objects.get(user_id=self.kwargs['employee_id'])
context['job'] = Job.objects.get(id=self.kwargs['job_id'])
2021-01-15 09:48:48 +01:00
context['categories'] = Category.objects.all()
return context
2021-01-15 15:19:10 +01:00
def form_valid(self, form):
instance = form.save(commit=False)
instance.user = Account.objects.get(pk=self.kwargs['employee_id'])
instance.job = Job.objects.get(pk=self.kwargs['job_id'])
instance.save()
return super(EmployeeProfileView, self).form_valid(form)
def get_success_url(self):
return reverse('users:employer_jobs')
@method_decorator(login_required(login_url='/users/login'), name='dispatch')
class EmployerPostedJobsView(ListView):
template_name = 'users/employer-posted-jobs.html'
context_object_name = 'employer_jobs'
model = Job
paginate_by = 3
def get_queryset(self):
return Job.objects.filter(employer=self.request.user).order_by('-id')
2021-01-15 16:03:10 +01:00
@method_decorator(login_required(login_url='/users/login'), name='dispatch')
class EmployeeMessagesView(ListView):
model = Job
template_name = 'users/employee-messages.html'
paginate_by = 5
context_object_name = 'jobs'
def get_queryset(self):
return Job.objects.filter(invites__isnull=False, invites__user_id=self.request.user).order_by('-invites')
2021-01-15 18:10:05 +01:00
class EmployeeDisplayMessages(DetailView):
model = Invite
template_name = 'users/employee-display-messages.html'
context_object_name = 'invite'
def get_queryset(self):
invite = self.model.objects.filter(id=self.kwargs['pk'])
invite.update(unread=False)
return invite
def get(self, request, *args, **kwargs):
self.object = self.get_object()
if self.object.user != request.user:
return HttpResponseRedirect('/')
return super(EmployeeDisplayMessages, self).get(request, *args, **kwargs)
2021-01-16 18:35:18 +01:00
@method_decorator(login_required(login_url='/users/login'), name='dispatch')
class AddWishListView(UpdateView):
template_name = 'jobs/index.html'
model = Profile
def get(self, request, *args, **kwargs):
if self.request.user.is_employee:
job = Job.objects.get(id=self.kwargs['pk'])
profile = Profile.objects.get(user=request.user)
profile.wish_list.add(job)
return redirect('jobs:home')
else:
return redirect('jobs:home')
@method_decorator(login_required(login_url='/users/login'), name='dispatch')
class RemoveFromWishListView(UpdateView):
template_name = 'jobs/index.html'
model = Profile
def get(self, request, *args, **kwargs):
if self.request.user.is_employee:
job = Job.objects.get(id=self.kwargs['pk'])
profile = Profile.objects.get(user=request.user)
profile.wish_list.remove(job)
return redirect('jobs:home')
else:
return redirect('jobs:home')
2021-01-16 19:22:06 +01:00
class MyWishList(ListView):
template_name = 'users/my-wish-list.html'
context_object_name = 'jobs'
model = Job
paginate_by = 5
def get_queryset(self):
return Job.objects.filter(wish_list__user_id=self.request.user.id)
def get_context_data(self, **kwargs):
context = super(MyWishList, self).get_context_data(**kwargs)
if self.request.user.is_authenticated:
context['wish_list'] = Job.objects.filter(wish_list__user_id=self.request.user.id).values_list('id',
flat=True)
return context