Build a Job Search Portal with Django - Part 5
By: Kumar Shubham
Tags: Programming, Bycetutorial, Coding, Django, Python

views.py

Now we will deal with the views.py file which contains all the views which are related to the recruiters part of our app.

It is the longest part of the backend section of the Recruiters app since many things are working in this part and almost all the logic of the app is being written here. So, we will try to break these in small parts, taking a single function at a time and discussing it.

First up, we need to import various things, all of which has separate functions. We will discuss them when they are being used.

So, let’s start:-

rec_details

This is a very simple function which has a simple work — to serve the recruiters' details page.

def rec_details(request):

context = {

'rec_home_page': "active",

'rec_navbar': 1,

}

return render(request, 'recruiters/details.html', context)

add_job

This function is used to add a new job post. It requires a user to be logged in to be able to perform this function. It displays the NewJobForm and then parses details from the form and save it into the database. It then redirects it to the job list page.

@login_required

def add_job(request):

user = request.user

if request.method == "POST":

form = NewJobForm(request.POST)

if form.is_valid():

data = form.save(commit=False)

data.recruiter = user

data.save()

return redirect('job-list')

else:

form = NewJobForm()

context = {

'add_job_page': "active",

'form': form,

'rec_navbar': 1,

}

return render(request, 'recruiters/add_job.html', context)

edit_job

This function is used to update an existing job post. It requires a user to be logged in to be able to perform this function. It displays the NewJobForm with an instance with the already existing details and then parses details from the form and save it into the database. It then redirects it to the job detail page.

@login_required

def edit_job(request, slug):

user = request.user

job = get_object_or_404(Job, slug=slug)

if request.method == "POST":

form = NewJobForm(request.POST, instance=job)

if form.is_valid():

data = form.save(commit=False)

data.save()

return redirect('add-job-detail', slug)

else:

form = NewJobForm(instance=job)

context = {

'form': form,

'rec_navbar': 1,

'job': job,

}

return render(request, 'recruiters/edit_job.html', context)

job_detail

This function is used to display the details of a particular job post. It is used to display the full information of the job post.

@login_required

def job_detail(request, slug):

job = get_object_or_404(Job, slug=slug)

context = {

'job': job,

'rec_navbar': 1,

}

return render(request, 'recruiters/job_detail.html', context)

all_jobs

This function is used to get and display all the jobs posted by a recruiter. It also uses a paginator to restrict the job posts to 20 per page.

@login_required

def all_jobs(request):

jobs = Job.objects.filter(recruiter=request.user).order_by('-date_posted')

paginator = Paginator(jobs, 20)

page_number = request.GET.get('page')

page_obj = paginator.get_page(page_number)

context = {

'manage_jobs_page': "active",

'jobs': page_obj,

'rec_navbar': 1,

}

return render(request, 'recruiters/job_posts.html', context)

search_candidates

This function lets a recruiter search for candidates based on location and job type they are looking for. This is like an access to the resume database and the recruiter can scan through resumes of all the candidates. Location and job type help us provide more relevant candidates for recruiters.

@login_required

def search_candidates(request):

profile_list = Profile.objects.all()

profiles = []

for profile in profile_list:

if profile.resume and profile.user != request.user:

profiles.append(profile)



rec1 = request.GET.get('r')

rec2 = request.GET.get('s')



if rec1 == None:

li1 = Profile.objects.all()

else:

li1 = Profile.objects.filter(location__icontains=rec1)



if rec2 == None:

li2 = Profile.objects.all()

else:

li2 = Profile.objects.filter(looking_for__icontains=rec2)



final = []

profiles_final = []



for i in li1:

if i in li2:

final.append(i)



for i in final:

if i in profiles:

profiles_final.append(i)



paginator = Paginator(profiles_final, 20)

page_number = request.GET.get('page')

page_obj = paginator.get_page(page_number)

context = {

'search_candidates_page': "active",

'rec_navbar': 1,

'profiles': page_obj,

}

