forked from horilla-opensource/horilla
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforms.py
309 lines (261 loc) · 9.45 KB
/
forms.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
304
305
306
307
308
309
"""
offboarding/forms.py
This module is used to register forms for offboarding app
"""
import contextlib
from typing import Any
from django import forms
from django.contrib import messages
from django.template.loader import render_to_string
from base.forms import ModelForm
from employee.forms import MultipleFileField
from horilla import horilla_middlewares
from notifications.signals import notify
from offboarding.models import (
EmployeeTask,
Offboarding,
OffboardingEmployee,
OffboardingNote,
OffboardingStage,
OffboardingStageMultipleFile,
OffboardingTask,
ResignationLetter,
)
class OffboardingForm(ModelForm):
"""
OffboardingForm model form class
"""
verbose_name = "Offboarding"
class Meta:
model = Offboarding
fields = "__all__"
exclude = ["is_active"]
def as_p(self):
"""
Render the form fields as HTML table rows with Bootstrap styling.
"""
context = {"form": self}
table_html = render_to_string("common_form.html", context)
return table_html
class OffboardingStageForm(ModelForm):
"""
OffboardingStage model form
"""
verbose_name = "Stage"
class Meta:
model = OffboardingStage
fields = "__all__"
exclude = ["offboarding_id", "is_active"]
def as_p(self):
"""
Render the form fields as HTML table rows with Bootstrap styling.
"""
context = {"form": self}
table_html = render_to_string("common_form.html", context)
return table_html
class OffboardingEmployeeForm(ModelForm):
"""
OffboardingEmployeeForm model form
"""
verbose_name = "Offboarding "
class Meta:
model = OffboardingEmployee
fields = "__all__"
exclude = ["notice_period", "unit", "is_active"]
widgets = {
"notice_period_starts": forms.DateInput(attrs={"type": "date"}),
"notice_period_ends": forms.DateInput(attrs={"type": "date"}),
}
def as_p(self):
"""
Render the form fields as HTML table rows with Bootstrap styling.
"""
context = {"form": self}
table_html = render_to_string("common_form.html", context)
return table_html
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
attrs = self.fields["employee_id"].widget.attrs
attrs["onchange"] = "initialNoticePeriod($(this))"
self.fields["employee_id"].widget.attrs.update(attrs)
attrs = self.fields["notice_period_starts"].widget.attrs
attrs["onchange"] = "noticePeriodUpdate($(this))"
self.fields["notice_period_starts"].widget.attrs.update(attrs)
if self.instance.pk:
if self.instance.notice_period_starts:
self.initial["notice_period_starts"] = (
self.instance.notice_period_starts.strftime("%Y-%m-%d")
)
if self.instance.notice_period_ends:
self.initial["notice_period_ends"] = (
self.instance.notice_period_ends.strftime("%Y-%m-%d")
)
class StageSelectForm(ModelForm):
"""
This form is used to register drop down for the pipeline
"""
class Meta:
model = OffboardingEmployee
fields = [
"stage_id",
]
def __init__(self, *args, offboarding=None, **kwargs):
super().__init__(*args, **kwargs)
attrs = self.fields["stage_id"].widget.attrs
attrs["onchange"] = "offboardingUpdateStage($(this))"
attrs["class"] = "w-100 oh-select-custom"
self.fields["stage_id"].widget.attrs.update(attrs)
self.fields["stage_id"].empty_label = None
self.fields["stage_id"].queryset = OffboardingStage.objects.filter(
offboarding_id=offboarding
)
self.fields["stage_id"].label = ""
class NoteForm(ModelForm):
"""
Offboarding note model form
"""
verbose_name = "Add Note"
class Meta:
model = OffboardingNote
fields = "__all__"
exclude = ["is_active"]
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields["attachment"] = MultipleFileField(label="Attachements")
self.fields["attachment"].required = False
def as_p(self):
"""
Render the form fields as HTML table rows with Bootstrap styling.
"""
context = {"form": self}
table_html = render_to_string("common_form.html", context)
return table_html
def save(self, commit: bool = ...) -> Any:
multiple_attachment_ids = []
attachments = None
if self.files.getlist("attachment"):
attachments = self.files.getlist("attachment")
self.instance.attachment = attachments[0]
multiple_attachment_ids = []
for attachment in attachments:
file_instance = OffboardingStageMultipleFile()
file_instance.attachment = attachment
file_instance.save()
multiple_attachment_ids.append(file_instance.pk)
instance = super().save(commit)
if commit:
instance.attachments.add(*multiple_attachment_ids)
return instance, attachments
class TaskForm(ModelForm):
"""
TaskForm model form
"""
verbose_name = "Offboarding Task"
tasks_to = forms.ModelMultipleChoiceField(
queryset=OffboardingEmployee.objects.all(),
required=False,
)
class Meta:
model = OffboardingTask
fields = "__all__"
exclude = ["status", "is_active"]
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields["stage_id"].empty_label = "All Stages in Offboarding"
self.fields["managers"].empty_label = None
if not self.instance.pk:
queryset = OffboardingEmployee.objects.filter(
stage_id__offboarding_id=OffboardingStage.objects.filter(
id=self.initial.get("stage_id")
)
.first()
.offboarding_id
)
self.fields["tasks_to"].queryset = queryset
def as_p(self):
"""
Render the form fields as HTML table rows with Bootstrap styling.
"""
context = {"form": self}
table_html = render_to_string("common_form.html", context)
return table_html
def save(self, commit: bool = ...) -> Any:
super().save(commit)
if commit:
employees = self.cleaned_data["tasks_to"]
for employee in employees:
assigned_task = EmployeeTask.objects.get_or_create(
employee_id=employee,
task_id=self.instance,
)
class ResignationLetterForm(ModelForm):
"""
Resignation Letter
"""
description = forms.CharField(
widget=forms.Textarea(attrs={"data-summernote": "", "style": "display:none;"}),
label="Description",
)
verbose_name = "Resignation Letter"
class Meta:
model = ResignationLetter
fields = "__all__"
exclude = ["is_active"]
def as_p(self):
"""
Render the form fields as HTML table rows with Bootstrap styling.
"""
context = {"form": self}
table_html = render_to_string("common_form.html", context)
return table_html
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields["planned_to_leave_on"].widget = forms.DateInput(
attrs={"type": "date", "class": "oh-input w-100"}
)
exclude = []
if self.instance.pk:
exclude.append("employee_id")
self.verbose_name = (
self.instance.employee_id.get_full_name() + " Resignation Letter"
)
request = getattr(horilla_middlewares._thread_locals, "request", None)
if request and not request.user.has_perm("offboarding.add_offboardingemployee"):
exclude = exclude + [
"employee_id",
"status",
]
self.instance.employee_id = request.user.employee_get
exclude = list(set(exclude))
for field in exclude:
del self.fields[field]
def save(self, commit: bool = ...) -> Any:
request = getattr(horilla_middlewares._thread_locals, "request", None)
instance = self.instance
if (
not request.user.has_perm("offboarding.add_offboardingemployee")
and instance.status == "requested"
) or request.user.has_perm("add_offboardingemployee"):
instance = super().save(commit)
else:
messages.info(
request, "You cannot edit a request that has been rejected/approved"
)
if (
instance.status == "requested"
and request
and not request.user.has_perm("offboarding.add_offboardingemployee")
):
with contextlib.suppress(Exception):
notify.send(
request.user.employee_get,
recipient=self.instance.employee_id.get_reporting_manager().employee_user_id,
verb=f"{self.instance.employee_id.get_full_name()} requested for resignation.",
verb_ar=f"",
verb_de=f"",
verb_es=f"",
verb_fr=f"",
redirect="#",
icon="information",
)
return instance