2024-01-01 18:44:28 +05:30
|
|
|
"""
|
|
|
|
|
employee/context_processors.py
|
|
|
|
|
|
|
|
|
|
This module is used to write context processor methods
|
|
|
|
|
"""
|
2024-03-07 11:57:28 +05:30
|
|
|
|
2024-05-30 10:01:49 +05:30
|
|
|
import json
|
2024-01-01 18:44:28 +05:30
|
|
|
from datetime import date
|
2024-05-07 12:23:36 +05:30
|
|
|
|
2024-01-01 18:44:28 +05:30
|
|
|
from django import template
|
|
|
|
|
from django.contrib import messages
|
|
|
|
|
from django.core.mail import EmailMessage
|
2024-05-07 12:23:36 +05:30
|
|
|
from django.core.paginator import Paginator
|
|
|
|
|
from django.http import HttpResponse, JsonResponse
|
|
|
|
|
from django.shortcuts import render
|
|
|
|
|
|
|
|
|
|
from base.backends import ConfiguredEmailBackend
|
2024-01-01 18:44:28 +05:30
|
|
|
from base.methods import generate_pdf
|
2024-05-07 12:23:36 +05:30
|
|
|
from employee.filters import EmployeeFilter
|
2024-01-01 18:44:28 +05:30
|
|
|
from employee.models import Employee
|
|
|
|
|
from horilla import settings
|
2024-05-07 12:23:36 +05:30
|
|
|
from horilla.decorators import login_required, manager_can_enter
|
2024-01-01 18:44:28 +05:30
|
|
|
from recruitment.models import RecruitmentMailTemplate
|
2024-01-12 21:24:01 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
def paginator_qry(qryset, page_number):
|
|
|
|
|
"""
|
|
|
|
|
This method is used to paginate query set
|
|
|
|
|
"""
|
|
|
|
|
paginator = Paginator(qryset, 20)
|
|
|
|
|
qryset = paginator.get_page(page_number)
|
|
|
|
|
return qryset
|
2024-01-01 18:44:28 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
|
@manager_can_enter("employee.view_employee")
|
|
|
|
|
def not_in_yet(request):
|
|
|
|
|
"""
|
|
|
|
|
This context processor wil return the employees, if they not marked the attendance
|
|
|
|
|
for the day
|
|
|
|
|
"""
|
2024-01-12 21:24:01 +05:30
|
|
|
page_number = request.GET.get("page")
|
|
|
|
|
previous_data = request.GET.urlencode()
|
2024-01-04 16:52:12 +05:30
|
|
|
emps = (
|
|
|
|
|
EmployeeFilter({"not_in_yet": date.today()})
|
|
|
|
|
.qs.exclude(employee_work_info__isnull=True)
|
|
|
|
|
.filter(is_active=True)
|
2024-01-01 18:44:28 +05:30
|
|
|
)
|
2024-03-07 11:57:28 +05:30
|
|
|
|
|
|
|
|
return render(
|
|
|
|
|
request,
|
|
|
|
|
"dashboard/not_in_yet.html",
|
|
|
|
|
{
|
|
|
|
|
"employees": paginator_qry(emps, page_number),
|
|
|
|
|
"pd": previous_data,
|
|
|
|
|
},
|
|
|
|
|
)
|
2024-01-01 18:44:28 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
|
@manager_can_enter("employee.view_employee")
|
|
|
|
|
def not_out_yet(request):
|
|
|
|
|
"""
|
|
|
|
|
This context processor wil return the employees, if they not marked the attendance
|
|
|
|
|
for the day
|
|
|
|
|
"""
|
2024-01-04 16:52:12 +05:30
|
|
|
emps = (
|
|
|
|
|
EmployeeFilter({"not_out_yet": date.today()})
|
|
|
|
|
.qs.exclude(employee_work_info__isnull=True)
|
|
|
|
|
.filter(is_active=True)
|
2024-01-01 18:44:28 +05:30
|
|
|
)
|
|
|
|
|
return render(request, "dashboard/not_out_yet.html", {"employees": emps})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
|
@manager_can_enter("employee.change_employee")
|
2024-05-30 10:01:49 +05:30
|
|
|
def send_mail(request, emp_id=None):
|
2024-01-01 18:44:28 +05:30
|
|
|
"""
|
|
|
|
|
This method used send mail to the employees
|
|
|
|
|
"""
|
2024-05-30 10:01:49 +05:30
|
|
|
employee = None
|
|
|
|
|
if emp_id:
|
|
|
|
|
employee = Employee.objects.get(id=emp_id)
|
|
|
|
|
employees = Employee.objects.all()
|
|
|
|
|
|
2024-01-01 18:44:28 +05:30
|
|
|
templates = RecruitmentMailTemplate.objects.all()
|
|
|
|
|
return render(
|
|
|
|
|
request,
|
|
|
|
|
"employee/send_mail.html",
|
2024-05-30 10:01:49 +05:30
|
|
|
{"employee": employee, "templates": templates, "employees": employees},
|
2024-01-01 18:44:28 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
|
def get_template(request, emp_id):
|
|
|
|
|
"""
|
|
|
|
|
This method is used to return the mail template
|
|
|
|
|
"""
|
|
|
|
|
body = RecruitmentMailTemplate.objects.get(id=emp_id).body
|
|
|
|
|
instance_id = request.GET.get("instance_id")
|
|
|
|
|
if instance_id:
|
|
|
|
|
instance = Employee.objects.get(id=instance_id)
|
|
|
|
|
template_bdy = template.Template(body)
|
|
|
|
|
context = template.Context(
|
|
|
|
|
{"instance": instance, "self": request.user.employee_get}
|
|
|
|
|
)
|
|
|
|
|
body = template_bdy.render(context)
|
|
|
|
|
|
|
|
|
|
return JsonResponse({"body": body})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
|
@manager_can_enter(perm="recruitment.change_employee")
|
|
|
|
|
def send_mail_to_employee(request):
|
|
|
|
|
"""
|
|
|
|
|
This method is used to send acknowledgement mail to the candidate
|
|
|
|
|
"""
|
|
|
|
|
employee_id = request.POST["id"]
|
|
|
|
|
subject = request.POST.get("subject")
|
|
|
|
|
bdy = request.POST.get("body")
|
2024-05-30 10:01:49 +05:30
|
|
|
|
|
|
|
|
employee_ids = request.POST.getlist("employees")
|
|
|
|
|
employees = Employee.objects.filter(id__in=employee_ids)
|
|
|
|
|
|
2024-01-01 18:44:28 +05:30
|
|
|
other_attachments = request.FILES.getlist("other_attachments")
|
|
|
|
|
attachments = [
|
|
|
|
|
(file.name, file.read(), file.content_type) for file in other_attachments
|
|
|
|
|
]
|
2024-03-07 11:57:28 +05:30
|
|
|
email_backend = ConfiguredEmailBackend()
|
2024-07-12 07:15:37 +02:00
|
|
|
host = email_backend.dynamic_from_email_with_display_name
|
2024-05-30 10:01:49 +05:30
|
|
|
|
|
|
|
|
if employee_id:
|
|
|
|
|
employee_obj = Employee.objects.filter(id=employee_id)
|
|
|
|
|
else:
|
|
|
|
|
employee_obj = Employee.objects.none()
|
|
|
|
|
employees = (employees | employee_obj).distinct()
|
|
|
|
|
|
2024-01-01 18:44:28 +05:30
|
|
|
template_attachment_ids = request.POST.getlist("template_attachments")
|
2024-05-30 10:01:49 +05:30
|
|
|
for employee in employees:
|
|
|
|
|
bodys = list(
|
|
|
|
|
RecruitmentMailTemplate.objects.filter(
|
|
|
|
|
id__in=template_attachment_ids
|
|
|
|
|
).values_list("body", flat=True)
|
|
|
|
|
)
|
|
|
|
|
for html in bodys:
|
|
|
|
|
# due to not having solid template we first need to pass the context
|
|
|
|
|
template_bdy = template.Template(html)
|
|
|
|
|
context = template.Context(
|
|
|
|
|
{"instance": employee, "self": request.user.employee_get}
|
|
|
|
|
)
|
|
|
|
|
render_bdy = template_bdy.render(context)
|
|
|
|
|
attachments.append(
|
|
|
|
|
(
|
|
|
|
|
"Document",
|
|
|
|
|
generate_pdf(render_bdy, {}, path=False, title="Document").content,
|
|
|
|
|
"application/pdf",
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
template_bdy = template.Template(bdy)
|
2024-01-01 18:44:28 +05:30
|
|
|
context = template.Context(
|
|
|
|
|
{"instance": employee, "self": request.user.employee_get}
|
|
|
|
|
)
|
|
|
|
|
render_bdy = template_bdy.render(context)
|
2024-05-30 10:01:49 +05:30
|
|
|
send_to_mail = (
|
|
|
|
|
employee.employee_work_info.email
|
|
|
|
|
if employee.employee_work_info and employee.employee_work_info.email
|
|
|
|
|
else employee.email
|
2024-01-01 18:44:28 +05:30
|
|
|
)
|
|
|
|
|
|
2024-05-30 10:01:49 +05:30
|
|
|
email = EmailMessage(
|
|
|
|
|
subject,
|
|
|
|
|
render_bdy,
|
|
|
|
|
host,
|
|
|
|
|
[send_to_mail],
|
|
|
|
|
)
|
|
|
|
|
email.content_subtype = "html"
|
|
|
|
|
|
|
|
|
|
email.attachments = attachments
|
|
|
|
|
try:
|
|
|
|
|
email.send()
|
|
|
|
|
if employee.employee_work_info.email or employee.email:
|
|
|
|
|
messages.success(request, f"Mail sent to {employee.get_full_name()}")
|
|
|
|
|
else:
|
|
|
|
|
messages.info(request, f"Email not set for {employee.get_full_name()}")
|
|
|
|
|
except Exception as e:
|
|
|
|
|
messages.error(request, "Something went wrong")
|
2024-01-01 18:44:28 +05:30
|
|
|
return HttpResponse("<script>window.location.reload()</script>")
|