[UPDT] EMPLOYEE: Employee filter method
This commit is contained in:
@@ -324,9 +324,12 @@ def get_nested_instances(model, field_names, field_values):
|
|||||||
related_model = related_field.remote_field.model
|
related_model = related_field.remote_field.model
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
field_values = [int(value) for value in field_values]
|
object_ids = [int(value) for value in field_values if value != "not_set"]
|
||||||
related_instances = related_model.objects.filter(id__in=field_values)
|
related_instances = related_model.objects.filter(id__in=object_ids)
|
||||||
return [str(instance) for instance in related_instances]
|
result = [str(instance) for instance in related_instances]
|
||||||
|
if "not_set" in field_values:
|
||||||
|
result.insert(0, "not_set")
|
||||||
|
return result
|
||||||
except (ObjectDoesNotExist, ValueError):
|
except (ObjectDoesNotExist, ValueError):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|||||||
@@ -300,4 +300,15 @@ _("company"),
|
|||||||
_("employee-create-personal-info"),
|
_("employee-create-personal-info"),
|
||||||
_("offboarding"),
|
_("offboarding"),
|
||||||
_("offboarding-pipeline"),
|
_("offboarding-pipeline"),
|
||||||
_("pagination-settings-view"),
|
_("pagination-settings-view"),
|
||||||
|
_("organisation-chart"),
|
||||||
|
_("document-request-view"),
|
||||||
|
_("disciplinary-actions"),
|
||||||
|
_("view-policies"),
|
||||||
|
_("resignation-requests-view"),
|
||||||
|
_("action-type"),
|
||||||
|
_("general-settings"),
|
||||||
|
_(""),
|
||||||
|
_(""),
|
||||||
|
_(""),
|
||||||
|
_(""),
|
||||||
@@ -4,6 +4,10 @@ filters.py
|
|||||||
This page is used to register filter for employee models
|
This page is used to register filter for employee models
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
import datetime
|
||||||
|
|
||||||
|
import django
|
||||||
|
from attendance.models import Attendance
|
||||||
from employee.models import DisciplinaryAction, Policy
|
from employee.models import DisciplinaryAction, Policy
|
||||||
import uuid
|
import uuid
|
||||||
from django import forms
|
from django import forms
|
||||||
@@ -69,6 +73,9 @@ class EmployeeFilter(FilterSet):
|
|||||||
(False, "No"),
|
(False, "No"),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
working_today = django_filters.BooleanFilter(
|
||||||
|
label="Working", method="get_working_today"
|
||||||
|
)
|
||||||
|
|
||||||
not_in_yet = django_filters.DateFilter(
|
not_in_yet = django_filters.DateFilter(
|
||||||
method="not_in_yet_func",
|
method="not_in_yet_func",
|
||||||
@@ -135,18 +142,43 @@ class EmployeeFilter(FilterSet):
|
|||||||
"""
|
"""
|
||||||
Override the default filtering behavior to handle None option.
|
Override the default filtering behavior to handle None option.
|
||||||
"""
|
"""
|
||||||
|
from django.db.models import Q
|
||||||
|
|
||||||
data = self.form.cleaned_data
|
data = self.form.cleaned_data
|
||||||
not_set_dict = {key: value for key, value in data.items() if value == "not_set"}
|
not_set_dict = {}
|
||||||
|
for key, value in data.items():
|
||||||
|
if isinstance(value,(list,django.db.models.query.QuerySet)):
|
||||||
|
if value and "not_set" in value:
|
||||||
|
not_set_dict[key] = value
|
||||||
|
|
||||||
if not_set_dict:
|
if not_set_dict:
|
||||||
filtered_data = queryset
|
q_objects = Q()
|
||||||
for key, value in not_set_dict.items():
|
for key, values in not_set_dict.items():
|
||||||
not_set = f"{key}__isnull"
|
for value in values:
|
||||||
filtered_data = filtered_data.filter(**{not_set: True})
|
if value == "not_set":
|
||||||
return filtered_data
|
q_objects |= Q(**{f"{key}__isnull": True})
|
||||||
|
else:
|
||||||
|
q_objects |= Q(**{key: value})
|
||||||
|
return queryset.filter(q_objects)
|
||||||
return super().filter_queryset(queryset)
|
return super().filter_queryset(queryset)
|
||||||
|
|
||||||
# Continue with the default behavior for other filters
|
# Continue with the default behavior for other filters
|
||||||
|
|
||||||
|
def get_working_today(self, queryset, _, value):
|
||||||
|
today = datetime.datetime.now().date()
|
||||||
|
yesterday = today - datetime.timedelta(days=1)
|
||||||
|
|
||||||
|
working_employees = Attendance.objects.filter(
|
||||||
|
attendance_date__gte=yesterday,
|
||||||
|
attendance_date__lte=today,
|
||||||
|
attendance_clock_out_date__isnull=True,
|
||||||
|
).values_list("employee_id", flat=True)
|
||||||
|
if value:
|
||||||
|
queryset = queryset.filter(id__in=working_employees)
|
||||||
|
else:
|
||||||
|
queryset = queryset.exclude(id__in=working_employees)
|
||||||
|
return queryset
|
||||||
|
|
||||||
def filter_by_name(self, queryset, _, value):
|
def filter_by_name(self, queryset, _, value):
|
||||||
"""
|
"""
|
||||||
Filter queryset by first name or last name.
|
Filter queryset by first name or last name.
|
||||||
@@ -207,7 +239,7 @@ class EmployeeFilter(FilterSet):
|
|||||||
self.model_choice_filters = [
|
self.model_choice_filters = [
|
||||||
filter
|
filter
|
||||||
for filter in self.filters.values()
|
for filter in self.filters.values()
|
||||||
if isinstance(filter, django_filters.ModelChoiceFilter)
|
if isinstance(filter, django_filters.ModelMultipleChoiceFilter)
|
||||||
]
|
]
|
||||||
for model_choice_filter in self.model_choice_filters:
|
for model_choice_filter in self.model_choice_filters:
|
||||||
queryset = (
|
queryset = (
|
||||||
@@ -216,20 +248,25 @@ class EmployeeFilter(FilterSet):
|
|||||||
else model_choice_filter.queryset
|
else model_choice_filter.queryset
|
||||||
)
|
)
|
||||||
choices = [
|
choices = [
|
||||||
("", "---------"),
|
|
||||||
("not_set", _("Not Set")),
|
("not_set", _("Not Set")),
|
||||||
]
|
]
|
||||||
choices.extend([(obj.id, str(obj)) for obj in queryset])
|
choices.extend([(obj.id, str(obj)) for obj in queryset])
|
||||||
self.form.fields[model_choice_filter.field_name] = forms.ChoiceField(
|
if (
|
||||||
choices=choices,
|
model_choice_filter.field_name != "user_permissions"
|
||||||
required=False,
|
and model_choice_filter.field_name != "groups"
|
||||||
widget=forms.Select(
|
):
|
||||||
attrs={
|
self.form.fields[
|
||||||
"class": "oh-select oh-select-2 select2-hidden-accessible",
|
model_choice_filter.field_name
|
||||||
"id": uuid.uuid4(),
|
] = forms.MultipleChoiceField(
|
||||||
}
|
choices=choices,
|
||||||
),
|
required=False,
|
||||||
)
|
widget=forms.SelectMultiple(
|
||||||
|
attrs={
|
||||||
|
"class": "oh-select oh-select-2 select2-hidden-accessible",
|
||||||
|
"id": uuid.uuid4(),
|
||||||
|
}
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class EmployeeReGroup:
|
class EmployeeReGroup:
|
||||||
@@ -303,20 +340,19 @@ class DisciplinaryActionFilter(FilterSet):
|
|||||||
search = CharFilter(method=filter_by_name)
|
search = CharFilter(method=filter_by_name)
|
||||||
|
|
||||||
start_date = django_filters.DateFilter(
|
start_date = django_filters.DateFilter(
|
||||||
widget=forms.DateInput(attrs={"type": "date"}),
|
widget=forms.DateInput(attrs={"type": "date"}),
|
||||||
)
|
)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
|
|
||||||
model = DisciplinaryAction
|
model = DisciplinaryAction
|
||||||
fields = [
|
fields = [
|
||||||
"employee_id",
|
"employee_id",
|
||||||
"action",
|
"action",
|
||||||
"employee_id__employee_work_info__job_position_id",
|
"employee_id__employee_work_info__job_position_id",
|
||||||
"employee_id__employee_work_info__department_id",
|
"employee_id__employee_work_info__department_id",
|
||||||
"employee_id__employee_work_info__work_type_id",
|
"employee_id__employee_work_info__work_type_id",
|
||||||
"employee_id__employee_work_info__job_role_id",
|
"employee_id__employee_work_info__job_role_id",
|
||||||
"employee_id__employee_work_info__reporting_manager_id",
|
"employee_id__employee_work_info__reporting_manager_id",
|
||||||
"employee_id__employee_work_info__company_id",
|
"employee_id__employee_work_info__company_id",
|
||||||
"employee_id__employee_work_info__shift_id",
|
"employee_id__employee_work_info__shift_id",
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -271,7 +271,7 @@ urlpatterns = [
|
|||||||
views.document_delete,
|
views.document_delete,
|
||||||
name="document-delete",
|
name="document-delete",
|
||||||
),
|
),
|
||||||
path("organisation-chart", views.organisation_chart, name="organisation-chart"),
|
path("organisation-chart/", views.organisation_chart, name="organisation-chart"),
|
||||||
|
|
||||||
path("delete-policies",policies.delete_policies,name="delete-policies"),
|
path("delete-policies",policies.delete_policies,name="delete-policies"),
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,12 @@ filters.py
|
|||||||
import uuid
|
import uuid
|
||||||
import django_filters
|
import django_filters
|
||||||
from django import forms
|
from django import forms
|
||||||
|
from django.db import models
|
||||||
from base.methods import reload_queryset
|
from base.methods import reload_queryset
|
||||||
|
from django_filters.filterset import FILTER_FOR_DBFIELD_DEFAULTS
|
||||||
|
|
||||||
|
|
||||||
|
FILTER_FOR_DBFIELD_DEFAULTS[models.ForeignKey]["filter_class"]=django_filters.ModelMultipleChoiceFilter
|
||||||
|
|
||||||
def filter_by_name(queryset, name, value):
|
def filter_by_name(queryset, name, value):
|
||||||
"""
|
"""
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -96,6 +96,12 @@ sidebar_urls = [
|
|||||||
"date-settings",
|
"date-settings",
|
||||||
"offboarding-pipeline",
|
"offboarding-pipeline",
|
||||||
"pagination-settings-view",
|
"pagination-settings-view",
|
||||||
|
"organisation-chart",
|
||||||
|
"disciplinary-actions",
|
||||||
|
"view-policies",
|
||||||
|
"resignation-requests-view",
|
||||||
|
"action-type",
|
||||||
|
"general-settings",
|
||||||
]
|
]
|
||||||
remove_urls = [
|
remove_urls = [
|
||||||
"objective-detailed-view",
|
"objective-detailed-view",
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ urlpatterns = [
|
|||||||
views.offboarding_individual_view,
|
views.offboarding_individual_view,
|
||||||
name="offboarding-individual-view",
|
name="offboarding-individual-view",
|
||||||
),
|
),
|
||||||
path("requests-view", views.request_view, name="resignation-request-view"),
|
path("resignation-requests-view/", views.request_view, name="resignation-request-view"),
|
||||||
path(
|
path(
|
||||||
"create-resignation-request",
|
"create-resignation-request",
|
||||||
views.create_resignation_request,
|
views.create_resignation_request,
|
||||||
|
|||||||
Reference in New Issue
Block a user