Esempio n. 1
0
class ProductSerializer(Serializer):
    pk = fields.IntegerField()
    reference = fields.CharField()
    collection = CollectionSerializer()
    images = ImageSerializer(many=True)
    variant = VariantSerializer(many=True)
    name = fields.CharField()
    in_stock = fields.BooleanField()
    our_favorite = fields.BooleanField()
    is_discounted = fields.BooleanField()
    price_pre_tax = fields.DecimalField(5, 2)
    discounted_price = fields.DecimalField(5, 2)
    slug = fields.SlugField()
Esempio n. 2
0
    def test_basic_mapping(self, assert_dict_equals):
        """
        Confirm that the serializer can still handle models w/
        standard Django fields
        """
        class TestSerializer(DjongoModelSerializer):
            class Meta:
                model = GenericModel
                fields = '__all__'

        expected_dict = {
            'id':
            drf_fields.IntegerField(label='ID', read_only=True),
            'big_int':
            drf_fields.IntegerField(max_value=9223372036854775807,
                                    min_value=-9223372036854775808),
            'bool':
            drf_fields.BooleanField(),
            'char':
            drf_fields.CharField(max_length=20),
            'comma_int':
            ("CharField(validators=[<django.core.validators.RegexValidator "
             "object>, <django.core.validators.MaxLengthValidator object>])"),
            'date':
            drf_fields.DateField(),
            'date_time':
            drf_fields.DateTimeField(),
            'decimal':
            drf_fields.DecimalField(decimal_places=5, max_digits=10),
            'email':
            drf_fields.EmailField(max_length=254),
            'float':
            drf_fields.FloatField(),
            'integer':
            drf_fields.IntegerField(max_value=2147483647,
                                    min_value=-2147483648),
            'null_bool':
            drf_fields.NullBooleanField(required=False),
            'pos_int':
            drf_fields.IntegerField(max_value=2147483647, min_value=0),
            'pos_small_int':
            drf_fields.IntegerField(max_value=32767, min_value=0),
            'slug':
            drf_fields.SlugField(allow_unicode=False, max_length=50),
            'small_int':
            drf_fields.IntegerField(max_value=32767, min_value=-32768),
            'text':
            "CharField(style={'base_template': 'textarea.html'})",
            'time':
            drf_fields.TimeField(),
            'url':
            drf_fields.URLField(max_length=200),
            'ip':
            drf_fields.IPAddressField(),
            'uuid':
            "ModelField(model_field=<django.db.models.fields.UUIDField: uuid>)",
        }

        assert_dict_equals(TestSerializer().get_fields(), expected_dict)
class PetSerializer(DataclassSerializer):
    weight = fields.DecimalField(required=False,
                                 allow_null=True,
                                 max_digits=4,
                                 decimal_places=1)

    class Meta:
        dataclass = Pet
Esempio n. 4
0
class BalanceSerializer(serializers.Serializer):
    """Simple serializer for balance requests"""
    ledger_balance = fields.DecimalField(11, 2)
    balance = fields.DecimalField(11, 2)
Esempio n. 5
0
class CartSerializer(serializers.ModelSerializer):
    items = SubclassListSerializer(child=LineItemMetadataSerializer())
    new_item_url = fields.SerializerMethodField()
    subtotal = fields.DecimalField(
        max_digits=7, decimal_places=2, source="get_subtotal"
    )
    delivery_addresses = fields.SerializerMethodField()
    new_address_url = fields.SerializerMethodField()
    payment_methods = fields.SerializerMethodField()
    new_payment_method_url = fields.SerializerMethodField()
    adjustments = SubclassListSerializer(child=AdjustmentSerializer())
    new_adjustment_url = fields.SerializerMethodField()
    grand_total = fields.DecimalField(
        max_digits=7, decimal_places=2, source="get_grand_total"
    )
    is_complete = fields.SerializerMethodField()
    incomplete_reasons = fields.SerializerMethodField()
    is_authenticated = fields.SerializerMethodField()
    checkout_url = fields.SerializerMethodField()
    generated_at = fields.SerializerMethodField()
    email = fields.EmailField()
    compatible_version = fields.SerializerMethodField()
    incompatible_version = fields.SerializerMethodField()

    def get_new_item_url(self, _):
        return reverse("lorikeet:add-to-cart")

    def get_new_address_url(self, _):
        return reverse("lorikeet:new-address")

    def get_delivery_addresses(self, cart):
        selected = cart.delivery_address_subclass
        the_set = []

        if cart.user:
            the_set = cart.user.delivery_addresses.filter(
                active=True
            ).select_subclasses()

        if selected is not None and selected not in the_set:
            the_set = chain(the_set, [selected])

        return DeliveryAddressSerializer(
            instance=the_set, many=True, context={"cart": cart}
        ).data

    def get_new_payment_method_url(self, _):
        return reverse("lorikeet:new-payment-method")

    def get_payment_methods(self, cart):
        the_set = []
        selected = cart.payment_method_subclass

        if cart.user:
            the_set = cart.user.paymentmethod_set.filter(
                active=True
            ).select_subclasses()

        if selected is not None and selected not in the_set:
            the_set = chain(the_set, [selected])

        return PaymentMethodSerializer(
            instance=the_set, many=True, context={"cart": cart}
        ).data

    def get_new_adjustment_url(self, _):
        return reverse("lorikeet:new-adjustment")

    def get_generated_at(self, cart):
        return time()

    def get_is_complete(self, cart):
        return cart.is_complete()

    def get_incomplete_reasons(self, cart):
        return cart.errors.to_json()

    def get_is_authenticated(self, cart):
        return cart.user_id is not None

    def get_checkout_url(self, _):
        return reverse("lorikeet:checkout")

    def get_compatible_version(self, _):
        return 2

    def get_incompatible_version(self, _):
        return 1

    class Meta:
        model = models.Cart
        fields = (
            "items",
            "new_item_url",
            "delivery_addresses",
            "new_address_url",
            "payment_methods",
            "new_payment_method_url",
            "grand_total",
            "generated_at",
            "is_complete",
            "incomplete_reasons",
            "checkout_url",
            "is_authenticated",
            "email",
            "adjustments",
            "new_adjustment_url",
            "subtotal",
            "compatible_version",
            "incompatible_version",
        )