<s> """<STR_LIT>""" <EOL> import logging <EOL> from django . core . exceptions import MultipleObjectsReturned <EOL> from django . shortcuts import get_object_or_404 <EOL> from rest_framework . generics import CreateAPIView , DestroyAPIView <EOL> from rest_framework . response import Response <EOL> from rest_framework import serializers , status <EOL> from . . models import CartItem , Plan <EOL> from . . mixins import CartMixin <EOL> LOGGER = logging . getLogger ( __name__ ) <EOL> class PlanRelatedField ( serializers . RelatedField ) : <EOL> def __init__ ( self , ** kwargs ) : <EOL> super ( PlanRelatedField , self ) . __init__ ( <EOL> queryset = Plan . objects . all ( ) , ** kwargs ) <EOL> def to_representation ( self , obj ) : <EOL> return obj . slug <EOL> def to_internal_value ( self , data ) : <EOL> return get_object_or_404 ( Plan , slug = data ) <EOL> class CartItemSerializer ( serializers . ModelSerializer ) : <EOL> plan = PlanRelatedField ( read_only = False , required = True ) <EOL> class Meta : <EOL> model = CartItem <EOL> fields = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:email>' ) <EOL> class CartItemAPIView ( CartMixin , CreateAPIView ) : <EOL> """<STR_LIT>""" <EOL> model = CartItem <EOL> serializer_class = CartItemSerializer <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> serializer = self . get_serializer ( data = request . data ) <EOL> if serializer . is_valid ( ) : <EOL> cart_item , created = self . insert_item ( request , ** serializer . data ) <EOL> if isinstance ( cart_item , CartItem ) : <EOL> cart_item = serializer . to_representation ( cart_item ) <EOL> if created : <EOL> headers = self . get_success_headers ( cart_item ) <EOL> return Response ( cart_item , status = status . HTTP_201_CREATED , <EOL> headers = headers ) <EOL> else : <EOL> headers = self . get_success_headers ( cart_item ) <EOL> return Response ( cart_item , status = status . HTTP_200_OK , <EOL> headers = headers ) <EOL> return Response ( serializer . errors , status = status . HTTP_400_BAD_REQUEST ) <EOL> class CartItemDestroyAPIView ( DestroyAPIView ) : <EOL> """<STR_LIT>""" <EOL> model = CartItem <EOL> @ staticmethod <EOL> def destroy_in_session ( request , * args , ** kwargs ) : <EOL> cart_items = [ ] <EOL> if request . session . has_key ( '<STR_LIT>' ) : <EOL> cart_items = request . session [ '<STR_LIT>' ] <EOL> candidate = kwargs . get ( '<STR_LIT>' ) <EOL> serialized_cart_items = [ ] <EOL> found = False <EOL> for item in cart_items : <EOL> if item [ '<STR_LIT>' ] == candidate : <EOL> found = True <EOL> continue <EOL> serialized_cart_items += [ item ] <EOL> request . session [ '<STR_LIT>' ] = serialized_cart_items <EOL> return found <EOL> def get_object ( self ) : <EOL> result = None <EOL> try : <EOL> result = get_object_or_404 ( CartItem , <EOL> plan__slug = self . kwargs . get ( '<STR_LIT>' ) , <EOL> user = self . request . user , recorded = False ) <EOL> except MultipleObjectsReturned as err : <EOL> LOGGER . exception ( err ) <EOL> result = CartItem . objects . filter ( <EOL> plan__slug = self . kwargs . get ( '<STR_LIT>' ) , <EOL> user = self . request . user , recorded = False ) . first ( ) <EOL> return result <EOL> def delete ( self , request , * args , ** kwargs ) : <EOL> destroyed = self . destroy_in_session ( request , * args , ** kwargs ) <EOL> if not destroyed and self . request . user . is_authenticated ( ) : <EOL> return self . destroy ( request , * args , ** kwargs ) <EOL> return Response ( status = status . HTTP_204_NO_CONTENT ) </s>
