Files
ihrm/payroll/widgets/component_widgets.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

135 lines
4.6 KiB
Python

"""
Custom form widgets for conditional visibility and styling.
"""
from django import forms
from django.utils.safestring import SafeText, mark_safe
from horilla import settings
class AllowanceConditionalVisibility(forms.Widget):
"""
A custom widget that loads conditional js to the form.
Example:
class MyForm(forms.Form):
my_field = forms.CharField(widget=AllowanceConditionalVisibility, required=False)
"""
def render(self, name, value, attrs=None, renderer=None):
# Exclude the label from the rendered HTML
rendered_script = (
f'<script src="/{settings.STATIC_URL}build/js/allowanceWidget.js"></script>'
)
additional_script = f"""
<script id="{name}Script">
$(document).ready(function () {{
$("[for='id_{name}']").remove()
$("#{name}Script").remove()
}});
</script>
"""
attrs = attrs or {}
attrs["required"] = False
return mark_safe(rendered_script + additional_script)
class DeductionConditionalVisibility(forms.Widget):
"""
A custom widget that loads conditional js to the form.
Example:
class MyForm(forms.Form):
my_field = forms.CharField(widget=DeductionConditionalVisibility, required=False)
"""
def render(self, name, value, attrs, renderer) -> SafeText:
# Exclude the label from the rendered HTML
rendered_script = (
f'<script src="/{settings.STATIC_URL}build/js/deductionWidget.js"></script>'
)
additional_script = f"""
<script id="{name}Script">
$(document).ready(function () {{
$("[for='id_{name}']").remove()
$("#{name}Script").remove()
}});
</script>
"""
attrs = attrs or {}
attrs["required"] = False
return mark_safe(rendered_script + additional_script)
class StyleWidget(forms.Widget):
"""
A custom widget that enhances the styling and functionality of elements.
Example:
class MyForm(forms.Form):
my_field = forms.CharField(widget=styleWidget, required=False)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['style'].widget = widget.styleWidget(form=self)
"""
def __init__(self, *args, form=None, **kwargs):
if form is not None:
for _, field in form.fields.items():
field.widget.attrs.update(
{"data-widget": "style-widget", "class": "style-widget"}
)
super().__init__(*args, **kwargs)
def render(self, name, value, attrs=None, renderer=None):
"""
Renders the widget as HTML, including the necessary scripts and styles for select2.
Args:
name (str): The name of the form field.
value (Any): The current value of the form field.
attrs (dict, optional): Additional HTML attributes for the widget.
renderer: A custom renderer to use, if applicable.
Returns:
str: The rendered HTML representation of the widget.
"""
rendered_script = (
f'<script src="/{settings.STATIC_URL}build/js/styleWidget.js"></script>'
)
additional_script = f"""
<script id="{name}Script">
$(document).ready(function () {{
$("[for='id_{name}']").remove()
$("#{name}Script").remove()
// Select all select elements with select2 initialized
var selects = $("select[data-widget='style-widget']").select2();
function toggleSelect2() {{
selects.each(function() {{
var select = $(this);
var select2Container = select.nextAll(".select2.select2-container").first();
if (select.is(":hidden")) {{
select2Container.hide();
}} else {{
select2Container.show();
}}
}});
}}
$("select, [type='checkbox'], [type='radio']").change(function (e) {{
e.preventDefault();
toggleSelect2();
}});
toggleSelect2();
}});
</script>
<link rel="stylesheet" type="text/css" href="/{settings.STATIC_URL}build/css/styleWidget.css">
"""
attrs = attrs or {}
attrs["required"] = False
return mark_safe(rendered_script + additional_script)