Added logs url

This commit is contained in:
keannu125 2023-03-07 17:23:39 +08:00
parent 2ea9d7a1de
commit 17495c960b
4 changed files with 36 additions and 6 deletions

View file

@ -1,3 +1,7 @@
from django.contrib import admin from django.contrib import admin
from simple_history.admin import SimpleHistoryAdmin
from .models import Product
# Register your models here. # Register your models here.
admin.site.register(Product, SimpleHistoryAdmin)

View file

@ -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 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): class ProductSerializer(serializers.HyperlinkedModelSerializer):
date_added = serializers.DateTimeField( date_added = serializers.DateTimeField(
format="%d-%m-%Y %I:%M%p", read_only=True) format="%d-%m-%Y %I:%M%p", read_only=True)
quantity = serializers.IntegerField(required=False, default=0) quantity = serializers.IntegerField(required=False, default=0)
history = HistoricalRecordField(read_only=True)
class Meta: class Meta:
model = Product model = Product
fields = ('id', 'name', 'quantity', 'date_added') fields = ('id', 'name', 'quantity', 'date_added', 'history')
read_only_fields = ('id', 'date_added') 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')

View file

@ -4,10 +4,10 @@ from . import views
router = routers.DefaultRouter() router = routers.DefaultRouter()
router.register(r'products', views.ProductViewSet) router.register(r'products', views.ProductViewSet)
router.register(r'logs', views.LogViewSet)
# Wire up our API using automatic URL routing. # Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API. # Additionally, we include login URLs for the browsable API.
urlpatterns = [ urlpatterns = [
path('', include(router.urls)), path('', include(router.urls)),
] ]

View file

@ -1,10 +1,17 @@
from rest_framework.permissions import IsAuthenticated from rest_framework.permissions import IsAuthenticated
from rest_framework import viewsets from rest_framework import viewsets
from .serializers import ProductSerializer from .serializers import ProductSerializer, LogSerializer
from .models import Product from .models import Product
class ProductViewSet(viewsets.ModelViewSet): class ProductViewSet(viewsets.ModelViewSet):
# permission_classes = [IsAuthenticated]
serializer_class = ProductSerializer serializer_class = ProductSerializer
queryset = Product.objects.all() 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')