2023-11-08 16:58:23 +05:30
|
|
|
"""
|
|
|
|
|
decorator functions for leave
|
|
|
|
|
"""
|
2024-03-10 19:37:46 +05:30
|
|
|
|
2023-11-08 16:58:23 +05:30
|
|
|
from django.contrib import messages
|
2024-02-05 13:59:30 +05:30
|
|
|
from django.http import HttpResponse, HttpResponseRedirect
|
2024-02-29 18:33:36 +05:30
|
|
|
from django.shortcuts import redirect, render
|
2023-11-08 16:58:23 +05:30
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
|
|
2024-05-24 20:34:44 +05:30
|
|
|
from leave.models import LeaveGeneralSetting
|
|
|
|
|
|
2024-05-07 12:23:36 +05:30
|
|
|
from .models import LeaveAllocationRequest
|
|
|
|
|
|
2023-11-08 16:58:23 +05:30
|
|
|
decorator_with_arguments = (
|
|
|
|
|
lambda decorator: lambda *args, **kwargs: lambda func: decorator(
|
|
|
|
|
func, *args, **kwargs
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
2024-03-10 19:37:46 +05:30
|
|
|
|
2023-11-08 16:58:23 +05:30
|
|
|
@decorator_with_arguments
|
|
|
|
|
def leave_allocation_change_permission(function=None, *args, **kwargs):
|
|
|
|
|
def check_permission(
|
|
|
|
|
request,
|
|
|
|
|
req_id=None,
|
|
|
|
|
*args,
|
|
|
|
|
**kwargs,
|
|
|
|
|
):
|
|
|
|
|
"""
|
|
|
|
|
This method is used to check the employee can change a leave allocation request or not
|
|
|
|
|
"""
|
|
|
|
|
leave_allocation_request = LeaveAllocationRequest.objects.get(id=req_id)
|
|
|
|
|
if (
|
2024-03-10 19:37:46 +05:30
|
|
|
request.user.has_perm("leave.change_leaveallocationrequest")
|
|
|
|
|
or request.user.employee_get
|
|
|
|
|
== leave_allocation_request.employee_id.employee_work_info.reporting_manager_id
|
2023-11-08 16:58:23 +05:30
|
|
|
or request.user.employee_get == leave_allocation_request.employee_id
|
|
|
|
|
):
|
2024-03-10 19:37:46 +05:30
|
|
|
return function(request, *args, req_id=req_id, **kwargs)
|
|
|
|
|
messages.info(request, _("You dont have permission."))
|
2024-02-05 13:59:30 +05:30
|
|
|
previous_url = request.META.get("HTTP_REFERER", "/")
|
|
|
|
|
script = f'<script>window.location.href = "{previous_url}"</script>'
|
|
|
|
|
key = "HTTP_HX_REQUEST"
|
|
|
|
|
if key in request.META.keys():
|
2024-03-10 19:37:46 +05:30
|
|
|
return render(request, "decorator_404.html")
|
2024-02-05 13:59:30 +05:30
|
|
|
return HttpResponse(script)
|
2024-03-10 19:37:46 +05:30
|
|
|
|
2023-11-08 16:58:23 +05:30
|
|
|
return check_permission
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@decorator_with_arguments
|
|
|
|
|
def leave_allocation_delete_permission(function=None, *args, **kwargs):
|
|
|
|
|
def check_permission(
|
|
|
|
|
request,
|
|
|
|
|
req_id=None,
|
|
|
|
|
*args,
|
|
|
|
|
**kwargs,
|
|
|
|
|
):
|
|
|
|
|
"""
|
|
|
|
|
This method is used to check the employee can delete a leave allocation request or not
|
|
|
|
|
"""
|
2024-02-29 18:33:36 +05:30
|
|
|
try:
|
|
|
|
|
leave_allocation_request = LeaveAllocationRequest.objects.get(id=req_id)
|
|
|
|
|
if (
|
2024-03-10 19:37:46 +05:30
|
|
|
request.user.has_perm("leave.delete_leaveallocationrequest")
|
|
|
|
|
or request.user.employee_get
|
|
|
|
|
== leave_allocation_request.employee_id.employee_work_info.reporting_manager_id
|
2024-02-29 18:33:36 +05:30
|
|
|
or request.user.employee_get == leave_allocation_request.employee_id
|
|
|
|
|
):
|
2024-03-10 19:37:46 +05:30
|
|
|
return function(request, *args, req_id=req_id, **kwargs)
|
|
|
|
|
messages.info(request, _("You dont have permission."))
|
2024-02-29 18:33:36 +05:30
|
|
|
previous_url = request.META.get("HTTP_REFERER", "/")
|
|
|
|
|
script = f'<script>window.location.href = "{previous_url}"</script>'
|
|
|
|
|
key = "HTTP_HX_REQUEST"
|
|
|
|
|
if key in request.META.keys():
|
2024-03-10 19:37:46 +05:30
|
|
|
return render(request, "decorator_404.html")
|
2024-02-29 18:33:36 +05:30
|
|
|
return HttpResponse(script)
|
2024-03-10 19:37:46 +05:30
|
|
|
except (LeaveAllocationRequest.DoesNotExist, OverflowError, ValueError):
|
|
|
|
|
messages.error(request, _("Leave allocation request not found"))
|
2024-02-29 18:33:36 +05:30
|
|
|
return redirect("/leave/leave-allocation-request-view/")
|
2024-03-10 19:37:46 +05:30
|
|
|
|
2023-11-08 16:58:23 +05:30
|
|
|
return check_permission
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@decorator_with_arguments
|
|
|
|
|
def leave_allocation_reject_permission(function=None, *args, **kwargs):
|
|
|
|
|
def check_permission(
|
|
|
|
|
request,
|
|
|
|
|
req_id=None,
|
|
|
|
|
*args,
|
|
|
|
|
**kwargs,
|
|
|
|
|
):
|
|
|
|
|
"""
|
|
|
|
|
This method is used to check the employee can reject a leave allocation request or not
|
|
|
|
|
"""
|
2024-02-29 18:33:36 +05:30
|
|
|
try:
|
|
|
|
|
leave_allocation_request = LeaveAllocationRequest.objects.get(id=req_id)
|
|
|
|
|
if (
|
2024-03-10 19:37:46 +05:30
|
|
|
request.user.has_perm("leave.delete_leaveallocationrequest")
|
|
|
|
|
or request.user.employee_get
|
|
|
|
|
== leave_allocation_request.employee_id.employee_work_info.reporting_manager_id
|
2024-02-29 18:33:36 +05:30
|
|
|
):
|
2024-03-10 19:37:46 +05:30
|
|
|
return function(request, *args, req_id=req_id, **kwargs)
|
|
|
|
|
messages.info(request, _("You dont have permission."))
|
2024-02-29 18:33:36 +05:30
|
|
|
previous_url = request.META.get("HTTP_REFERER", "/")
|
|
|
|
|
script = f'<script>window.location.href = "{previous_url}"</script>'
|
|
|
|
|
key = "HTTP_HX_REQUEST"
|
|
|
|
|
if key in request.META.keys():
|
2024-03-10 19:37:46 +05:30
|
|
|
return render(request, "decorator_404.html")
|
2024-02-29 18:33:36 +05:30
|
|
|
return HttpResponse(script)
|
2024-03-10 19:37:46 +05:30
|
|
|
except (LeaveAllocationRequest.DoesNotExist, OverflowError, ValueError):
|
|
|
|
|
messages.error(request, _("Leave allocation request not found"))
|
2024-02-29 18:33:36 +05:30
|
|
|
return redirect("/leave/leave-allocation-request-view/")
|
2024-03-10 19:37:46 +05:30
|
|
|
|
2023-11-13 12:17:24 +05:30
|
|
|
return check_permission
|
2024-05-24 20:34:44 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
@decorator_with_arguments
|
|
|
|
|
def is_compensatory_leave_enabled(func=None, *args, **kwargs):
|
|
|
|
|
def function(request, *args, **kwargs):
|
|
|
|
|
"""
|
|
|
|
|
This function check whether the compensatory leave feature is enabled
|
|
|
|
|
"""
|
|
|
|
|
if (
|
|
|
|
|
LeaveGeneralSetting.objects.exists()
|
|
|
|
|
and LeaveGeneralSetting.objects.all().first().compensatory_leave
|
|
|
|
|
):
|
|
|
|
|
return func(request, *args, **kwargs)
|
|
|
|
|
messages.info(request, _("Sorry,Compensatory leave is not enabled."))
|
|
|
|
|
previous_url = request.META.get("HTTP_REFERER", "/")
|
|
|
|
|
script = f'<script>window.location.href = "{previous_url}"</script>'
|
|
|
|
|
return HttpResponse(script)
|
|
|
|
|
|
|
|
|
|
return function
|