Skip to content

Commit

Permalink
add order filters with status and products pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
Horlawhumy-dev committed Jul 2, 2024
1 parent baa0238 commit 9c686dd
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
8 changes: 8 additions & 0 deletions api_doc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ Inventory Products Endpoints:
- Request Body: nil
- Response: nil

Note: This search functionality works when postgres database is used
5. GET /api/inventory/products/search?q=
- Description: Search for products by the specified field
- Request Body: nil
- Response: list of products
- Search From: title, description


Inventory Orders Endpoints:

Expand All @@ -66,6 +73,7 @@ Inventory Orders Endpoints:
- Description: List orders
- Request Body: nil
- Response: list of orders
- Filters: status, date_from, date_to

3. PUT /api/inventory/orders/:id/status/
- Description: Update order status by an admin user
Expand Down
1 change: 1 addition & 0 deletions drugstoc_inventory/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
'rest_framework',
'rest_framework_simplejwt',
'rest_framework_simplejwt.token_blacklist',
'django_filters',
#internal apps
"users",
"inventory"
Expand Down
29 changes: 27 additions & 2 deletions inventory/views.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# views.py
import logging
from django.contrib.postgres.search import SearchQuery, SearchRank
from django_filters import rest_framework as filters

from rest_framework import status, permissions
from rest_framework.response import Response
from rest_framework.views import APIView
from django.shortcuts import get_object_or_404
from django.http import Http404
from rest_framework.pagination import PageNumberPagination
from django.db.models import Sum, F
from datetime import datetime, timedelta
from .models import Product, Order, OrderItem
Expand All @@ -24,8 +27,13 @@ class InventoryProductList(APIView):
def get(self, request):
logger.info(f"User {request.user.id} requested product list")
products = Product.objects.all()
serializer = ProductSerializer(products, many=True)
return Response(serializer.data)

# Apply pagination
paginator = PageNumberPagination()
paginated_products = paginator.paginate_queryset(products, request)

serializer = ProductSerializer(paginated_products, many=True)
return paginator.get_paginated_response(serializer.data)

class InventoryProductCreate(APIView):
permission_classes = [permissions.IsAuthenticated, IsAdminOrReadOnly]
Expand Down Expand Up @@ -73,13 +81,30 @@ def delete(self, request, pk):
return Response(status=status.HTTP_204_NO_CONTENT)


class OrderFilter(filters.FilterSet):
status = filters.CharFilter(field_name='status', lookup_expr='iexact')
date_from = filters.DateFilter(field_name='created_at', lookup_expr='gte')
date_to = filters.DateFilter(field_name='created_at', lookup_expr='lte')

class Meta:
model = Order
fields = ['status', 'created_at']



class OrderListCreate(APIView):
permission_classes = [permissions.IsAuthenticated]
authentication_classes = [CustomJWTAuthentication]

def get(self, request):
logger.info(f"User {request.user.id} requested their order list")
orders = Order.objects.filter(owner=request.user)

filterset = OrderFilter(request.GET, queryset=orders)
if not filterset.is_valid():
return Response(filterset.errors, status=status.HTTP_400_BAD_REQUEST)

orders = filterset.qs
serializer = OrderSerializer(orders, many=True)
return Response(serializer.data)

Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Pillow==10.3.0
pytest-django==4.8.0
django-redis==5.4.0

django_filter==24.2

whitenoise # https://github.com/evansd/whitenoise
redis # https://github.com/redis/redis-py
hiredis # https://github.com/redis/hiredis-py
Expand Down

0 comments on commit 9c686dd

Please sign in to comment.