from rest_framework.decorators import action from rest_framework.permissions import AllowAny from rest_framework.response import Response from rest_framework.viewsets import ReadOnlyModelViewSet, GenericViewSet from core.mixins import ActionBasedMixin from graph.models import Transaction, MonthlyTransaction from graph.serializers import ( TransactionSerializer, MonthlyTransactionSerializer, ) from market.models import Product class TransactionViewset(ActionBasedMixin, ReadOnlyModelViewSet): queryset = Transaction.objects.all().select_related("product") serializer_class = TransactionSerializer permission_classes = [AllowAny] @action(detail=False, methods=["GET"]) def monthly(self, request): queryset = MonthlyTransaction.objects.all() page = self.paginate_queryset(queryset) if page is not None: serializer = MonthlyTransactionSerializer(page, many=True) return self.get_paginated_response(serializer.data) serializer = MonthlyTransactionSerializer(queryset, many=True) return Response(serializer.data) class MonthlyTransactionViewset(ActionBasedMixin, ReadOnlyModelViewSet): queryset = MonthlyTransaction.objects.all().select_related("product") serializer_class = MonthlyTransactionSerializer permission_classes = [AllowAny] class GraphViewset(ActionBasedMixin, GenericViewSet): queryset = Product.objects.all() queryset_map = {} serializer_class = MonthlyTransactionSerializer def retrieve(self, request, *args, **kwargs): product = self.get_object() data = reversed(MonthlyTransaction.objects.filter(product=product)[:6]) serializer = self.get_serializer(data, many=True) return Response(serializer.data)