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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
logger.exception(e)
|
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():
|
2023-12-16 09:29:22 +05:30
|
|
|
return HttpResponse("method not allowed...")
|
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:
|
|
|
|
|
employee = model.objects.get(id=instance_id).employee_id
|
|
|
|
|
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
|