return render(request, 'recruiters/candidate_search.html', context)

job_candidate_search

This function is very similar to the intelligent search feature we had in the candidate's section of our App. Now, we have a similar function here which does just the opposite work.

This function allows the recruiter to see candidates which are perfect matches to a particular job post. It works similarly and matches the skill set required for the job with the skill set of candidates. It also filters out candidates based on the job type.

Then it displays a list of candidates who are perfect matches for the job post to the recruiter in order of most perfect being first up.

@login_required

def job_candidate_search(request, slug):

job = get_object_or_404(Job, slug=slug)

relevant_candidates = []

common = []

applicants = Profile.objects.filter(looking_for=job.job_type)

job_skills = []

skills = str(job.skills_req).split(",")

for skill in skills:

job_skills.append(skill.strip().lower())

for applicant in applicants:

user = applicant.user

skill_list = list(Skill.objects.filter(user=user))

skills = []

for i in skill_list:

skills.append(i.skill.lower())

common_skills = list(set(job_skills) & set(skills))

if (len(common_skills) != 0 and len(common_skills) >= len(job_skills)//2):

relevant_candidates.append(applicant)

common.append(len(common_skills))

objects = zip(relevant_candidates, common)

objects = sorted(objects, key=lambda t: t[1], reverse=True)

objects = objects[:100]

context = {

'rec_navbar': 1,

'job': job,

'objects': objects,

'job_skills': len(job_skills),

'relevant': len(relevant_candidates),



}

return render(request, 'recruiters/job_candidate_search.html', context)

applicant_list

This function is used to display the candidates who have applied for a particular job post. It first gets the applicants and then it gets the profile of all the applicants by filtering based on the applicant’s username.

@login_required

def applicant_list(request, slug):

job = get_object_or_404(Job, slug=slug)

applicants = Applicants.objects.filter(job=job).order_by('date_posted')

profiles = []

for applicant in applicants:

profile = Profile.objects.filter(user=applicant.applicant).first()

profiles.append(profile)

context = {

'rec_navbar': 1,

'profiles': profiles,



'job': job,

}

return render(request, 'recruiters/applicant_list.html', context)

selected_list

This function is similar to the Applicants list but contains the applicant’s profile who have been selected from the applicant’s list by the recruiter.

@login_required

def selected_list(request, slug):

job = get_object_or_404(Job, slug=slug)

selected = Selected.objects.filter(job=job).order_by('date_posted')

profiles = []

for applicant in selected:

profile = Profile.objects.filter(user=applicant.applicant).first()

profiles.append(profile)

context = {

'rec_navbar': 1,

'profiles': profiles,

'job': job,

}

return render(request, 'recruiters/selected_list.html', context)

select_applicant

This function is used to select an applicant from the Applicant’s list. It creates an object for that candidate in the selected list. It then deletes the candidate from the Applicants list after adding it to the Selected list.

@login_required

def select_applicant(request, can_id, job_id):

job = get_object_or_404(Job, slug=job_id)

profile = get_object_or_404(Profile, slug=can_id)

user = profile.user

selected, created = Selected.objects.get_or_create(job=job, applicant=user)

applicant = Applicants.objects.filter(job=job, applicant=user).first()

applicant.delete()

return HttpResponseRedirect('/hiring/job/{}/applicants'.format(job.slug))

remove_applicant

This function is used to reject an applicant who has applied to a particular job post. It deletes the candidate from the Applicants list.

@login_required

def remove_applicant(request, can_id, job_id):

job = get_object_or_404(Job, slug=job_id)

profile = get_object_or_404(Profile, slug=can_id)

user = profile.user

applicant = Applicants.objects.filter(job=job, applicant=user).first()

applicant.delete()

return HttpResponseRedirect('/hiring/job/{}/applicants'.format(job.slug))

So, that sums up the views.py file of the Recruiters App. Here’s a look at the complete code:-