132 lines
3.6 KiB
Python
132 lines
3.6 KiB
Python
"""
|
|
forms.py
|
|
"""
|
|
from django import forms
|
|
from django.forms import widgets
|
|
from django.utils.translation import gettext_lazy as trans
|
|
from django.template.loader import render_to_string
|
|
from payroll.models.models import ReimbursementrequestComment, WorkRecord
|
|
from payroll.models.models import Contract
|
|
from base.methods import reload_queryset
|
|
|
|
|
|
class ModelForm(forms.ModelForm):
|
|
"""
|
|
ModelForm override for additional style
|
|
"""
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
reload_queryset(self.fields)
|
|
for _, field in self.fields.items():
|
|
widget = field.widget
|
|
|
|
if isinstance(widget, (forms.DateInput)):
|
|
field.widget.attrs.update({"class": "oh-input oh-calendar-input w-100"})
|
|
elif isinstance(
|
|
widget, (forms.NumberInput, forms.EmailInput, forms.TextInput)
|
|
):
|
|
label = trans(field.label)
|
|
field.widget.attrs.update(
|
|
{"class": "oh-input w-100", "placeholder": label}
|
|
)
|
|
elif isinstance(widget, (forms.Select,)):
|
|
field.widget.attrs.update(
|
|
{"class": "oh-select oh-select-2 select2-hidden-accessible"}
|
|
)
|
|
elif isinstance(widget, (forms.Textarea)):
|
|
label = trans(field.label)
|
|
field.widget.attrs.update(
|
|
{
|
|
"class": "oh-input w-100",
|
|
"placeholder": label,
|
|
"rows": 2,
|
|
"cols": 40,
|
|
}
|
|
)
|
|
elif isinstance(
|
|
widget,
|
|
(
|
|
forms.CheckboxInput,
|
|
forms.CheckboxSelectMultiple,
|
|
),
|
|
):
|
|
field.widget.attrs.update({"class": "oh-switch__checkbox"})
|
|
|
|
|
|
class ContractForm(ModelForm):
|
|
"""
|
|
ContactForm
|
|
"""
|
|
|
|
verbose_name = trans("Contract")
|
|
contract_start_date = forms.DateField()
|
|
contract_end_date = forms.DateField()
|
|
|
|
|
|
class Meta:
|
|
"""
|
|
Meta class for additional options
|
|
"""
|
|
|
|
fields = "__all__"
|
|
exclude = [
|
|
"is_active",
|
|
]
|
|
model = Contract
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
self.fields["employee_id"].widget.attrs.update(
|
|
{"onchange": "contractInitial(this)"}
|
|
)
|
|
self.fields["contract_start_date"].widget = widgets.DateInput(
|
|
attrs={
|
|
"type":"date",
|
|
"class": "oh-input w-100",
|
|
"placeholder": "Select a date",
|
|
}
|
|
)
|
|
self.fields["contract_end_date"].widget = widgets.DateInput(
|
|
attrs={
|
|
"type":"date",
|
|
"class": "oh-input w-100",
|
|
"placeholder": "Select a date",
|
|
}
|
|
)
|
|
|
|
def as_p(self):
|
|
"""
|
|
Render the form fields as HTML table rows with Bootstrap styling.
|
|
"""
|
|
context = {"form": self}
|
|
table_html = render_to_string("contract_form.html", context)
|
|
return table_html
|
|
|
|
|
|
class WorkRecordForm(ModelForm):
|
|
"""
|
|
WorkRecordForm
|
|
"""
|
|
|
|
class Meta:
|
|
"""
|
|
Meta class for additional options
|
|
"""
|
|
|
|
fields = "__all__"
|
|
model = WorkRecord
|
|
|
|
|
|
class ReimbursementrequestCommentForm(ModelForm):
|
|
"""
|
|
ReimbursementrequestCommentForm form
|
|
"""
|
|
|
|
class Meta:
|
|
"""
|
|
Meta class for additional options
|
|
"""
|
|
|
|
model = ReimbursementrequestComment
|
|
fields = ('comment',) |