Files
ihrm/offboarding/forms.py
Horilla 2fee7c18bb [IMP] Remove inter module dependency (#274)
This commit introduces significant changes to the architecture of the Horilla HRMS system by decoupling interdependent modules. The following modifications were made:

1. **Module Independence**: Each module has been refactored to eliminate reliance on other modules, promoting a more modular and maintainable codebase.
2. **Refactored Imports and Dependencies**: Adjusted import statements and dependency injections to support independent module operation.
3. **Compatibility and Functionality**: Ensured that all modules are compatible with existing systems and maintain their intended functionality both independently and when integrated with other modules.

These changes enhance the modularity, maintainability, and scalability of the Horilla HRMS, allowing developers to work on individual modules without affecting the entire system. Future development and deployment will be more efficient and less prone to issues arising from tightly coupled code.

**NOTE**
For existing Horilla users, if you face any issues during the migrations, please run the following command and try again the migrations.

- `python3 manage.py makemigrations`
- `python3 manage.py migrate base`
- `python3 manage.py migrate`





* [IMP] ASSET: Asset module dependency removal from other Horilla apps

* [IMP] ATTENDANCE: Attendance module dependency removal from other Horilla apps

* [IMP] BASE: Base module dependency removal from other Horilla apps

* [IMP] EMPLOYEE: Employee module dependency removal from other Horilla apps

* [IMP] HELPDESK: Helpdesk module dependency removal from other Horilla apps

* [IMP] HORILLA AUDIT: Horilla Audit module dependency removal from other Horilla apps

* [IMP] HORILLA CRUMBS: Horilla Crumbs module dependency removal from other Horilla apps

* [IMP] HORILLA AUTOMATIONS: Horilla Automations module dependency removal from other Horilla apps

* [IMP] HORILLA VIEWS: Horilla Views module dependency removal from other Horilla apps

* [IMP] LEAVE: Leave module dependency removal from other Horilla apps

* [IMP] OFFBOARDING: Offboarding module dependency removal from other Horilla apps

* [IMP] ONBOARDING: Onboarding module dependency removal from other Horilla apps

* [IMP] PMS: PMS module dependency removal from other Horilla apps

* [IMP] PAYROLL: Payroll module dependency removal from other Horilla apps

* [IMP] RECRUITMENT: Recruitment module dependency removal from other Horilla apps

* [IMP] HORILLA: Dependency removal updates

* [IMP] TEMPLATES: Dependency removal updates

* [IMP] STATIC: Dependency removal updates

* [IMP] HORILLA DOCUMENTS: Horilla Documents module dependency removal from other Horilla apps

* [ADD] HORILLA: methods.py

* [UPDT] HORILLA: Settings.py

* [FIX] EMPLOYEE: About tab issue

* Update horilla_settings.py

* Remove dummy db init password
2024-08-05 14:22:44 +05:30

310 lines
9.4 KiB
Python

"""
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