2024-05-07 12:23:36 +05:30
|
|
|
import logging
|
|
|
|
|
import os
|
2023-11-23 12:20:11 +05:30
|
|
|
from urllib.parse import urlencode
|
2024-05-07 12:23:36 +05:30
|
|
|
|
|
|
|
|
from django.contrib import messages
|
2024-04-17 21:56:01 +05:30
|
|
|
from django.http import HttpResponse, HttpResponseRedirect
|
2024-05-07 12:23:36 +05:30
|
|
|
from django.shortcuts import redirect, render
|
2023-11-23 12:20:11 +05:30
|
|
|
from django.urls import reverse
|
2024-04-17 21:56:01 +05:30
|
|
|
from django.utils.translation import gettext as _
|
2024-05-07 12:23:36 +05:30
|
|
|
|
|
|
|
|
from base.models import BiometricAttendance, MultipleApprovalManagers
|
2023-05-10 15:06:57 +05:30
|
|
|
from employee.models import Employee, EmployeeWorkInformation
|
2023-12-16 09:29:22 +05:30
|
|
|
from horilla import settings
|
2024-05-07 12:23:36 +05:30
|
|
|
from horilla.settings import BASE_DIR, TEMPLATES
|
2023-12-14 15:27:36 +05:30
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
2023-12-16 09:29:22 +05:30
|
|
|
TEMPLATES[0]["DIRS"] = [os.path.join(BASE_DIR, "templates")]
|
|
|
|
|
|
|
|
|
|
decorator_with_arguments = (
|
|
|
|
|
lambda decorator: lambda *args, **kwargs: lambda func: decorator(
|
|
|
|
|
func, *args, **kwargs
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_manager(employee, instance):
|
|
|
|
|
try:
|
|
|
|
|
if isinstance(instance, Employee):
|
|
|
|
|
return instance.employee_work_info.reporting_manager_id == employee
|
|
|
|
|
return employee == instance.employee_id.employee_work_info.reporting_manager_id
|
|
|
|
|
except:
|
|
|
|
|
return False
|
|
|
|
|
|
2023-05-10 15:06:57 +05:30
|
|
|
|
|
|
|
|
@decorator_with_arguments
|
|
|
|
|
def permission_required(function, perm):
|
|
|
|
|
def _function(request, *args, **kwargs):
|
|
|
|
|
if request.user.has_perm(perm):
|
|
|
|
|
return function(request, *args, **kwargs)
|
|
|
|
|
else:
|
2023-12-16 09:29:22 +05:30
|
|
|
messages.info(request, "You dont have permission.")
|
2024-02-05 13:59:30 +05:30
|
|
|
previous_url = request.META.get("HTTP_REFERER", "/")
|
|
|
|
|
key = "HTTP_HX_REQUEST"
|
|
|
|
|
if key in request.META.keys():
|
2024-02-16 10:34:12 +05:30
|
|
|
return render(request, "decorator_404.html")
|
2024-02-05 13:59:30 +05:30
|
|
|
script = f'<script>window.location.href = "{previous_url}"</script>'
|
|
|
|
|
return HttpResponse(script)
|
2023-05-10 15:06:57 +05:30
|
|
|
|
|
|
|
|
return _function
|
|
|
|
|
|
|
|
|
|
|
2023-12-16 09:29:22 +05:30
|
|
|
decorator_with_arguments = (
|
|
|
|
|
lambda decorator: lambda *args, **kwargs: lambda func: decorator(
|
|
|
|
|
func, *args, **kwargs
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2023-11-17 14:30:30 +05:30
|
|
|
@decorator_with_arguments
|
|
|
|
|
def delete_permission(function):
|
|
|
|
|
def _function(request, *args, **kwargs):
|
2024-03-06 20:50:51 +05:30
|
|
|
user = request.user
|
|
|
|
|
employee = user.employee_get
|
|
|
|
|
is_manager = EmployeeWorkInformation.objects.filter(
|
|
|
|
|
reporting_manager_id=employee
|
|
|
|
|
).exists()
|
|
|
|
|
if (
|
|
|
|
|
request.user.has_perm(
|
|
|
|
|
kwargs["model"]._meta.app_label
|
|
|
|
|
+ ".delete_"
|
|
|
|
|
+ kwargs["model"]._meta.model_name
|
|
|
|
|
)
|
|
|
|
|
or is_manager
|
2023-12-16 09:29:22 +05:30
|
|
|
):
|
2023-11-17 14:30:30 +05:30
|
|
|
return function(request, *args, **kwargs)
|
|
|
|
|
else:
|
2023-12-16 09:29:22 +05:30
|
|
|
messages.info(request, "You dont have permission for delete.")
|
2024-02-05 13:59:30 +05:30
|
|
|
previous_url = request.META.get("HTTP_REFERER", "/")
|
|
|
|
|
key = "HTTP_HX_REQUEST"
|
|
|
|
|
if key in request.META.keys():
|
2024-02-16 10:34:12 +05:30
|
|
|
return render(request, "decorator_404.html")
|
2024-02-05 13:59:30 +05:30
|
|
|
script = f'<script>window.location.href = "{previous_url}"</script>'
|
|
|
|
|
return HttpResponse(script)
|
2024-03-06 20:50:51 +05:30
|
|
|
|
|
|
|
|
return _function
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
decorator_with_arguments = (
|
|
|
|
|
lambda decorator: lambda *args, **kwargs: lambda func: decorator(
|
|
|
|
|
func, *args, **kwargs
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@decorator_with_arguments
|
|
|
|
|
def duplicate_permission(function):
|
|
|
|
|
def _function(request, *args, **kwargs):
|
|
|
|
|
user = request.user
|
|
|
|
|
employee = user.employee_get
|
|
|
|
|
is_manager = EmployeeWorkInformation.objects.filter(
|
|
|
|
|
reporting_manager_id=employee
|
|
|
|
|
).exists()
|
2024-04-17 21:56:01 +05:30
|
|
|
|
2024-03-06 20:50:51 +05:30
|
|
|
app_label = kwargs["model"]._meta.app_label
|
2024-03-18 12:30:34 +05:30
|
|
|
model_name = kwargs["model"]._meta.model_name
|
|
|
|
|
obj_id = kwargs["obj_id"]
|
2024-04-17 21:56:01 +05:30
|
|
|
object_instance = kwargs["model"].objects.filter(pk=obj_id).first()
|
2024-03-18 12:30:34 +05:30
|
|
|
try:
|
|
|
|
|
if object_instance.employee_id == employee:
|
2024-04-17 21:56:01 +05:30
|
|
|
return function(request, *args, **kwargs)
|
2024-03-18 12:30:34 +05:30
|
|
|
except:
|
|
|
|
|
pass
|
|
|
|
|
permission = f"{app_label}.add_{model_name}"
|
2024-03-06 20:50:51 +05:30
|
|
|
if request.user.has_perm(permission) or is_manager:
|
|
|
|
|
return function(request, *args, **kwargs)
|
|
|
|
|
else:
|
|
|
|
|
messages.info(request, "You dont have permission for duplicate action.")
|
|
|
|
|
previous_url = request.META.get("HTTP_REFERER", "/")
|
|
|
|
|
key = "HTTP_HX_REQUEST"
|
|
|
|
|
if key in request.META.keys():
|
|
|
|
|
return render(request, "decorator_404.html")
|
|
|
|
|
script = f'<script>window.location.href = "{previous_url}"</script>'
|
|
|
|
|
return HttpResponse(script)
|
2023-11-17 14:30:30 +05:30
|
|
|
|
|
|
|
|
return _function
|
|
|
|
|
|
|
|
|
|
|
2023-12-16 09:29:22 +05:30
|
|
|
decorator_with_arguments = (
|
|
|
|
|
lambda decorator: lambda *args, **kwargs: lambda func: decorator(
|
|
|
|
|
func, *args, **kwargs
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
2023-05-10 15:06:57 +05:30
|
|
|
|
|
|
|
|
@decorator_with_arguments
|
|
|
|
|
def manager_can_enter(function, perm):
|
|
|
|
|
"""
|
|
|
|
|
This method is used to check permission to employee for enter to the function if the employee
|
|
|
|
|
do not have permission also checks, has reporting manager.
|
|
|
|
|
"""
|
2023-12-16 09:29:22 +05:30
|
|
|
|
2023-05-10 15:06:57 +05:30
|
|
|
def _function(request, *args, **kwargs):
|
2024-01-12 21:31:15 +05:30
|
|
|
leave_perm = [
|
|
|
|
|
"leave.view_leaverequest",
|
|
|
|
|
"leave.change_leaverequest",
|
|
|
|
|
"leave.delete_leaverequest",
|
|
|
|
|
]
|
2023-05-10 15:06:57 +05:30
|
|
|
user = request.user
|
2024-02-16 10:34:12 +05:30
|
|
|
employee = user.employee_get
|
2024-01-12 21:31:15 +05:30
|
|
|
if perm in leave_perm:
|
|
|
|
|
is_approval_manager = MultipleApprovalManagers.objects.filter(
|
|
|
|
|
employee_id=employee.id
|
|
|
|
|
).exists()
|
|
|
|
|
if is_approval_manager:
|
|
|
|
|
return function(request, *args, **kwargs)
|
2023-12-16 09:29:22 +05:30
|
|
|
is_manager = EmployeeWorkInformation.objects.filter(
|
|
|
|
|
reporting_manager_id=employee
|
|
|
|
|
).exists()
|
2023-05-10 15:06:57 +05:30
|
|
|
if user.has_perm(perm) or is_manager:
|
|
|
|
|
return function(request, *args, **kwargs)
|
|
|
|
|
else:
|
2023-12-16 09:29:22 +05:30
|
|
|
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-02-16 10:34:12 +05:30
|
|
|
return render(request, "decorator_404.html")
|
2024-02-05 13:59:30 +05:30
|
|
|
return HttpResponse(script)
|
2023-12-16 09:29:22 +05:30
|
|
|
|
2023-05-10 15:06:57 +05:30
|
|
|
return _function
|
|
|
|
|
|
|
|
|
|
|
2024-06-11 22:57:12 +05:30
|
|
|
from urllib.parse import urlparse
|
|
|
|
|
|
|
|
|
|
|
2023-05-10 15:06:57 +05:30
|
|
|
def login_required(view_func):
|
|
|
|
|
def wrapped_view(request, *args, **kwargs):
|
|
|
|
|
path = request.path
|
2023-12-16 09:29:22 +05:30
|
|
|
res = path.split("/", 2)[1].capitalize().replace("-", " ").upper()
|
|
|
|
|
if res == "PMS":
|
|
|
|
|
res = "Performance"
|
|
|
|
|
request.session["title"] = res
|
|
|
|
|
if path == "" or path == "/":
|
|
|
|
|
request.session["title"] = "Dashboard".upper()
|
2023-05-10 15:06:57 +05:30
|
|
|
if not request.user.is_authenticated:
|
2023-12-16 09:29:22 +05:30
|
|
|
login_url = reverse("login")
|
2023-11-23 12:20:11 +05:30
|
|
|
params = urlencode(request.GET)
|
2023-12-16 09:29:22 +05:30
|
|
|
url = f"{login_url}?next={request.path}"
|
2023-11-23 12:20:11 +05:30
|
|
|
if params:
|
2023-12-16 09:29:22 +05:30
|
|
|
url += f"&{params}"
|
2023-11-23 12:20:11 +05:30
|
|
|
return redirect(url)
|
2023-12-14 15:27:36 +05:30
|
|
|
try:
|
|
|
|
|
func = view_func(request, *args, **kwargs)
|
|
|
|
|
except Exception as e:
|
2024-06-11 22:57:12 +05:30
|
|
|
logger.error(e)
|
|
|
|
|
if (
|
|
|
|
|
"notifications_notification" in str(e)
|
|
|
|
|
and request.headers.get("X-Requested-With") != "XMLHttpRequest"
|
|
|
|
|
):
|
|
|
|
|
referer = request.META.get("HTTP_REFERER", "/")
|
|
|
|
|
messages.warning(request, str(e))
|
|
|
|
|
return HttpResponse(
|
|
|
|
|
f"<script>window.location.href ='{str(referer)}'</script>"
|
|
|
|
|
)
|
|
|
|
|
|
2023-12-16 09:29:22 +05:30
|
|
|
if not settings.DEBUG:
|
2024-01-12 21:31:15 +05:30
|
|
|
return render(request, "went_wrong.html")
|
2023-12-16 09:29:22 +05:30
|
|
|
return view_func(request, *args, **kwargs)
|
2023-12-14 15:27:36 +05:30
|
|
|
return func
|
2023-12-16 09:29:22 +05:30
|
|
|
|
2023-05-10 15:06:57 +05:30
|
|
|
return wrapped_view
|
|
|
|
|
|
2023-12-16 09:29:22 +05:30
|
|
|
|
2023-05-10 15:06:57 +05:30
|
|
|
def hx_request_required(view_func):
|
|
|
|
|
def wrapped_view(request, *args, **kwargs):
|
2023-12-16 09:29:22 +05:30
|
|
|
key = "HTTP_HX_REQUEST"
|
2023-05-10 15:06:57 +05:30
|
|
|
if key not in request.META.keys():
|
2024-05-24 09:41:02 +05:30
|
|
|
html_content = """
|
|
|
|
|
<!DOCTYPE html>
|
|
|
|
|
<html lang="en">
|
|
|
|
|
<head>
|
|
|
|
|
<meta charset="UTF-8">
|
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
|
|
<title>Method Not Allowed</title>
|
|
|
|
|
<style>
|
|
|
|
|
body {
|
|
|
|
|
font-family: Arial, sans-serif;
|
|
|
|
|
background-color: #f8f9fa;
|
|
|
|
|
color: #333;
|
|
|
|
|
display: flex;
|
|
|
|
|
align-items: center;
|
|
|
|
|
justify-content: center;
|
|
|
|
|
height: 100vh;
|
|
|
|
|
margin: 0;
|
|
|
|
|
}
|
|
|
|
|
.container {
|
|
|
|
|
text-align: center;
|
|
|
|
|
background: #fff;
|
|
|
|
|
padding: 20px;
|
|
|
|
|
border: 1px solid #ddd;
|
|
|
|
|
border-radius: 5px;
|
|
|
|
|
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
|
|
|
|
|
}
|
|
|
|
|
h1 {
|
|
|
|
|
font-size: 24px;
|
|
|
|
|
margin-bottom: 10px;
|
|
|
|
|
}
|
|
|
|
|
p {
|
|
|
|
|
font-size: 18px;
|
|
|
|
|
margin-bottom: 20px;
|
|
|
|
|
}
|
|
|
|
|
a {
|
|
|
|
|
color: #007bff;
|
|
|
|
|
text-decoration: none;
|
|
|
|
|
font-weight: bold;
|
|
|
|
|
}
|
|
|
|
|
a:hover {
|
|
|
|
|
text-decoration: underline;
|
|
|
|
|
}
|
|
|
|
|
</style>
|
|
|
|
|
</head>
|
|
|
|
|
<body>
|
|
|
|
|
<div class="container">
|
|
|
|
|
<h1>405 Method Not Allowed</h1>
|
|
|
|
|
<p>The request method is not allowed. Please make sure you are sending a proper request.</p>
|
|
|
|
|
<a href="/">Go Back to Home</a>
|
|
|
|
|
</div>
|
|
|
|
|
</body>
|
|
|
|
|
</html>
|
|
|
|
|
"""
|
|
|
|
|
return HttpResponse(html_content, content_type="text/html", status=405)
|
2023-05-10 15:06:57 +05:30
|
|
|
return view_func(request, *args, **kwargs)
|
2023-12-16 09:29:22 +05:30
|
|
|
|
2023-05-10 15:06:57 +05:30
|
|
|
return wrapped_view
|
|
|
|
|
|
|
|
|
|
|
2023-12-16 09:29:22 +05:30
|
|
|
@decorator_with_arguments
|
|
|
|
|
def owner_can_enter(function, perm: str, model: object, manager_access=False):
|
|
|
|
|
"""
|
|
|
|
|
Only the users with permission, or the owner, or employees manager can enter,
|
|
|
|
|
If manager_access:True then all the managers can enter
|
|
|
|
|
"""
|
2023-05-10 15:06:57 +05:30
|
|
|
|
2023-12-16 09:29:22 +05:30
|
|
|
def _function(request, *args, **kwargs):
|
|
|
|
|
instance_id = kwargs[list(kwargs.keys())[0]]
|
|
|
|
|
if model == Employee:
|
|
|
|
|
employee = Employee.objects.get(id=instance_id)
|
|
|
|
|
else:
|
2024-05-24 09:41:02 +05:30
|
|
|
try:
|
|
|
|
|
employee = model.objects.get(id=instance_id).employee_id
|
|
|
|
|
except:
|
|
|
|
|
messages.error(request, ("Sorry, something went wrong!"))
|
|
|
|
|
return HttpResponseRedirect(request.META.get("HTTP_REFERER", "/"))
|
2023-12-16 09:29:22 +05:30
|
|
|
can_enter = (
|
|
|
|
|
request.user.employee_get == employee
|
|
|
|
|
or request.user.has_perm(perm)
|
|
|
|
|
or check_manager(request.user.employee_get, employee)
|
|
|
|
|
or (
|
|
|
|
|
EmployeeWorkInformation.objects.filter(
|
|
|
|
|
reporting_manager_id__employee_user_id=request.user
|
|
|
|
|
).exists()
|
|
|
|
|
if manager_access
|
|
|
|
|
else False
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
if can_enter:
|
|
|
|
|
return function(request, *args, **kwargs)
|
|
|
|
|
return render(request, "no_perm.html")
|
2023-05-10 15:06:57 +05:30
|
|
|
|
2023-12-16 09:29:22 +05:30
|
|
|
return _function
|
2024-04-17 21:56:01 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
def install_required(function):
|
|
|
|
|
def _function(request, *args, **kwargs):
|
|
|
|
|
object = BiometricAttendance.objects.all().first()
|
|
|
|
|
if object.is_installed:
|
|
|
|
|
return function(request, *args, **kwargs)
|
|
|
|
|
else:
|
|
|
|
|
messages.info(
|
|
|
|
|
request,
|
|
|
|
|
_(
|
|
|
|
|
"Please activate the biometric attendance feature in the settings menu."
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
return HttpResponseRedirect(request.META.get("HTTP_REFERER", "/"))
|
|
|
|
|
|
|
|
|
|
return _function
|
2024-04-24 12:16:28 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
@decorator_with_arguments
|
2024-05-07 12:23:36 +05:30
|
|
|
def meeting_manager_can_enter(function, perm, answerable=False):
|
2024-04-24 12:16:28 +05:30
|
|
|
def _function(request, *args, **kwargs):
|
|
|
|
|
|
|
|
|
|
user = request.user
|
|
|
|
|
employee = user.employee_get
|
|
|
|
|
is_answer_employee = False
|
|
|
|
|
|
2024-05-07 12:23:36 +05:30
|
|
|
is_manager = (
|
|
|
|
|
Employee.objects.filter(
|
2024-04-24 12:16:28 +05:30
|
|
|
meeting_manager__isnull=False,
|
2024-05-07 12:23:36 +05:30
|
|
|
)
|
|
|
|
|
.filter(id=employee.id)
|
|
|
|
|
.exists()
|
|
|
|
|
)
|
2024-04-24 12:16:28 +05:30
|
|
|
|
|
|
|
|
if answerable:
|
2024-05-07 12:23:36 +05:30
|
|
|
is_answer_employee = (
|
|
|
|
|
Employee.objects.filter(
|
2024-04-24 16:44:25 +05:30
|
|
|
meeting_answer_employees__isnull=False,
|
2024-05-07 12:23:36 +05:30
|
|
|
)
|
|
|
|
|
.filter(id=employee.id)
|
|
|
|
|
.exists()
|
|
|
|
|
)
|
2024-04-24 12:16:28 +05:30
|
|
|
|
|
|
|
|
if user.has_perm(perm) or is_manager or is_answer_employee:
|
|
|
|
|
return function(request, *args, **kwargs)
|
|
|
|
|
else:
|
|
|
|
|
messages.info(request, "You dont have permission.")
|
|
|
|
|
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():
|
|
|
|
|
return render(request, "decorator_404.html")
|
|
|
|
|
return HttpResponse(script)
|
|
|
|
|
|
2024-05-07 12:23:36 +05:30
|
|
|
return _function
|