Skip to content

Commit

Permalink
Advance search export log (#151)
Browse files Browse the repository at this point in the history
* Advance Search for Export log page

* bug fix for id__in

* linting fixed
  • Loading branch information
Ashutosh619-sudo authored Mar 26, 2024
1 parent bf082be commit 7cb65ac
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 18 deletions.
71 changes: 71 additions & 0 deletions apps/accounting_exports/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
from apps.accounting_exports.models import AccountingExport
from apps.fyle.models import Expense
import django_filters


class AdvanceSearchFilter(django_filters.FilterSet):
def filter_queryset(self, queryset):
or_filtered_queryset = queryset.none()
or_filter_fields = getattr(self.Meta, 'or_fields', [])
or_field_present = False

for field_name in self.Meta.fields:
value = self.data.get(field_name)
if value:
if field_name == 'is_skipped':
value = True if str(value) == 'true' else False

if field_name in ('status__in', 'type__in', 'id__in'):
value_lt = value.split(',')
filter_instance = self.filters[field_name]
queryset = filter_instance.filter(queryset, value_lt)
else:
filter_instance = self.filters[field_name]
queryset = filter_instance.filter(queryset, value)

for field_name in or_filter_fields:
value = self.data.get(field_name)
if value:
or_field_present = True
filter_instance = self.filters[field_name]
field_filtered_queryset = filter_instance.filter(queryset, value)
or_filtered_queryset |= field_filtered_queryset

if or_field_present:
queryset = queryset & or_filtered_queryset
return queryset

return queryset


class AccountingExportSearchFilter(AdvanceSearchFilter):
id__in = django_filters.CharFilter(lookup_expr='in', field_name='id')
exported_at__gte = django_filters.DateTimeFilter(lookup_expr='gte', field_name='exported_at')
exported_at__lte = django_filters.DateTimeFilter(lookup_expr='lte', field_name='exported_at')
status__in = django_filters.CharFilter(lookup_expr='in', field_name='status')
type__in = django_filters.CharFilter(lookup_expr='in', field_name='type')
expenses__expense_number = django_filters.CharFilter(field_name='expenses__expense_number', lookup_expr='icontains')
expenses__employee_name = django_filters.CharFilter(field_name='expenses__employee_name', lookup_expr='icontains')
expenses__employee_email = django_filters.CharFilter(field_name='expenses__employee_email', lookup_expr='icontains')
expenses__claim_number = django_filters.CharFilter(field_name='expenses__claim_number', lookup_expr='icontains')

class Meta:
model = AccountingExport
fields = ['exported_at__gte', 'exported_at__lte', 'status__in', 'type__in', 'id__in']
or_fields = ['expenses__expense_number', 'expenses__employee_name', 'expenses__employee_email', 'expenses__claim_number']


class ExpenseSearchFilter(AdvanceSearchFilter):
org_id = django_filters.CharFilter()
is_skipped = django_filters.BooleanFilter()
updated_at__gte = django_filters.DateTimeFilter(lookup_expr='gte', field_name='updated_at')
updated_at__lte = django_filters.DateTimeFilter(lookup_expr='lte', field_name='updated_at')
expense_number = django_filters.CharFilter(field_name='expense_number', lookup_expr='icontains')
employee_name = django_filters.CharFilter(field_name='employee_name', lookup_expr='icontains')
employee_email = django_filters.CharFilter(field_name='employee_email', lookup_expr='icontains')
claim_number = django_filters.CharFilter(field_name='claim_number', lookup_expr='icontains')

class Meta:
model = Expense
fields = ['org_id', 'is_skipped', 'updated_at__gte', 'updated_at__lte']
or_fields = ['expense_number', 'employee_name', 'employee_email', 'claim_number']
4 changes: 3 additions & 1 deletion apps/accounting_exports/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
AccountingExportSummarySerializer,
ErrorSerializer,
)
from apps.accounting_exports.helpers import AccountingExportSearchFilter

from sage_desktop_api.utils import LookupFieldMixin

logger = logging.getLogger(__name__)
Expand All @@ -23,7 +25,7 @@ class AccountingExportView(LookupFieldMixin, generics.ListAPIView):
serializer_class = AccountingExportSerializer
queryset = AccountingExport.objects.all().order_by("-updated_at")
filter_backends = (DjangoFilterBackend,)
filterset_fields = {"type": {"in"}, "updated_at": {"lte", "gte"}, "id": {"in"}, "status": {"in"}}
filterset_class = AccountingExportSearchFilter


class AccountingExportCountView(generics.RetrieveAPIView):
Expand Down
25 changes: 8 additions & 17 deletions apps/fyle/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from rest_framework.views import status
from rest_framework.response import Response

from django_filters.rest_framework import DjangoFilterBackend

from sage_desktop_api.utils import LookupFieldMixin
from apps.fyle.serializers import (
ImportFyleAttributesSerializer,
Expand All @@ -13,7 +15,9 @@
)
from apps.accounting_exports.serializers import ExpenseSerializer

from apps.workspaces.models import ExportSetting, Workspace
from apps.accounting_exports.helpers import ExpenseSearchFilter

from apps.workspaces.models import ExportSetting
from apps.fyle.models import ExpenseFilter, DependentFieldSetting, Expense
from apps.fyle.helpers import get_exportable_accounting_exports_ids
from apps.fyle.queue import queue_import_reimbursable_expenses, queue_import_credit_card_expenses
Expand Down Expand Up @@ -120,19 +124,6 @@ class SkippedExpenseView(generics.ListAPIView):
List Skipped Expenses
"""
serializer_class = ExpenseSerializer

def get_queryset(self):
start_date = self.request.query_params.get('start_date', None)
end_date = self.request.query_params.get('end_date', None)
org_id = Workspace.objects.get(id=self.kwargs['workspace_id']).org_id

filters = {
'org_id': org_id,
'is_skipped': True
}

if start_date and end_date:
filters['updated_at__range'] = [start_date, end_date]

queryset = Expense.objects.filter(**filters).order_by('-updated_at')
return queryset
queryset = Expense.objects.all().order_by("-updated_at")
filter_backends = (DjangoFilterBackend,)
filterset_class = ExpenseSearchFilter

0 comments on commit 7cb65ac

Please sign in to comment.