BuyAndKnow/bk_api/bills/views.py

110 lines
3.3 KiB
Python
Raw Normal View History

2020-01-05 21:41:14 +01:00
from django.shortcuts import render
2020-01-27 14:46:28 +01:00
from rest_framework.views import APIView
from rest_framework.response import Response
2020-01-05 21:41:14 +01:00
2020-01-20 00:05:18 +01:00
from rest_framework import viewsets, permissions, pagination, filters
2020-01-27 14:46:28 +01:00
from django.http import JsonResponse
2020-01-05 21:41:14 +01:00
from .models import Tag, Product, ProductOnBill, Receipt, Shop, ShopTag
from .serializers import TagSerializer, ProductSerializer, ProductOnBillSerializer, ReceiptSerializer, ShopSerializer, ShopTagSerializer
2020-01-27 14:46:28 +01:00
from .statistics import get_general_stats, get_shops_stats, get_products_stats
2020-01-05 21:41:14 +01:00
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
2020-01-27 14:46:28 +01:00
2020-01-05 21:41:14 +01:00
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
2020-01-20 00:05:18 +01:00
2020-01-05 21:41:14 +01:00
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
2020-01-20 00:05:18 +01:00
filter_backends = [filters.OrderingFilter]
ordering = ['-date']
2020-01-05 21:41:14 +01:00
def get_queryset(self):
return self.request.user.receipts.all()
def perform_create(self, serializer):
serializer.save(owner=self.request.user)
2020-01-27 14:46:28 +01:00
# === 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)