<s> from rest_framework import serializers <EOL> from shop import api as shop_api <EOL> from . . fields import MoneyField <EOL> class PurchaseRequestSerializer ( serializers . Serializer ) : <EOL> account_id = serializers . UUIDField ( allow_null = True ) <EOL> products = serializers . ListField ( allow_empty = False ) <EOL> def validate_products ( self , value ) : <EOL> for product in value : <EOL> if '<STR_LIT:id>' not in product or '<STR_LIT>' not in product : <EOL> raise serializers . ValidationError ( <EOL> '<STR_LIT>' <EOL> ) <EOL> product_obj = shop_api . get_product ( product [ '<STR_LIT:id>' ] ) <EOL> if product_obj is None : <EOL> raise serializers . ValidationError ( <EOL> '<STR_LIT>' . format ( product [ '<STR_LIT:id>' ] ) <EOL> ) <EOL> if product [ '<STR_LIT>' ] <= <NUM_LIT:0> : <EOL> raise serializers . ValidationError ( <EOL> '<STR_LIT>' <EOL> ) <EOL> return value <EOL> def as_purchase_kwargs ( self ) : <EOL> products = [ ( p [ '<STR_LIT:id>' ] , p [ '<STR_LIT>' ] ) <EOL> for p in self . validated_data [ '<STR_LIT>' ] ] <EOL> return { <EOL> '<STR_LIT>' : self . validated_data [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : products <EOL> } <EOL> class PurchaseSerializer ( serializers . Serializer ) : <EOL> amount = MoneyField ( ) </s>
