forked from horilla-opensource/horilla
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext_processors.py
303 lines (259 loc) · 9.24 KB
/
context_processors.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
"""
context_processor.py
This module is used to register context processor`
"""
import re
from django.apps import apps
from django.contrib import messages
from django.http import HttpResponse
from django.urls import path, reverse
from django.utils.translation import gettext_lazy as _
from base.models import Company, TrackLateComeEarlyOut
from base.urls import urlpatterns
from employee.models import (
Employee,
EmployeeGeneralSetting,
EmployeeWorkInformation,
ProfileEditFeature,
)
from horilla import horilla_apps
from horilla.decorators import hx_request_required, login_required, permission_required
from horilla.methods import get_horilla_model_class
class AllCompany:
"""
Dummy class
"""
class Urls:
url = "https://ui-avatars.com/api/?name=All+Company&background=random"
company = "All Company"
icon = Urls()
text = "All companies"
id = None
def get_last_section(path):
# Remove any trailing slash and split the path
segments = path.strip("/").split("/")
# Get the last section (the ID)
last_section = segments[-1] if segments else None
return last_section
def get_companies(request):
"""
This method will return the history additional field form
"""
companies = list(
[company.id, company.company, company.icon.url, False]
for company in Company.objects.all()
)
companies = [
[
"all",
"All Company",
"https://ui-avatars.com/api/?name=All+Company&background=random",
False,
],
] + companies
selected_company = request.session.get("selected_company")
company_selected = False
if selected_company and selected_company == "all":
companies[0][3] = True
company_selected = True
else:
for company in companies:
if str(company[0]) == selected_company:
company[3] = True
company_selected = True
return {"all_companies": companies, "company_selected": company_selected}
@login_required
@hx_request_required
@permission_required("base.change_company")
def update_selected_company(request):
"""
This method is used to update the selected company on the session
"""
company_id = request.GET.get("company_id")
request.session["selected_company"] = company_id
company = (
AllCompany()
if company_id == "all"
else (
Company.objects.filter(id=company_id).first()
if Company.objects.filter(id=company_id).first()
else AllCompany()
)
)
previous_path = request.GET.get("next", "/")
# Define the regex pattern for the path
pattern = r"^/employee/employee-view/\d+/$"
# Check if the previous path matches the pattern
if company_id != "all":
if re.match(pattern, previous_path):
employee_id = get_last_section(previous_path)
employee = Employee.objects.filter(id=employee_id).first()
if (
not EmployeeWorkInformation.objects.filter(
employee_id=employee_id
).exists()
or employee.employee_work_info.company_id != company
):
text = "Other Company"
if (
company_id
== request.user.employee_get.employee_work_info.company_id
):
text = "My Company"
if company_id == "all":
text = "All companies"
company = {
"company": company.company,
"icon": company.icon.url,
"text": text,
"id": company.id,
}
messages.error(
request, _("Employee is not working in the selected company.")
)
request.session["selected_company_instance"] = company
return HttpResponse(
f"""
<script>window.location.href = `{reverse("employee-view")}`</script>
"""
)
text = "Other Company"
if company_id == request.user.employee_get.employee_work_info.company_id:
text = "My Company"
if company_id == "all":
text = "All companies"
company = {
"company": company.company,
"icon": company.icon.url,
"text": text,
"id": company.id,
}
request.session["selected_company_instance"] = company
return HttpResponse("<script>window.location.reload();</script>")
urlpatterns.append(
path(
"update-selected-company",
update_selected_company,
name="update-selected-company",
)
)
def white_labelling_company(request):
white_labelling = getattr(horilla_apps, "WHITE_LABELLING", False)
if white_labelling:
hq = Company.objects.filter(hq=True).last()
try:
company = (
request.user.employee_get.get_company()
if request.user.employee_get.get_company()
else hq
)
except:
company = hq
return {
"white_label_company_name": company.company if company else "Horilla",
"white_label_company": company,
}
else:
return {
"white_label_company_name": "Horilla",
"white_label_company": None,
}
def resignation_request_enabled(request):
"""
Check weather resignation_request enabled of not in offboarding
"""
enabled_resignation_request = False
first = None
if apps.is_installed("offboarding"):
OffboardingGeneralSetting = get_horilla_model_class(
app_label="offboarding", model="offboardinggeneralsetting"
)
first = OffboardingGeneralSetting.objects.first()
if first:
enabled_resignation_request = first.resignation_request
return {"enabled_resignation_request": enabled_resignation_request}
def timerunner_enabled(request):
"""
Check weather resignation_request enabled of not in offboarding
"""
first = None
enabled_timerunner = True
if apps.is_installed("attendance"):
AttendanceGeneralSetting = get_horilla_model_class(
app_label="attendance", model="attendancegeneralsetting"
)
first = AttendanceGeneralSetting.objects.first()
if first:
enabled_timerunner = first.time_runner
return {"enabled_timerunner": enabled_timerunner}
def intial_notice_period(request):
"""
Check weather resignation_request enabled of not in offboarding
"""
initial = 30
first = None
if apps.is_installed("payroll"):
PayrollGeneralSetting = get_horilla_model_class(
app_label="payroll", model="payrollgeneralsetting"
)
first = PayrollGeneralSetting.objects.first()
if first:
initial = first.notice_period
return {"get_initial_notice_period": initial}
def check_candidate_self_tracking(request):
"""
This method is used to get the candidate self tracking is enabled or not
"""
candidate_self_tracking = False
if apps.is_installed("recruitment"):
RecruitmentGeneralSetting = get_horilla_model_class(
app_label="recruitment", model="recruitmentgeneralsetting"
)
first = RecruitmentGeneralSetting.objects.first()
else:
first = None
if first:
candidate_self_tracking = first.candidate_self_tracking
return {"check_candidate_self_tracking": candidate_self_tracking}
def check_candidate_self_tracking_rating(request):
"""
This method is used to check enabled/disabled of rating option
"""
rating_option = False
if apps.is_installed("recruitment"):
RecruitmentGeneralSetting = get_horilla_model_class(
app_label="recruitment", model="recruitmentgeneralsetting"
)
first = RecruitmentGeneralSetting.objects.first()
else:
first = None
if first:
rating_option = first.show_overall_rating
return {"check_candidate_self_tracking_rating": rating_option}
def get_initial_prefix(request):
"""
This method is used to get the initial prefix
"""
settings = EmployeeGeneralSetting.objects.first()
instance_id = None
prefix = "PEP"
if settings:
instance_id = settings.id
prefix = settings.badge_id_prefix
return {"get_initial_prefix": prefix, "prefix_instance_id": instance_id}
def biometric_app_exists(request):
from django.conf import settings
biometric_app_exists = "biometric" in settings.INSTALLED_APPS
return {"biometric_app_exists": biometric_app_exists}
def enable_late_come_early_out_tracking(request):
tracking = TrackLateComeEarlyOut.objects.first()
enable = tracking.is_enable if tracking else True
return {"tracking": enable, "late_come_early_out_tracking": enable}
def enable_profile_edit(request):
from accessibility.accessibility import ACCESSBILITY_FEATURE
profile_edit = ProfileEditFeature.objects.filter().first()
enable = True if profile_edit and profile_edit.is_enabled else False
if enable:
if not any(item[0] == "profile_edit" for item in ACCESSBILITY_FEATURE):
ACCESSBILITY_FEATURE.append(("profile_edit", _("Profile Edit Access")))
return {"profile_edit_enabled": enable}