add search filter for product list api
This commit is contained in:
@@ -1,22 +1,33 @@
|
|||||||
|
from django.db.models import Q
|
||||||
from django.shortcuts import get_object_or_404
|
from django.shortcuts import get_object_or_404
|
||||||
|
|
||||||
from rest_framework import generics, views
|
from rest_framework import generics, views
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
|
|
||||||
|
from core.apps.accounts.permissions.permissions import HasRolePermission
|
||||||
from core.apps.products.models import Product
|
from core.apps.products.models import Product
|
||||||
from core.apps.products.serializers import product as serializers
|
from core.apps.products.serializers import product as serializers
|
||||||
from core.apps.accounts.permissions.permissions import HasRolePermission
|
|
||||||
from core.apps.shared.paginations.custom import CustomPageNumberPagination
|
from core.apps.shared.paginations.custom import CustomPageNumberPagination
|
||||||
|
|
||||||
|
|
||||||
class ProductListApiView(generics.ListAPIView):
|
class ProductListApiView(generics.ListAPIView):
|
||||||
serializer_class = serializers.ProductListSerializer
|
serializer_class = serializers.ProductListSerializer
|
||||||
queryset = Product.objects.select_related('unity').only(
|
queryset = Product.objects.select_related("unity").only(
|
||||||
'id', 'name', 'type', 'unity'
|
"id", "name", "type", "unity"
|
||||||
)
|
)
|
||||||
permission_classes = [HasRolePermission]
|
permission_classes = [HasRolePermission]
|
||||||
pagination_class = CustomPageNumberPagination
|
pagination_class = CustomPageNumberPagination
|
||||||
|
|
||||||
|
def get(self, request):
|
||||||
|
search = request.query_params.get("search")
|
||||||
|
if search:
|
||||||
|
self.queryset = self.queryset.filter(
|
||||||
|
Q(name__istartswith=search) | Q(unity__value__istartswith=search)
|
||||||
|
)
|
||||||
|
page = self.paginate_queryset(self.queryset)
|
||||||
|
if page is not None:
|
||||||
|
serializer = self.serializer_class(page, many=True)
|
||||||
|
return self.get_paginated_response(serializer.data)
|
||||||
|
|
||||||
|
|
||||||
class ProductCreateApiView(generics.GenericAPIView):
|
class ProductCreateApiView(generics.GenericAPIView):
|
||||||
serializer_class = serializers.ProductSerializer
|
serializer_class = serializers.ProductSerializer
|
||||||
@@ -29,19 +40,19 @@ class ProductCreateApiView(generics.GenericAPIView):
|
|||||||
data = serializer.save()
|
data = serializer.save()
|
||||||
return Response(
|
return Response(
|
||||||
{
|
{
|
||||||
'success': True,
|
"success": True,
|
||||||
'message': "product successfully created!",
|
"message": "product successfully created!",
|
||||||
'product': serializers.ProductListSerializer(data).data
|
"product": serializers.ProductListSerializer(data).data,
|
||||||
},
|
},
|
||||||
status=201
|
status=201,
|
||||||
)
|
)
|
||||||
return Response(
|
return Response(
|
||||||
{
|
{
|
||||||
"success": False,
|
"success": False,
|
||||||
"message": "an error occurred while adding th product.",
|
"message": "an error occurred while adding th product.",
|
||||||
"error": serializer.errors
|
"error": serializer.errors,
|
||||||
},
|
},
|
||||||
status=400
|
status=400,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -56,34 +67,36 @@ class ProductUpdateApiView(generics.GenericAPIView):
|
|||||||
if serializer.is_valid(raise_exception=True):
|
if serializer.is_valid(raise_exception=True):
|
||||||
serializer.save()
|
serializer.save()
|
||||||
return Response(
|
return Response(
|
||||||
{'success': True, 'message': 'product successfully updated!'},
|
{"success": True, "message": "product successfully updated!"},
|
||||||
status=200
|
status=200,
|
||||||
)
|
)
|
||||||
return Response(
|
return Response(
|
||||||
{
|
{
|
||||||
'success': False,
|
"success": False,
|
||||||
'message': "an error occurred while updating the product.",
|
"message": "an error occurred while updating the product.",
|
||||||
"error": serializer.errors,
|
"error": serializer.errors,
|
||||||
},
|
},
|
||||||
status=400
|
status=400,
|
||||||
)
|
)
|
||||||
|
|
||||||
def patch(self, request, product_id):
|
def patch(self, request, product_id):
|
||||||
product = get_object_or_404(Product, id=product_id)
|
product = get_object_or_404(Product, id=product_id)
|
||||||
serializer = self.serializer_class(data=request.data, instance=product, partial=True)
|
serializer = self.serializer_class(
|
||||||
|
data=request.data, instance=product, partial=True
|
||||||
|
)
|
||||||
if serializer.is_valid(raise_exception=True):
|
if serializer.is_valid(raise_exception=True):
|
||||||
serializer.save()
|
serializer.save()
|
||||||
return Response(
|
return Response(
|
||||||
{'success': True, "message": "product successfully updated!"},
|
{"success": True, "message": "product successfully updated!"},
|
||||||
status=200
|
status=200,
|
||||||
)
|
)
|
||||||
return Response(
|
return Response(
|
||||||
{
|
{
|
||||||
"successs": False,
|
"successs": False,
|
||||||
"message": "an error accurred while updating the product.",
|
"message": "an error accurred while updating the product.",
|
||||||
"error": serializer.errors
|
"error": serializer.errors,
|
||||||
},
|
},
|
||||||
status=400
|
status=400,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -94,6 +107,5 @@ class ProductDeleteApiView(views.APIView):
|
|||||||
product = get_object_or_404(Product, id=product_id)
|
product = get_object_or_404(Product, id=product_id)
|
||||||
product.delete()
|
product.delete()
|
||||||
return Response(
|
return Response(
|
||||||
{'success': True, 'message': 'product successfully deleted!'},
|
{"success": True, "message": "product successfully deleted!"}, status=204
|
||||||
status=204
|
|
||||||
)
|
)
|
||||||
Reference in New Issue
Block a user