<s> from __future__ import unicode_literals <EOL> import os <EOL> from django . db . models import Q <EOL> from django . shortcuts import get_object_or_404 <EOL> from django . utils . cache import add_never_cache_headers <EOL> from django . utils . translation import get_language_from_request <EOL> from rest_framework import generics <EOL> from rest_framework import status <EOL> from rest_framework import views <EOL> from rest_framework . settings import api_settings <EOL> from rest_framework . renderers import BrowsableAPIRenderer <EOL> from rest_framework . response import Response <EOL> from shop import settings as shop_settings <EOL> from shop . rest . money import JSONRenderer <EOL> from shop . rest . filters import CMSPagesFilterBackend <EOL> from shop . rest . serializers import AddToCartSerializer , ProductSelectSerializer <EOL> from shop . rest . renderers import CMSPageRenderer <EOL> from shop . models . product import ProductModel <EOL> class ProductListView ( generics . ListAPIView ) : <EOL> """<STR_LIT>""" <EOL> product_model = ProductModel <EOL> serializer_class = None <EOL> filter_class = None <EOL> limit_choices_to = Q ( ) <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> response = self . list ( request , * args , ** kwargs ) <EOL> add_never_cache_headers ( response ) <EOL> return response <EOL> def get_queryset ( self ) : <EOL> qs = self . product_model . objects . filter ( self . limit_choices_to ) <EOL> if hasattr ( self . product_model , '<STR_LIT>' ) : <EOL> language = get_language_from_request ( self . request ) <EOL> qs = qs . prefetch_related ( '<STR_LIT>' ) . filter ( translations__language_code = language ) <EOL> qs = qs . select_related ( '<STR_LIT>' ) <EOL> return qs <EOL> def get_template_names ( self ) : <EOL> return [ self . request . current_page . get_template ( ) ] <EOL> class CMSPageProductListView ( ProductListView ) : <EOL> """<STR_LIT>""" <EOL> renderer_classes = ( CMSPageRenderer , JSONRenderer , BrowsableAPIRenderer ) <EOL> filter_backends = list ( api_settings . DEFAULT_FILTER_BACKENDS ) <EOL> filter_backends . append ( CMSPagesFilterBackend ( ) ) <EOL> def filter_queryset ( self , queryset ) : <EOL> self . filter_context = None <EOL> if self . filter_class : <EOL> filter_instance = self . filter_class ( self . request . query_params , queryset = queryset ) <EOL> if callable ( getattr ( filter_instance , '<STR_LIT>' , None ) ) : <EOL> self . filter_context = filter_instance . get_render_context ( ) <EOL> elif hasattr ( filter_instance , '<STR_LIT>' ) : <EOL> self . filter_context = filter_instance . render_context <EOL> qs = super ( CMSPageProductListView , self ) . filter_queryset ( queryset ) <EOL> return qs <EOL> def get_renderer_context ( self ) : <EOL> renderer_context = super ( CMSPageProductListView , self ) . get_renderer_context ( ) <EOL> if renderer_context [ '<STR_LIT>' ] . accepted_renderer . format == '<STR_LIT:html>' : <EOL> renderer_context [ '<STR_LIT>' ] = self . filter_context <EOL> return renderer_context <EOL> class SyncCatalogView ( views . APIView ) : <EOL> """<STR_LIT>""" <EOL> renderer_classes = ( JSONRenderer , BrowsableAPIRenderer ) <EOL> product_model = ProductModel <EOL> product_field = '<STR_LIT>' <EOL> serializer_class = None <EOL> filter_class = None <EOL> limit_choices_to = Q ( ) <EOL> def get_context ( self , request , ** kwargs ) : <EOL> filter_kwargs = { '<STR_LIT:id>' : request . data . get ( '<STR_LIT:id>' ) } <EOL> if hasattr ( self . product_model , '<STR_LIT>' ) : <EOL> filter_kwargs . update ( translations__language_code = get_language_from_request ( self . request ) ) <EOL> queryset = self . product_model . objects . filter ( self . limit_choices_to , ** filter_kwargs ) <EOL> product = get_object_or_404 ( queryset ) <EOL> return { self . product_field : product , '<STR_LIT>' : request } <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> context = self . get_context ( request , ** kwargs ) <EOL> serializer = self . serializer_class ( context = context , ** kwargs ) <EOL> return Response ( serializer . data ) <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> context = self . get_context ( request , ** kwargs ) <EOL> serializer = self . serializer_class ( data = request . data , context = context ) <EOL> if serializer . is_valid ( ) : <EOL> return Response ( serializer . data , status = status . HTTP_202_ACCEPTED ) <EOL> return Response ( serializer . errors , status = status . HTTP_400_BAD_REQUEST ) <EOL> class AddToCartView ( views . APIView ) : <EOL> """<STR_LIT>""" <EOL> renderer_classes = ( JSONRenderer , BrowsableAPIRenderer ) <EOL> product_model = ProductModel <EOL> serializer_class = AddToCartSerializer <EOL> lookup_field = lookup_url_kwarg = '<STR_LIT>' <EOL> limit_choices_to = Q ( ) <EOL> def get_context ( self , request , ** kwargs ) : <EOL> assert self . lookup_url_kwarg in kwargs <EOL> filter_kwargs = { self . lookup_field : kwargs . pop ( self . lookup_url_kwarg ) } <EOL> if hasattr ( self . product_model , '<STR_LIT>' ) : <EOL> filter_kwargs . update ( translations__language_code = get_language_from_request ( self . request ) ) <EOL> queryset = self . product_model . objects . filter ( self . limit_choices_to , ** filter_kwargs ) <EOL> product = get_object_or_404 ( queryset ) <EOL> return { '<STR_LIT>' : product , '<STR_LIT>' : request } <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> context = self . get_context ( request , ** kwargs ) <EOL> serializer = self . serializer_class ( context = context , ** kwargs ) <EOL> return Response ( serializer . data ) <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> context = self . get_context ( request , ** kwargs ) <EOL> serializer = self . serializer_class ( data = request . data , context = context ) <EOL> if serializer . is_valid ( ) : <EOL> return Response ( serializer . data , status = status . HTTP_202_ACCEPTED ) <EOL> return Response ( serializer . errors , status = status . HTTP_400_BAD_REQUEST ) <EOL> class ProductRetrieveView ( generics . RetrieveAPIView ) : <EOL> """<STR_LIT>""" <EOL> renderer_classes = ( CMSPageRenderer , JSONRenderer , BrowsableAPIRenderer ) <EOL> lookup_field = lookup_url_kwarg = '<STR_LIT>' <EOL> product_model = ProductModel <EOL> serializer_class = None <EOL> limit_choices_to = Q ( ) <EOL> def get_template_names ( self ) : <EOL> product = self . get_object ( ) <EOL> app_label = product . _meta . app_label . lower ( ) <EOL> basename = '<STR_LIT>' . format ( product . __class__ . __name__ . lower ( ) ) <EOL> return [ <EOL> os . path . join ( app_label , '<STR_LIT>' , basename ) , <EOL> os . path . join ( app_label , '<STR_LIT>' ) , <EOL> '<STR_LIT>' , <EOL> ] <EOL> def get_renderer_context ( self ) : <EOL> renderer_context = super ( ProductRetrieveView , self ) . get_renderer_context ( ) <EOL> if renderer_context [ '<STR_LIT>' ] . accepted_renderer . format == '<STR_LIT:html>' : <EOL> renderer_context [ '<STR_LIT>' ] = self . get_object ( ) <EOL> renderer_context [ '<STR_LIT>' ] = shop_settings . ADD2CART_NG_MODEL_OPTIONS <EOL> return renderer_context <EOL> def get_object ( self ) : <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> assert self . lookup_url_kwarg in self . kwargs <EOL> filter_kwargs = { self . lookup_field : self . kwargs [ self . lookup_url_kwarg ] } <EOL> if hasattr ( self . product_model , '<STR_LIT>' ) : <EOL> filter_kwargs . update ( translations__language_code = get_language_from_request ( self . request ) ) <EOL> queryset = self . product_model . objects . filter ( self . limit_choices_to , ** filter_kwargs ) <EOL> self . _product = get_object_or_404 ( queryset ) <EOL> return self . _product <EOL> class ProductSelectView ( generics . ListAPIView ) : <EOL> """<STR_LIT>""" <EOL> renderer_classes = ( JSONRenderer , BrowsableAPIRenderer ) <EOL> serializer_class = ProductSelectSerializer <EOL> def get_queryset ( self ) : <EOL> term = self . request . GET . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if len ( term ) >= <NUM_LIT:2> : <EOL> return ProductModel . objects . select_lookup ( term ) [ : <NUM_LIT:10> ] <EOL> return ProductModel . objects . all ( ) [ : <NUM_LIT:10> ] </s>
