From 17495c960bf7a7cbdef28672c1cc978efa29e858 Mon Sep 17 00:00:00 2001 From: keannu125 Date: Tue, 7 Mar 2023 17:23:39 +0800 Subject: [PATCH] Added logs url --- ivy/products/admin.py | 4 ++++ ivy/products/serializers.py | 25 ++++++++++++++++++++++--- ivy/products/urls.py | 2 +- ivy/products/views.py | 11 +++++++++-- 4 files changed, 36 insertions(+), 6 deletions(-) diff --git a/ivy/products/admin.py b/ivy/products/admin.py index ea5d68b..8b1c519 100644 --- a/ivy/products/admin.py +++ b/ivy/products/admin.py @@ -1,3 +1,7 @@ from django.contrib import admin +from simple_history.admin import SimpleHistoryAdmin +from .models import Product # Register your models here. + +admin.site.register(Product, SimpleHistoryAdmin) diff --git a/ivy/products/serializers.py b/ivy/products/serializers.py index 85d5dc3..b88b5ce 100644 --- a/ivy/products/serializers.py +++ b/ivy/products/serializers.py @@ -1,13 +1,32 @@ -from rest_framework import serializers +from rest_framework import serializers, mixins +from simple_history.models import HistoricalRecords from .models import Product +class HistoricalRecordField(serializers.ListField): + child = serializers.DictField() + + def to_representation(self, data): + return super().to_representation(data.values('id', 'history_date').order_by('history_date')) + + class ProductSerializer(serializers.HyperlinkedModelSerializer): date_added = serializers.DateTimeField( format="%d-%m-%Y %I:%M%p", read_only=True) quantity = serializers.IntegerField(required=False, default=0) + history = HistoricalRecordField(read_only=True) class Meta: model = Product - fields = ('id', 'name', 'quantity', 'date_added') - read_only_fields = ('id', 'date_added') + fields = ('id', 'name', 'quantity', 'date_added', 'history') + read_only_fields = ('id', 'date_added', 'history') + + +class LogSerializer(serializers.HyperlinkedModelSerializer): + history_date = serializers.DateTimeField( + format="%d-%m-%Y %I:%M%p", read_only=True) + + class Meta: + model = Product.history.model + fields = ('history_id', 'name', 'quantity', 'history_date') + read_only_fields = ('history_id', 'name', 'quantity', 'history_date') diff --git a/ivy/products/urls.py b/ivy/products/urls.py index 033f097..88a3c5b 100644 --- a/ivy/products/urls.py +++ b/ivy/products/urls.py @@ -4,10 +4,10 @@ from . import views router = routers.DefaultRouter() router.register(r'products', views.ProductViewSet) +router.register(r'logs', views.LogViewSet) # Wire up our API using automatic URL routing. # Additionally, we include login URLs for the browsable API. urlpatterns = [ path('', include(router.urls)), - ] diff --git a/ivy/products/views.py b/ivy/products/views.py index 4f3eb0d..18034ea 100644 --- a/ivy/products/views.py +++ b/ivy/products/views.py @@ -1,10 +1,17 @@ from rest_framework.permissions import IsAuthenticated from rest_framework import viewsets -from .serializers import ProductSerializer +from .serializers import ProductSerializer, LogSerializer from .models import Product class ProductViewSet(viewsets.ModelViewSet): - + # permission_classes = [IsAuthenticated] serializer_class = ProductSerializer queryset = Product.objects.all() + + +class LogViewSet(viewsets.ModelViewSet): + # permission_classes = [IsAuthenticated] + http_method_names = ['get'] + serializer_class = LogSerializer + queryset = Product.history.all().order_by('history_date')