2024-01-23 15:21:53 +05:30
|
|
|
"""
|
|
|
|
|
offboarding/decorators.py
|
|
|
|
|
|
|
|
|
|
This module is used to write custom authentication decorators for offboarding module
|
|
|
|
|
"""
|
2024-03-10 19:37:46 +05:30
|
|
|
|
2024-01-23 15:21:53 +05:30
|
|
|
from django.contrib import messages
|
2024-01-24 12:28:16 +05:30
|
|
|
from django.http import HttpResponse, HttpResponseRedirect
|
2024-02-05 13:59:30 +05:30
|
|
|
from django.shortcuts import render
|
2024-01-23 15:21:53 +05:30
|
|
|
from horilla.decorators import decorator_with_arguments
|
2024-01-29 11:47:23 +05:30
|
|
|
from offboarding.models import (
|
|
|
|
|
Offboarding,
|
|
|
|
|
OffboardingGeneralSetting,
|
|
|
|
|
OffboardingStage,
|
|
|
|
|
OffboardingTask,
|
|
|
|
|
)
|
2024-01-23 15:21:53 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
@decorator_with_arguments
|
2024-01-24 12:28:16 +05:30
|
|
|
def any_manager_can_enter(function, perm, offboarding_employee_can_enter=False):
|
2024-01-23 15:21:53 +05:30
|
|
|
def _function(request, *args, **kwargs):
|
|
|
|
|
employee = request.user.employee_get
|
2024-01-24 12:28:16 +05:30
|
|
|
if (
|
|
|
|
|
request.user.has_perm(perm)
|
|
|
|
|
or offboarding_employee_can_enter
|
|
|
|
|
or (
|
|
|
|
|
Offboarding.objects.filter(managers=employee).exists()
|
|
|
|
|
| OffboardingStage.objects.filter(managers=employee).exists()
|
|
|
|
|
| OffboardingTask.objects.filter(managers=employee).exists()
|
|
|
|
|
)
|
2024-01-23 15:21:53 +05:30
|
|
|
):
|
|
|
|
|
return function(request, *args, **kwargs)
|
|
|
|
|
else:
|
2024-02-03 14:00:42 +05:30
|
|
|
previous_url = request.META.get("HTTP_REFERER", "/")
|
|
|
|
|
script = f'<script>window.location.href = "{previous_url}"</script>'
|
2024-02-05 13:59:30 +05:30
|
|
|
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-03 14:00:42 +05:30
|
|
|
return HttpResponse(script)
|
2024-01-23 15:21:53 +05:30
|
|
|
|
|
|
|
|
return _function
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@decorator_with_arguments
|
|
|
|
|
def offboarding_manager_can_enter(function, perm):
|
|
|
|
|
def _function(request, *args, **kwargs):
|
|
|
|
|
employee = request.user.has_perm(perm)
|
|
|
|
|
if (
|
|
|
|
|
request.user.has_perm(perm)
|
|
|
|
|
or Offboarding.objects.filter(managers=employee).exists()
|
|
|
|
|
):
|
|
|
|
|
return function(request, *args, **kwargs)
|
|
|
|
|
else:
|
|
|
|
|
messages.info(request, "You dont have permission.")
|
2024-02-03 14:00:42 +05:30
|
|
|
previous_url = request.META.get("HTTP_REFERER", "/")
|
|
|
|
|
script = f'<script>window.location.href = "{previous_url}"</script>'
|
2024-02-05 13:59:30 +05:30
|
|
|
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-03 14:00:42 +05:30
|
|
|
return HttpResponse(script)
|
2024-01-23 15:21:53 +05:30
|
|
|
|
|
|
|
|
return _function
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@decorator_with_arguments
|
|
|
|
|
def offboarding_or_stage_manager_can_enter(function, perm):
|
|
|
|
|
def _function(request, *args, **kwargs):
|
|
|
|
|
employee = request.user.has_perm(perm)
|
|
|
|
|
if (
|
|
|
|
|
request.user.has_perm(perm)
|
|
|
|
|
or Offboarding.objects.filter(managers=employee).exists()
|
|
|
|
|
or OffboardingStage.objects.filter(managers=employee).exists()
|
|
|
|
|
):
|
|
|
|
|
return function(request, *args, **kwargs)
|
|
|
|
|
else:
|
|
|
|
|
messages.info(request, "You dont have permission.")
|
2024-02-03 14:00:42 +05:30
|
|
|
previous_url = request.META.get("HTTP_REFERER", "/")
|
2024-02-05 13:59:30 +05:30
|
|
|
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-03 14:00:42 +05:30
|
|
|
script = f'<script>window.location.href = "{previous_url}"</script>'
|
|
|
|
|
return HttpResponse(script)
|
2024-01-23 15:21:53 +05:30
|
|
|
|
|
|
|
|
return _function
|
2024-01-29 11:47:23 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
@decorator_with_arguments
|
2024-02-26 20:17:39 +05:30
|
|
|
def check_feature_enabled(function, feature_name):
|
2024-01-29 11:47:23 +05:30
|
|
|
def _function(request, *args, **kwargs):
|
|
|
|
|
general_setting = OffboardingGeneralSetting.objects.first()
|
|
|
|
|
enabled = getattr(general_setting, feature_name, False)
|
|
|
|
|
if enabled:
|
|
|
|
|
return function(request, *args, **kwargs)
|
|
|
|
|
messages.info(request, "Feature is not enabled on the settings")
|
2024-02-03 14:00:42 +05:30
|
|
|
previous_url = request.META.get("HTTP_REFERER", "/")
|
2024-02-05 13:59:30 +05:30
|
|
|
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-03 14:00:42 +05:30
|
|
|
script = f'<script>window.location.href = "{previous_url}"</script>'
|
|
|
|
|
return HttpResponse(script)
|
2024-01-29 11:47:23 +05:30
|
|
|
|
|
|
|
|
return _function
|