Source code for shoop.core.api.products
# -*- coding: utf-8 -*-
# This file is part of Shoop.
#
# Copyright (c) 2012-2016, Shoop Ltd. All rights reserved.
#
# This source code is licensed under the AGPLv3 license found in the
# LICENSE file in the root directory of this source tree.
from parler_rest.fields import TranslatedFieldsField
from parler_rest.serializers import TranslatableModelSerializer
from rest_framework.serializers import ModelSerializer
from rest_framework.viewsets import ModelViewSet
from shoop.core.models import Product, ShopProduct
[docs]class ShopProductSerializer(ModelSerializer):
[docs]class ProductSerializer(TranslatableModelSerializer):
translations = TranslatedFieldsField(shared_model=Product)
shop_products = ShopProductSerializer(many=True, read_only=True)
[docs]class ProductViewSet(ModelViewSet):
queryset = Product.objects.none()
serializer_class = ProductSerializer
[docs] def get_queryset(self):
if getattr(self.request.user, 'is_superuser', False):
return Product.objects.all_except_deleted()
return Product.objects.list_visible(
customer=self.request.customer,
shop=self.request.shop
)
[docs]class ShopProductViewSet(ModelViewSet):
queryset = ShopProduct.objects.none()
serializer_class = ShopProductSerializer
[docs] def get_queryset(self):
if getattr(self.request.user, 'is_superuser', False):
products = Product.objects.all_except_deleted()
else:
products = Product.objects.list_visible(
customer=self.request.customer,
shop=self.request.shop
)
return ShopProduct.objects.filter(id__in=products)