class BasketLineSerializer(OscarHyperlinkedModelSerializer): """ This serializer computes the prices of this line by using the basket strategy. """ url = DrillDownHyperlinkedIdentityField( view_name='basket-line-detail', extra_url_kwargs={'basket_pk': 'basket.id'}) attributes = LineAttributeSerializer(many=True, fields=('url', 'option', 'value'), required=False, read_only=True) price_excl_tax = serializers.DecimalField( decimal_places=2, max_digits=12, source='line_price_excl_tax_incl_discounts') price_incl_tax = TaxIncludedDecimalField( decimal_places=2, max_digits=12, excl_tax_field='line_price_excl_tax_incl_discounts', source='line_price_incl_tax_incl_discounts') price_incl_tax_excl_discounts = TaxIncludedDecimalField( decimal_places=2, max_digits=12, excl_tax_field='line_price_excl_tax', source='line_price_incl_tax') price_excl_tax_excl_discounts = serializers.DecimalField( decimal_places=2, max_digits=12, source='line_price_excl_tax') warning = serializers.CharField(read_only=True, required=False, source='get_warning') @property def basket_pk(self): return self.kwargs.get('basket_pk') class Meta: model = Line fields = overridable( 'OSCARAPI_BASKETLINE_FIELDS', default=('url', 'product', 'quantity', 'attributes', 'price_currency', 'price_excl_tax', 'price_incl_tax', 'price_incl_tax_excl_discounts', 'price_excl_tax_excl_discounts', 'is_tax_known', 'warning', 'basket', 'stockrecord', 'date_created')) def to_representation(self, obj): # This override is needed to reflect offer discounts or strategy # related prices immediately in the response operations.assign_basket_strategy(obj.basket, self.context['request']) # Oscar stores the calculated discount in line._discount_incl_tax or # line._discount_excl_tax when offers are applied. So by just # retrieving the line from the db you will loose this values, that's # why we need to get the line from the in-memory resultset here lines = (x for x in obj.basket.all_lines() if x.id == obj.id) line = next(lines, None) return super(BasketLineSerializer, self).to_representation(line)
class PriceSerializer(serializers.Serializer): currency = serializers.CharField( max_length=12, default=settings.OSCAR_DEFAULT_CURRENCY, required=False) excl_tax = serializers.DecimalField( decimal_places=2, max_digits=12, required=True) incl_tax = TaxIncludedDecimalField( excl_tax_field='excl_tax', decimal_places=2, max_digits=12, required=False) tax = TaxIncludedDecimalField( excl_tax_value='0.00', decimal_places=2, max_digits=12, required=False)
class BasketSerializer(serializers.HyperlinkedModelSerializer): lines = serializers.HyperlinkedIdentityField(view_name="basket-lines-list") offer_discounts = OfferDiscountSerializer(many=True, required=False) total_excl_tax = serializers.DecimalField(decimal_places=2, max_digits=12, required=False) total_excl_tax_excl_discounts = serializers.DecimalField(decimal_places=2, max_digits=12, required=False) total_incl_tax = TaxIncludedDecimalField(excl_tax_field="total_excl_tax", decimal_places=2, max_digits=12, required=False) total_incl_tax_excl_discounts = TaxIncludedDecimalField( excl_tax_field="total_excl_tax_excl_discounts", decimal_places=2, max_digits=12, required=False, ) total_tax = TaxIncludedDecimalField(excl_tax_value=Decimal("0.00"), decimal_places=2, max_digits=12, required=False) currency = serializers.CharField(required=False) voucher_discounts = VoucherDiscountSerializer(many=True, required=False) owner = serializers.HyperlinkedRelatedField( view_name="user-detail", required=False, allow_null=True, queryset=User.objects.all(), ) class Meta: model = Basket fields = overridable( "OSCARAPI_BASKET_FIELDS", default=( "id", "owner", "status", "lines", "url", "total_excl_tax", "total_excl_tax_excl_discounts", "total_incl_tax", "total_incl_tax_excl_discounts", "total_tax", "currency", "voucher_discounts", "offer_discounts", "is_tax_known", ), )
class PriceSerializer(_PriceSerializer): excl_tax = serializers.DecimalField(decimal_places=2, max_digits=12, required=True, localize=True) incl_tax = TaxIncludedDecimalField(excl_tax_field="excl_tax", decimal_places=2, max_digits=12, required=False, localize=True) tax = TaxIncludedDecimalField(excl_tax_value="0.00", decimal_places=2, max_digits=12, required=False, localize=True)
class BasketLineSerializer(OscarHyperlinkedModelSerializer): """ This serializer computes the prices of this line by using the basket strategy. """ url = DrillDownHyperlinkedIdentityField( view_name='basket-line-detail', extra_url_kwargs={'basket_pk': 'basket.id'}) attributes = LineAttributeSerializer(many=True, fields=('url', 'option', 'value'), required=False, read_only=True) price_excl_tax = serializers.DecimalField( decimal_places=2, max_digits=12, source='line_price_excl_tax_incl_discounts') price_incl_tax = TaxIncludedDecimalField( decimal_places=2, max_digits=12, excl_tax_field='line_price_excl_tax_incl_discounts', source='line_price_incl_tax_incl_discounts') price_incl_tax_excl_discounts = TaxIncludedDecimalField( decimal_places=2, max_digits=12, excl_tax_field='line_price_excl_tax', source='line_price_incl_tax') price_excl_tax_excl_discounts = serializers.DecimalField( decimal_places=2, max_digits=12, source='line_price_excl_tax') warning = serializers.CharField(read_only=True, required=False, source='get_warning') @property def basket_pk(self): return self.kwargs.get('basket_pk') class Meta: model = Line fields = overridable( 'OSCARAPI_BASKETLINE_FIELDS', default=('url', 'product', 'quantity', 'attributes', 'price_currency', 'price_excl_tax', 'price_incl_tax', 'price_incl_tax_excl_discounts', 'price_excl_tax_excl_discounts', 'is_tax_known', 'warning', 'basket', 'stockrecord', 'date_created'))
class BasketSerializer(serializers.HyperlinkedModelSerializer): lines = serializers.HyperlinkedIdentityField(view_name='basket-lines-list') offer_discounts = OfferDiscountSerializer(many=True, required=False) total_excl_tax = serializers.DecimalField(decimal_places=2, max_digits=12, required=False) total_excl_tax_excl_discounts = serializers.DecimalField(decimal_places=2, max_digits=12, required=False) total_incl_tax = TaxIncludedDecimalField(excl_tax_field='total_excl_tax', decimal_places=2, max_digits=12, required=False) total_incl_tax_excl_discounts = TaxIncludedDecimalField( excl_tax_field='total_excl_tax_excl_discounts', decimal_places=2, max_digits=12, required=False) total_tax = TaxIncludedDecimalField(excl_tax_value=Decimal('0.00'), decimal_places=2, max_digits=12, required=False) currency = serializers.CharField(required=False) voucher_discounts = VoucherDiscountSerializer(many=True, required=False) class Meta: model = Basket fields = overridable( 'OSCARAPI_BASKET_FIELDS', default=('id', 'owner', 'status', 'lines', 'url', 'total_excl_tax', 'total_excl_tax_excl_discounts', 'total_incl_tax', 'total_incl_tax_excl_discounts', 'total_tax', 'currency', 'voucher_discounts', 'offer_discounts', 'is_tax_known')) def get_validation_exclusions(self, instance=None): """ This is needed because oscar declared the owner field as ``null=True``, but ``blank=False``. That means the validator will claim you can not leave this value set to None. """ return super(BasketSerializer, self).get_validation_exclusions(instance) + ['owner']
class AssetLocationBasketSerializer(DynamicFieldsModelSerializer): offer_discounts = OfferDiscountSerializer(many=True, required=False) total_excl_tax = DecimalField( decimal_places=2, max_digits=12, required=False) total_excl_tax_excl_discounts = DecimalField( decimal_places=2, max_digits=12, required=False) total_incl_tax = TaxIncludedDecimalField( excl_tax_field='total_excl_tax', decimal_places=2, max_digits=12, required=False) total_incl_tax_excl_discounts = TaxIncludedDecimalField( excl_tax_field='total_excl_tax_excl_discounts', decimal_places=2, max_digits=12, required=False) total_tax = TaxIncludedDecimalField( excl_tax_value=Decimal('0.00'), decimal_places=2, max_digits=12, required=False) currency = CharField(required=False) voucher_discounts = VoucherDiscountSerializer(many=True, required=False) lines = AssetLocationBasketLineSerializer(many=True, required=False) class Meta: model = Basket fields = '__all__'
class BasketLineSerializer(OscarHyperlinkedModelSerializer): """ This serializer computes the prices of this line by using the basket strategy. """ url = DrillDownHyperlinkedIdentityField( view_name="basket-line-detail", extra_url_kwargs={"basket_pk": "basket.id"} ) attributes = LineAttributeSerializer( many=True, fields=("url", "option", "value"), required=False, read_only=True ) price_excl_tax = serializers.DecimalField( decimal_places=2, max_digits=12, source="line_price_excl_tax_incl_discounts", read_only=True, ) price_incl_tax = TaxIncludedDecimalField( decimal_places=2, max_digits=12, excl_tax_field="line_price_excl_tax_incl_discounts", source="line_price_incl_tax_incl_discounts", read_only=True, ) price_incl_tax_excl_discounts = TaxIncludedDecimalField( decimal_places=2, max_digits=12, excl_tax_field="line_price_excl_tax", source="line_price_incl_tax", read_only=True, ) price_excl_tax_excl_discounts = serializers.DecimalField( decimal_places=2, max_digits=12, source="line_price_excl_tax", read_only=True ) warning = serializers.CharField( read_only=True, required=False, source="get_warning" ) class Meta: model = Line fields = overridable( "OSCARAPI_BASKETLINE_FIELDS", default=( "url", "product", "quantity", "attributes", "price_currency", "price_excl_tax", "price_incl_tax", "price_incl_tax_excl_discounts", "price_excl_tax_excl_discounts", "is_tax_known", "warning", "basket", "stockrecord", "date_created", ), ) def to_representation(self, obj): # This override is needed to reflect offer discounts or strategy # related prices immediately in the response operations.assign_basket_strategy(obj.basket, self.context["request"]) # Oscar stores the calculated discount in line._discount_incl_tax or # line._discount_excl_tax when offers are applied. So by just # retrieving the line from the db you will loose this values, that's # why we need to get the line from the in-memory resultset here lines = (x for x in obj.basket.all_lines() if x.id == obj.id) line = next(lines, None) return super(BasketLineSerializer, self).to_representation(line)