Skip to content

Commit

Permalink
feat: filter by detail field (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hrishabh17 authored Oct 4, 2024
1 parent 34fe80a commit 66e3048
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
19 changes: 19 additions & 0 deletions fyle_accounting_mappings/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from rest_framework.views import Response
from rest_framework.serializers import ValidationError
from rest_framework.filters import BaseFilterBackend
from django.db.models import Q


def assert_valid(condition: bool, message: str) -> Response or None:
Expand All @@ -14,6 +16,7 @@ def assert_valid(condition: bool, message: str) -> Response or None:
'message': message
})


class LookupFieldMixin:
lookup_field = 'workspace_id'

Expand All @@ -23,3 +26,19 @@ def filter_queryset(self, queryset):
filter_kwargs = {self.lookup_field: lookup_value}
queryset = queryset.filter(**filter_kwargs)
return super().filter_queryset(queryset)


class JSONFieldFilterBackend(BaseFilterBackend):
"""
Custom filter backend to filter on JSONField for dynamic key lookups.
Supports filters like detail__{key} and detail__{key}__in.
"""

def filter_queryset(self, request, queryset, view):
filters = Q()

for param, value in request.query_params.items():
if param.startswith('detail__'):
filters &= Q(**{param: value if '__in' not in param else value.split(',')})

return queryset.filter(filters)
18 changes: 9 additions & 9 deletions fyle_accounting_mappings/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from rest_framework.views import status
from django.db.models import Count, Q

from .utils import LookupFieldMixin
from .utils import LookupFieldMixin, JSONFieldFilterBackend
from .exceptions import BulkError
from .utils import assert_valid
from .models import MappingSetting, Mapping, ExpenseAttribute, DestinationAttribute, EmployeeMapping, \
Expand Down Expand Up @@ -194,7 +194,7 @@ def get(self, request, *args, **kwargs):
assert_valid(destination_type is not None, 'query param destination_type not found')

filters = {
'attribute_type' : source_type,
'attribute_type': source_type,
'workspace_id': self.kwargs['workspace_id']
}

Expand Down Expand Up @@ -234,8 +234,8 @@ def get(self, request, *args, **kwargs):
).count()
else:
filters = {
'source_type' : source_type,
'destination_type' : destination_type,
'source_type': source_type,
'destination_type': destination_type,
'workspace_id': self.kwargs['workspace_id']
}
if source_type in ('PROJECT', 'CATEGORY'):
Expand Down Expand Up @@ -358,8 +358,8 @@ def get_queryset(self):

# Prepare filters for ExpenseAttribute
base_filters = Q(workspace_id=self.kwargs['workspace_id']) & \
Q(attribute_type='CATEGORY') & \
Q(active=True)
Q(attribute_type='CATEGORY') & \
Q(active=True)

# Get the 'Activity' mapping and attribute
activity_mapping = CategoryMapping.objects.filter(
Expand Down Expand Up @@ -419,7 +419,7 @@ def get_queryset(self):
).values_list('source_employee_id', flat=True)

filters = {
'workspace_id' : self.kwargs['workspace_id'],
'workspace_id': self.kwargs['workspace_id'],
'attribute_type': 'EMPLOYEE'
}

Expand Down Expand Up @@ -456,7 +456,7 @@ class DestinationAttributesView(LookupFieldMixin, ListAPIView):
queryset = DestinationAttribute.objects.all().order_by('value')
serializer_class = DestinationAttributeSerializer
pagination_class = None
filter_backends = (DjangoFilterBackend,)
filter_backends = (DjangoFilterBackend, JSONFieldFilterBackend,)
filterset_fields = {'attribute_type': {'exact', 'in'}, 'display_name': {'exact', 'in'}, 'active': {'exact'}}


Expand All @@ -478,5 +478,5 @@ class PaginatedDestinationAttributesView(LookupFieldMixin, ListAPIView):
"""
queryset = DestinationAttribute.objects.filter(active=True).order_by('value')
serializer_class = DestinationAttributeSerializer
filter_backends = (DjangoFilterBackend,)
filter_backends = (DjangoFilterBackend, JSONFieldFilterBackend,)
filterset_class = DestinationAttributeFilter
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

setuptools.setup(
name='fyle-accounting-mappings',
version='1.34.8',
version='1.34.9',
author='Shwetabh Kumar',
author_email='[email protected]',
description='Django application to store the fyle accounting mappings in a generic manner',
Expand Down

0 comments on commit 66e3048

Please sign in to comment.