[UPDT] LEAVE: Leave search method by adding search element by option

This commit is contained in:
Horilla
2024-01-20 12:57:04 +05:30
parent 946cd755f2
commit ba74fd0675
5 changed files with 127 additions and 56 deletions

View File

@@ -25,9 +25,6 @@ from .models import (
from base.filters import FilterSet
class LeaveTypeFilter(FilterSet):
"""
Filter class for LeaveType model.
@@ -37,8 +34,12 @@ class LeaveTypeFilter(FilterSet):
name = filters.CharFilter(field_name="name", lookup_expr="icontains")
search = filters.CharFilter(field_name="name", lookup_expr="icontains")
carry_forward_gte = filters.CharFilter(field_name="carryforward_max", lookup_expr="gte")
carry_forward_lte = filters.CharFilter(field_name="carryforward_max", lookup_expr="lte")
carry_forward_gte = filters.CharFilter(
field_name="carryforward_max", lookup_expr="gte"
)
carry_forward_lte = filters.CharFilter(
field_name="carryforward_max", lookup_expr="lte"
)
total_days_gte = filters.CharFilter(field_name="total_days", lookup_expr="gte")
total_days_lte = filters.CharFilter(field_name="total_days", lookup_expr="lte")
@@ -117,6 +118,9 @@ class AssignedLeaveFilter(FilterSet):
self.form.fields[field].widget.attrs["id"] = f"{uuid.uuid4()}"
from horilla.filters import filter_by_name
class LeaveRequestFilter(FilterSet):
"""
Filter class for LeaveRequest model.
@@ -126,9 +130,7 @@ class LeaveRequestFilter(FilterSet):
overall_leave = django_filters.CharFilter(method="overall_leave_filter")
employee_id = filters.CharFilter(
field_name="employee_id__employee_first_name", lookup_expr="icontains"
)
search = django_filters.CharFilter(method="filter_by_name")
from_date = DateFilter(
field_name="start_date",
lookup_expr="gte",
@@ -214,6 +216,41 @@ class LeaveRequestFilter(FilterSet):
queryset = today_leave_requests
return queryset
def filter_by_name(self, queryset, name, value):
# Call the imported function
filter_method = {
"leave_type": "leave_type_id__name__icontains",
"status": "status__icontains",
"department": "employee_id__employee_work_info__department_id__department__icontains",
"job_position": "employee_id__employee_work_info__job_position_id__job_position__icontains",
"company": "employee_id__employee_work_info__company_id__company__icontains",
}
search_field = self.data.get("search_field")
if not search_field:
parts = value.split()
first_name = parts[0]
last_name = " ".join(parts[1:]) if len(parts) > 1 else ""
# Filter the queryset by first name and last name
if first_name and last_name:
queryset = queryset.filter(
employee_id__employee_first_name__icontains=first_name,
employee_id__employee_last_name__icontains=last_name,
)
elif first_name:
queryset = queryset.filter(
employee_id__employee_first_name__icontains=first_name
)
elif last_name:
queryset = queryset.filter(
employee_id__employee_last_name__icontains=last_name
)
else:
filter = filter_method.get(search_field)
queryset = queryset.filter(**{filter: value})
return queryset
def __init__(self, data=None, queryset=None, *, request=None, prefix=None):
super().__init__(data=data, queryset=queryset, request=request, prefix=prefix)
for field in self.form.fields.keys():

View File

@@ -15,13 +15,6 @@
>
{% trans "Unselect All Requests" %}
</div>
{% comment %} <div
class="oh-checkpoint-badge text-info mb-2"
id="exportLeaverequests"
style="cursor: pointer; display: none"
>
{% trans "Export Requests" %}
</div> {% endcomment %}
<div class="oh-checkpoint-badge text-danger mb-2" id="selectedShowLeaverequests" style="display: none"></div>
<!-- end of selection buttons -->
@@ -65,8 +58,11 @@
<div class="oh-sticky-table__th" hx-get="{% url 'request-filter' %}?{{pd}}&sortby=status" hx-target="#leaveRequest">{% trans "Status" %}</div>
{% if not dashboard %}
<div class="oh-sticky-table__th">{% trans "Comment" %}</div>
<div class="oh-sticky-table__th" >{% trans "Penalties" %}</div>
<div class="oh-sticky-table__th">{% trans "Options" %}</div>
<div class="oh-sticky-table__th">{% trans "Actions" %}</div>
{% if request.user|is_reportingmanager or perms.leave.chanage_leaverequest or perms.leave.delete_leaverequest or request.user|is_leave_approval_manager %}
<div class="oh-sticky-table__th">{% trans "Actions" %}</div>
{% endif %}
{% endif %}
</div>
</div>
@@ -124,6 +120,16 @@
</div>
</div>
<div class="oh-sticky-table__td" onclick="event.stopPropagation()">
{% if leave_request.get_penalties_count %}
<div data-target="#penaltyViewModal" data-toggle="oh-modal-toggle"
hx-get="{% url "view-penalties" %}?leave_request_id={{leave_request.id}}"
hx-target="#penaltyViewModalBody" align="center" style="background-color: rgba(229, 79, 56, 0.036); border: 2px solid rgb(229, 79, 56); border-radius: 18px; padding: 10px; font-weight: bold; width: 85%;">
{% trans "Penalties" %} :{{leave_request.get_penalties_count}}
</div>
{% endif %}
</div>
<div class="oh-sticky-table__td" onclick="event.stopPropagation()">
<div class="oh-btn-group" >
{% if leave_request.status == 'requested' or leave_request.status == 'rejected' %}
@@ -156,6 +162,18 @@
</div>
<div class="oh-sticky-table__td" onclick="event.stopPropagation()">
<div class="oh-btn-group">
{% if request.user|is_reportingmanager or perms.attendance.chanage_penaltyaccount or request.user|is_leave_approval_manager %}
<button
data-toggle="oh-modal-toggle"
data-target="#penaltyModal"
hx-target="#penaltyModalBody"
hx-get="{% url "leave-cut-penalty" leave_request.id %}?{{pd}}"
type='submit'
class="oh-btn oh-btn--danger-outline oh-btn--light-bkg w-100"
title="{% trans 'Penalty' %}">
<ion-icon name="information-circle-outline"></ion-icon>
</button>
{% endif %}
<button class="oh-btn oh-btn--light-bkg w-100" title="{% trans 'Edit' %}" data-toggle="oh-modal-toggle"
data-target="#editModal2" hx-get="{% url 'request-update' leave_request.id %}"
hx-target="#updateForm"><ion-icon name="create-outline"></ion-icon></button>
@@ -226,23 +244,19 @@
class="oh-modal"
id="requestCommentModal"
role="dialog"
aria-labelledby="emptagModal"
aria-labelledby="requestCommentModal"
aria-hidden="true"
>
<div class="oh-modal__dialog" id="requestCommentFormModal">
{% comment %} <div class="oh-modal__dialog-header">
<h2 class="oh-modal__dialog-title" id="createModalTitle">
{% trans "Add Comment" %}
</h2>
<button class="oh-modal__close" aria-label="Close">
<ion-icon name="close-outline"></ion-icon>
</button>
</div>
<div class="oh-modal__dialog-body" ></div> {% endcomment %}
</div>
<div class="oh-modal__dialog" id="requestCommentFormModal"></div>
</div>
<!-- end of comment modal -->
<div class="oh-modal" id="penaltyModal"
role="dialog" aria-labelledby="requestCommentModal"
aria-hidden="true">
<div class="oh-modal__dialog" style="max-width: 550px" id="penaltyModalBody"></div>
</div>
<div
class="oh-modal" style="z-index: 60;"
id="shiftRequestDetailModal"
@@ -269,6 +283,16 @@
</div>
</div>
<div class="oh-modal" id="penaltyViewModal" role="dialog" aria-hidden="true">
<div class="oh-modal__dialog" style="max-width: 1050px">
<div class="oh-modal__dialog-header">
<button type="button" class="oh-modal__close" aria-label="Close"><ion-icon name="close-outline"></ion-icon></button>
</div>
<div class="oh-modal__dialog-body" id="penaltyViewModalBody"></div>
</div>
</div>
<script>
$(document).ready(function () {

View File

@@ -141,9 +141,9 @@
</div>
</div>
<div class="oh-sticky-table__td">
<div class="oh-sticky-table__td" onclick="event.stopPropagation()">
{% if leave_request.get_penalties_count %}
<div data-target="#penaltyViewModal" data-toggle="oh-modal-toggle" hx-get="{% url "view-penalties" %}?leave_request_id={{leave_request.id}}" hx-target="#penaltyViewModalBody" align="center" style="background-color: rgba(229, 79, 56, 0.036); border: 2px solid rgb(229, 79, 56); border-radius: 18px; padding: 10px; font-weight: bold; width: 85%;">Penalties :{{leave_request.get_penalties_count}}</div>
<div data-target="#penaltyViewModal" data-toggle="oh-modal-toggle" hx-get="{% url "view-penalties" %}?leave_request_id={{leave_request.id}}" hx-target="#penaltyViewModalBody" align="center" style="background-color: rgba(229, 79, 56, 0.036); border: 2px solid rgb(229, 79, 56); border-radius: 18px; padding: 10px; font-weight: bold; width: 85%;">{% trans "Penalties" %} :{{leave_request.get_penalties_count}}</div>
{% endif %}
</div>
<div class="oh-sticky-table__td" onclick="event.stopPropagation()">
@@ -185,7 +185,7 @@
data-toggle="oh-modal-toggle"
data-target="#penaltyModal"
hx-target="#penaltyModalBody"
hx-get="{% url "leave-cut-penalty" leave_request.id %}"
hx-get="{% url "leave-cut-penalty" leave_request.id %}?{{pd}}"
type='submit'
class="oh-btn oh-btn--danger-outline oh-btn--light-bkg w-100"
title="{% trans 'Penalty' %}">

View File

@@ -16,7 +16,9 @@
</div>
{% endif %}
<div class="oh-modal__dialog-header">
<button type="button" class="oh-modal__close" hx-target="#leaveRequest" hx-get="{% url 'request-filter' %}" aria-label="Close">
<button type="button" class="oh-modal__close"
hx-target="#leaveRequest" hx-get="{% url 'request-filter' %}?{{pd}}"
aria-label="Close">
<ion-icon name="close-outline"></ion-icon>
</button>
</div>
@@ -35,7 +37,7 @@
</div>
</div>
</div>
<form hx-post="{% url 'leave-cut-penalty' instance.id %}" hx-target="#penaltyModalBody">
<form hx-post="{% url 'leave-cut-penalty' instance.id %}?{{pd}}" hx-target="#penaltyModalBody">
{{ form.as_p }}
<div class="oh-sticky-table__table oh-table--sortable">

View File

@@ -13,17 +13,6 @@
right: 15px;
}
</style>
{% comment %} {% if messages %}
<div class="oh-wrapper">
{% for message in messages %}
<div class="oh-alert-container">
<div class="oh-alert oh-alert--animated {{ message.tags }}">
{{ message }}
</div>
</div>
{% endfor %}
</div>
{% endif %} {% endcomment %}
<section class="oh-wrapper oh-main__topbar" x-data="{searchShow: false}">
<div class="oh-main__titlebar oh-main__titlebar--left">
@@ -44,6 +33,13 @@
</div>
<div class="oh-main__titlebar oh-main__titlebar--right">
{% if leave_requests %}
<form
method="post"
hx-get="{% url 'request-filter' %}"
hx-target="#leaveRequest"
id="filterForm"
class="d-flex"
>
<div
class="oh-input-group oh-input__search-group"
:class="searchShow ? 'oh-input__search-group--show' : ''"
@@ -51,26 +47,27 @@
<ion-icon
name="search-outline"
class="oh-input-group__icon oh-input-group__icon--left"
></ion-icon>
>
</ion-icon>
<input
type="text"
class="oh-input oh-input__icon"
id="leave-request-search"
aria-label="Search Input"
placeholder="{% trans 'Search' %}"
name="employee_id"
hx-get="{% url 'request-filter' %}"
hx-trigger="keyup"
hx-target="#leaveRequest"
name="search"
/>
<div id="searchFieldDiv" style="display:none;">
<select name="search_field" class='oh-input__icon' style="border: none; display: flex; position: absolute; z-index: 999; margin-left:8%;" size="3" onclick="$('.filterButton')[0].click();">
<option style="margin-left: -10px;" value="leave_type">{% trans "Search in : Leave Type" %}</option>
<option style="margin-left: -10px;" value="status">{% trans "Search in : Status" %}</option>
<option style="margin-left: -10px;" value="department">{% trans "Search in : Department" %}</option>
<option style="margin-left: -10px;" value="job_position">{% trans "Search in : Job Position" %}</option>
<option style="margin-left: -10px;" value="company">{% trans "Search in : Company" %}</option>
</select>
</div>
</div>
<div class="oh-main__titlebar-button-container">
<form
method="post"
hx-get="{% url 'request-filter' %}"
hx-target="#leaveRequest"
id="filterForm"
class="d-flex"
>
<div class="oh-dropdown" x-data="{open: false}">
<button
class="oh-btn ml-2"
@@ -473,6 +470,17 @@
$("#filterCount").text("(" + count + ")");
}
});
$('#leave-request-search').on('keyup', function () {
var searchFieldDiv = $('#searchFieldDiv');
var selectedField = searchFieldDiv.find(':selected');
if ($(this).val().trim() !== '') {
searchFieldDiv.show();
} else {
searchFieldDiv.hide();
selectedField.prop('selected', false);
}
$('.filterButton').eq(0).click();
});
$(document).ready(function () {
$('#id_field').on('change',function(){
$('.filterButton')[0].click();