Skip to content

Commit

Permalink
Merge pull request #173 from rkshaon/171-publisher-apis
Browse files Browse the repository at this point in the history
done: #124 - search API
  • Loading branch information
rkshaon authored Nov 28, 2024
2 parents 53c3535 + dc14a2f commit a10d0c1
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 1 deletion.
4 changes: 4 additions & 0 deletions backend/BookShelf/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@
),
'DEFAULT_PAGINATION_CLASS': 'BookShelf.utilities.pagination.Pagination',
'PAGE_SIZE': 10,
'DEFAULT_FILTER_BACKENDS': 'BookShelf.utilities.filters.SearchFilter',
# 'DEFAULT_FILTER_BACKENDS': [
# 'rest_framework.filters.SearchFilter',
# ],
}

SIMPLE_JWT = {
Expand Down
33 changes: 33 additions & 0 deletions backend/BookShelf/utilities/filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from rest_framework.filters import BaseFilterBackend


class SearchFilter(BaseFilterBackend):
"""
A custom search filter that allows more flexible query handling.
"""

def get_search_fields(self, view):
"""
Returns the search fields defined on the view.
"""
return getattr(view, 'search_fields', None)

def filter_queryset(self, request, queryset, view):
"""
Perform custom filtering based on search fields and query parameters.
"""
search_fields = self.get_search_fields(view)
search_param = request.query_params.get(
'search') # Default query param is `search`

if not search_fields or not search_param:
return queryset # No search fields or query parameter, return unfiltered queryset # noqa

# Build a query dynamically
from django.db.models import Q
query = Q()
for field in search_fields:
# Case-insensitive search
query |= Q(**{f"{field}__icontains": search_param})

return queryset.filter(query)
11 changes: 11 additions & 0 deletions backend/BookShelf/utilities/pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,14 @@ def get_page_size(self, request):
self.max_page_size
)
return self.page_size

# For customizing the paginated response
# def get_paginated_response(self, data):
# return Response({
# 'total_count': self.page.paginator.count,
# 'total_pages': self.page.paginator.num_pages,
# 'current_page': self.page.number,
# 'next': self.get_next_link(),
# 'previous': self.get_previous_link(),
# 'results': data,
# })
18 changes: 18 additions & 0 deletions backend/publisher_api/migrations/0003_alter_publisher_name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.1.1 on 2024-11-28 20:25

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('publisher_api', '0002_publisher_added_by_publisher_added_date_time_and_more'),
]

operations = [
migrations.AlterField(
model_name='publisher',
name='name',
field=models.CharField(db_index=True, max_length=255),
),
]
2 changes: 1 addition & 1 deletion backend/publisher_api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


class Publisher(models.Model):
name = models.CharField(max_length=255)
name = models.CharField(max_length=255, db_index=True)
address = models.CharField(max_length=255, blank=True, null=True)
website = models.URLField(blank=True, null=True)
email = models.EmailField(blank=True, null=True)
Expand Down
4 changes: 4 additions & 0 deletions backend/publisher_api/views/v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from rest_framework import status

from BookShelf.utilities.permissions import IsAdminOrModerator
from BookShelf.utilities.filters import SearchFilter

from publisher_api.models import Publisher

Expand All @@ -15,6 +16,9 @@ class PublisherViewSet(ModelViewSet):
permission_classes = [
IsAdminOrModerator,
]
filter_backends = [SearchFilter]
search_fields = ['name']
# search_param = 'q' # Default search query parameter is `search`
# authentication_classes = [TokenAuthentication]

def perform_create(self, serializer):
Expand Down

0 comments on commit a10d0c1

Please sign in to comment.