2023-12-16 09:29:22 +05:30
|
|
|
import logging, os
|
2023-11-23 12:20:11 +05:30
|
|
|
from urllib.parse import urlencode
|
2023-12-16 09:29:22 +05:30
|
|
|
from django.http import HttpResponse, HttpResponseRedirect, Http404
|
2023-12-14 15:27:36 +05:30
|
|
|
from django.core.exceptions import ObjectDoesNotExist
|
2023-05-10 15:06:57 +05:30
|
|
|
from django.shortcuts import redirect
|
2023-11-23 12:20:11 +05:30
|
|
|
from django.urls import reverse
|
2023-05-10 15:06:57 +05:30
|
|
|
from employee.models import Employee, EmployeeWorkInformation
|
|
|
|
|
from django.contrib import messages
|
2023-12-14 15:27:36 +05:30
|
|
|
from django.shortcuts import render
|
2023-12-16 09:29:22 +05:30
|
|
|
from horilla.settings import TEMPLATES, BASE_DIR
|
|
|
|
|
from horilla import settings
|
|
|
|
|
|
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.")
|
|
|
|
|
return HttpResponseRedirect(request.META.get("HTTP_REFERER", "/"))
|
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):
|
2023-12-16 09:29:22 +05:30
|
|
|
if request.user.has_perm(
|
|
|
|
|
kwargs["model"]._meta.app_label
|
|
|
|
|
+ ".delete_"
|
|
|
|
|
+ kwargs["model"]._meta.model_name
|
|
|
|
|
):
|
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.")
|
|
|
|
|
return HttpResponseRedirect(request.META.get("HTTP_REFERER", "/"))
|
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):
|
|
|
|
|
user = request.user
|
|
|
|
|
employee = Employee.objects.filter(employee_user_id=user).first()
|
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.")
|
|
|
|
|
return HttpResponseRedirect(request.META.get("HTTP_REFERER", "/"))
|
|
|
|
|
|
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:
|
|
|
|
|
return render(request,"went_wrong.html")
|
|
|
|
|
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
|