Exemple #1
0
class AnonymousProductSerializer(ProductSerializer):
    image = serializers.ImageField(required=False)
    region_id = serializers.IntegerField(write_only=True, required=False)
    region_ids = serializers.ListField(
        child=serializers.IntegerField(write_only=True),
        write_only=True,
        source="regions",
        required=False,
    )
    region = RegionSerializer(read_only=True)
    regions = RegionSerializer(many=True, required=False, read_only=True)

    class Meta:
        model = Product
        fields = (
            "name",
            "description",
            "id",
            "tags",
            "image_url",
            "image",
            "category",
            "category_id",
            "seller",
            "is_organic",
            "is_vegan",
            "is_gluten_free",
            "is_grazing_animal",
            "is_gmo_free",
            "amount",
            "unit",
            "vat",
            "container_type",
            "container_type_id",
            "container_description",
            "delivery_charge",
            "delivery_options",
            "delivery_options_ids",
            "tags",
            "ean8",
            "ean13",
            "sellers_product_identifier",
            "items_available",
            "is_available",
            "created_at",
            "updated_at",
            "seller_id",
            "delivery_requirements",
            "third_party_delivery",
            "base_unit",
            "item_quantity",
            "region_id",
            "region_ids",
            "region",
            "regions",
        )
        depth = 2
        ordering = ["-created"]
Exemple #2
0
class ListProductSerializer(ImageFallbackMixin, serializers.ModelSerializer):
    items_available = serializers.IntegerField()
    category = CategorySerializer()
    region = RegionSerializer()
    seller = ListProductSellerSerializer()

    class Meta:
        model = Product
        fields = (
            "id",
            "name",
            "image",
            "category",
            "seller",
            "region",
            "items_available",
            "price",
            "unit",
            "amount",
        )
        read_only_fields = (
            "id",
            "name",
            "image",
            "category",
            "seller",
            "region",
            "items_available",
            "price",
            "unit",
            "amount",
        )
        image_fallback_fields = [
            ("image", "image_url"),
        ]
class UserPersonalProfile(serializers.ModelSerializer):
    region = RegionSerializer(read_only=True)

    class Meta:
        model = User
        fields = [
            "first_name",
            "last_name",
            "email",
            "invoice_email",
            "region",
            "birthday",
            "nationality_country_code",
            "phone",
            "residence_country_code",
        ]
Exemple #4
0
class ProductSerializer(
        BaseProductSerializer,
        TaggitSerializer,
        ImageFallbackMixin,
        serializers.ModelSerializer,
):
    id = serializers.ReadOnlyField()
    tags = CustomTagListSerializerField(allow_null=True, required=False)
    items_available = serializers.IntegerField(read_only=True)
    is_available = serializers.BooleanField(read_only=True)
    seller_id = serializers.IntegerField(write_only=True, required=False)
    category_id = serializers.IntegerField(write_only=True)
    seller = AnonymousUserSerializer(read_only=True)
    container_type_id = serializers.IntegerField(write_only=True)
    delivery_options_ids = serializers.ListField(
        child=serializers.IntegerField(write_only=True),
        write_only=True,
        source="delivery_options",
    )
    image = serializers.ImageField(required=False)
    item_quantity = BlankableDecimalField(required=False,
                                          allow_null=True,
                                          decimal_places=2,
                                          max_digits=10)
    region_id = serializers.IntegerField(write_only=True, required=False)
    region = RegionSerializer(read_only=True)
    regions = CustomRegionsSerializerField(allow_null=True, required=False)

    def validate(self, data):
        forced_seller_id = data.get("seller_id")
        request_user = self.context["request"].user

        if (not request_user.is_seller and request_user.is_admin
                and not forced_seller_id):
            raise ValidationError({"seller_id": ["This field is required."]})

        if (forced_seller_id and forced_seller_id != request_user.id
                and not request_user.is_admin):
            # TODO: forced_seller_id != request_user.id is a quick fix for the frontend issue.
            # The frontend should not send sellerId when a seller wants to add/edit his product.
            raise PermissionDenied(
                {"seller_id": ["Only admin can use this field."]})

        if request_user.is_admin and forced_seller_id:
            data["seller_id"] = forced_seller_id
        else:
            data["seller_id"] = request_user.id

        return data

    class Meta:
        model = Product
        fields = (
            "name",
            "description",
            "items",
            "id",
            "tags",
            "image_url",
            "image",
            "category",
            "category_id",
            "seller",
            "is_organic",
            "is_vegan",
            "is_gluten_free",
            "is_grazing_animal",
            "is_gmo_free",
            "amount",
            "unit",
            "price",
            "vat",
            "container_type",
            "container_type_id",
            "container_description",
            "delivery_charge",
            "delivery_options",
            "delivery_options_ids",
            "tags",
            "ean8",
            "ean13",
            "sellers_product_identifier",
            "items_available",
            "is_available",
            "created_at",
            "updated_at",
            "seller_id",
            "delivery_requirements",
            "third_party_delivery",
            "base_unit",
            "item_quantity",
            "region_id",
            "region",
            "regions",
        )
        depth = 2
        ordering = ["-created"]
Exemple #5
0
 def to_representation(self, value):
     if type(value) is not list:
         return RegionSerializer(value, many=True).data
     return value
Exemple #6
0
class ProductDetailsSerializer(
        BaseProductSerializer,
        ImageFallbackMixin,
        serializers.ModelSerializer,
        TaggitSerializer,
):
    items_available = serializers.IntegerField(read_only=True)
    tags = CustomTagListSerializerField(allow_null=True, required=False)
    seller = AnonymousUserSerializer(read_only=True)
    image = serializers.ImageField(required=False)
    delivery = serializers.SerializerMethodField()
    category = CategorySerializer()
    container_type = ContainerSerializer()
    region_id = serializers.IntegerField(write_only=True, required=False)
    regions = CustomRegionsSerializerField(allow_null=True, required=False)
    region = RegionSerializer(read_only=True)

    class Meta:
        model = Product
        fields = (
            "name",
            "description",
            "id",
            "image",
            "category",
            "seller",
            "is_organic",
            "is_vegan",
            "is_gluten_free",
            "is_grazing_animal",
            "is_gmo_free",
            "amount",
            "unit",
            "price",
            "vat",
            "container_type",
            "delivery_charge",
            "delivery_options",
            "third_party_delivery",
            "items_available",
            "delivery",
            "region_id",
            "regions",
            "region",
            "ean8",
            "ean13",
            "sellers_product_identifier",
            "tags",
        )
        read_only_fields = ("id", )
        depth = 2
        ordering = ["-created"]

    def get_delivery(self, obj):
        request = self.context.get("request")

        cart = Cart(user=request.user)
        seller_delivery_cart_item = CartItem(
            product=obj,
            quantity=1,
            delivery_option_id=DeliveryOption.SELLER,
            cart=cart)

        options = {
            "seller": seller_delivery_cart_item.seller_delivery_fee.netto,
            "pickup": Decimal("0.0"),
        }

        region_settings = get_setting(request.region.id)
        if region_settings.central_logistics_company:
            central_logistics_cart_item = CartItem(
                product=obj,
                quantity=1,
                delivery_option_id=DeliveryOption.CENTRAL_LOGISTICS,
                cart=cart,
            )
            options[
                "logistics"] = central_logistics_cart_item.central_logistic_delivery_fee.netto

        return options