from django.shortcuts import render from rest_framework.views import APIView from rest_framework.response import Response from rest_framework import viewsets, permissions, pagination, filters from django.http import JsonResponse from .models import Tag, Product, ProductOnBill, Receipt, Shop, ShopTag from .serializers import TagSerializer, ProductSerializer, ProductOnBillSerializer, ReceiptSerializer, ShopSerializer, ShopTagSerializer from .statistics import get_general_stats, get_shops_stats, get_products_stats class TagViewSet(viewsets.ModelViewSet): queryset = Tag.objects.all() # permission_classes = [permissions.AllowAny] serializer_class = TagSerializer def get_queryset(self): return self.request.user.tags.all() def perform_create(self, serializer): serializer.save(owner=self.request.user) class ShopTagViewSet(viewsets.ModelViewSet): queryset = ShopTag.objects.all() # permission_classes = [permissions.AllowAny] serializer_class = ShopTagSerializer def get_queryset(self): return self.request.user.shop_tags.all() def perform_create(self, serializer): serializer.save(owner=self.request.user) class ShopViewSet(viewsets.ModelViewSet): queryset = Shop.objects.all() # permission_classes = [permissions.AllowAny] serializer_class = ShopSerializer def get_queryset(self): return self.request.user.shops.all() def perform_create(self, serializer): serializer.save(owner=self.request.user) class ProductViewSet(viewsets.ModelViewSet): queryset = Product.objects.all() # permission_classes = [permissions.AllowAny] serializer_class = ProductSerializer def get_queryset(self): return self.request.user.products.all() def perform_create(self, serializer): serializer.save(owner=self.request.user) class ProductOnBillViewSet(viewsets.ModelViewSet): queryset = ProductOnBill.objects.all() # permission_classes = [permissions.AllowAny] serializer_class = ProductOnBillSerializer def get_queryset(self): return self.request.user.products_on_bills.all() def perform_create(self, serializer): serializer.save(owner=self.request.user) class StandardResultsSetPagination(pagination.PageNumberPagination): page_size = 10 page_size_query_param = 'page_size' class ReceiptViewSet(viewsets.ModelViewSet): queryset = Receipt.objects.all() serializer_class = ReceiptSerializer pagination_class = StandardResultsSetPagination filter_backends = [filters.OrderingFilter] ordering = ['-date'] def get_queryset(self): return self.request.user.receipts.all() def perform_create(self, serializer): serializer.save(owner=self.request.user) # === STATS VIEWS === # def GeneralStats(APIView): # # authentication_classes = [authentication.TokenAuthentication] # # permission_classes = [permissions.IsAdminUser] # def get(self, request, format=None): # print(request.user) # return Response(get_general_stats(request.user)) # # def shops_stats_view(request): # # responseData = get_shops_stats(request.user) # # return JsonResponse(responseData, safe=False) # # def products_stats_view(request): # # responseData = get_products_stats(request.user) # # return JsonResponse(responseData, safe=False)