From 3205880bde4920cb1f1846b35d6ab76de9365bb5 Mon Sep 17 00:00:00 2001 From: keannu125 Date: Tue, 7 Mar 2023 19:28:38 +0800 Subject: [PATCH] Added least stocked product url --- ivy/products/serializers.py | 7 +++++-- ivy/products/urls.py | 1 + ivy/products/views.py | 14 +++++++++++++- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/ivy/products/serializers.py b/ivy/products/serializers.py index c7dace1..7a658f1 100644 --- a/ivy/products/serializers.py +++ b/ivy/products/serializers.py @@ -1,4 +1,5 @@ from rest_framework import serializers, mixins +from django.contrib.auth.models import User from simple_history.models import HistoricalRecords from .models import Product @@ -28,5 +29,7 @@ class LogSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = Product.history.model - fields = ('history_id', 'name', 'quantity', 'history_date') - read_only_fields = ('history_id', 'name', 'quantity', 'history_date') + fields = ('history_id', 'name', 'quantity', + 'history_date', 'history_user_id') + read_only_fields = ('history_id', 'name', 'quantity', + 'history_date', 'history_user_id') diff --git a/ivy/products/urls.py b/ivy/products/urls.py index 88a3c5b..c6f1356 100644 --- a/ivy/products/urls.py +++ b/ivy/products/urls.py @@ -5,6 +5,7 @@ from . import views router = routers.DefaultRouter() router.register(r'products', views.ProductViewSet) router.register(r'logs', views.LogViewSet) +router.register(r'lowest_stock_product', views.LeastStockProductViewSet) # Wire up our API using automatic URL routing. # Additionally, we include login URLs for the browsable API. diff --git a/ivy/products/views.py b/ivy/products/views.py index af0f55f..35eb93f 100644 --- a/ivy/products/views.py +++ b/ivy/products/views.py @@ -1,5 +1,6 @@ from rest_framework.permissions import IsAuthenticated from rest_framework import viewsets +from django.db.models import Min from .serializers import ProductSerializer, LogSerializer from .models import Product @@ -7,7 +8,18 @@ from .models import Product class ProductViewSet(viewsets.ModelViewSet): # permission_classes = [IsAuthenticated] serializer_class = ProductSerializer - queryset = Product.objects.all() + queryset = Product.objects.all().order_by('-date_added') + + +class LeastStockProductViewSet(viewsets.ModelViewSet): + # permission_classes = [IsAuthenticated] + http_method_names = ['get'] + serializer_class = ProductSerializer + queryset = Product.objects.all().order_by('quantity') + + def get_queryset(self): + return super().get_queryset()[:1] + # queryset = Product.objects.all().order_by('quantity').first() class LogViewSet(viewsets.ModelViewSet):