forked from horilla-opensource/horilla
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecorators.py
103 lines (88 loc) · 3.71 KB
/
decorators.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
"""
offboarding/decorators.py
This module is used to write custom authentication decorators for offboarding module
"""
from django.contrib import messages
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
from horilla.decorators import decorator_with_arguments
from offboarding.models import (
Offboarding,
OffboardingGeneralSetting,
OffboardingStage,
OffboardingTask,
)
@decorator_with_arguments
def any_manager_can_enter(function, perm, offboarding_employee_can_enter=False):
def _function(request, *args, **kwargs):
employee = request.user.employee_get
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()
)
):
return function(request, *args, **kwargs)
else:
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)
return _function
@decorator_with_arguments
def offboarding_manager_can_enter(function, perm):
def _function(request, *args, **kwargs):
employee = request.user.employee_get
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.")
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)
return _function
@decorator_with_arguments
def offboarding_or_stage_manager_can_enter(function, perm):
def _function(request, *args, **kwargs):
employee = request.user.employee_get
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.")
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)
return _function
@decorator_with_arguments
def check_feature_enabled(function, feature_name):
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")
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)
return _function