83 lines
2.5 KiB
Python
83 lines
2.5 KiB
Python
from django.shortcuts import render
|
|
|
|
from rest_framework import viewsets, permissions, pagination, filters
|
|
|
|
from .models import Tag, Product, ProductOnBill, Receipt, Shop, ShopTag
|
|
from .serializers import TagSerializer, ProductSerializer, ProductOnBillSerializer, ReceiptSerializer, ShopSerializer, ShopTagSerializer
|
|
|
|
|
|
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)
|