From eeea3b55cf4bb307da0dfe6b7809335617783810 Mon Sep 17 00:00:00 2001 From: Mojtaba-z Date: Wed, 26 Nov 2025 10:30:10 +0330 Subject: [PATCH] add - upload product image --- apps/product/services/upload_product_image.py | 28 ++++++++++++++ .../web/api/v1/viewsets/product_api.py | 38 ++++++++++++++++++- common/storage.py | 4 ++ 3 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 apps/product/services/upload_product_image.py create mode 100644 common/storage.py diff --git a/apps/product/services/upload_product_image.py b/apps/product/services/upload_product_image.py new file mode 100644 index 0000000..3061d7a --- /dev/null +++ b/apps/product/services/upload_product_image.py @@ -0,0 +1,28 @@ +from apps.product.models import Product +from common.generics import base64_to_image_file +from common.liara_tools import upload_to_liara + + +class UploadProductImage: + """ + upload product image + """ + + def upload_image(self, request, product_id): + product = Product.objects.get(id=product_id) + + image = base64_to_image_file( + request.data['image'], + filename=f'product_image_{product}' + ) + file, file_format = image[0], image[1] + + # upload document to liara + image_url = upload_to_liara( + file, + f'product_image_{product_id}.{file_format}' + ) + product.img = image_url + product.save() + + return product diff --git a/apps/product/web/api/v1/viewsets/product_api.py b/apps/product/web/api/v1/viewsets/product_api.py index 31d9bf3..68a1237 100644 --- a/apps/product/web/api/v1/viewsets/product_api.py +++ b/apps/product/web/api/v1/viewsets/product_api.py @@ -13,6 +13,7 @@ from apps.core.api import BaseViewSet from apps.core.mixins.search_mixin import DynamicSearchMixin from apps.core.mixins.soft_delete_mixin import SoftDeleteMixin from apps.product import models as product_models +from apps.product.services.upload_product_image import UploadProductImage from apps.product.web.api.v1.serializers import product_serializers as product_serializers from apps.product.web.api.v1.serializers import quota_serializers from common.helpers import get_organization_by_user @@ -69,7 +70,7 @@ class ProductCategoryViewSet(SoftDeleteMixin, viewsets.ModelViewSet, DynamicSear return Response(e, status=status.HTTP_204_NO_CONTENT) -class ProductViewSet(SoftDeleteMixin, viewsets.ModelViewSet, DynamicSearchMixin): +class ProductViewSet(SoftDeleteMixin, viewsets.ModelViewSet, DynamicSearchMixin, UploadProductImage): queryset = product_models.Product.objects.all() serializer_class = product_serializers.ProductSerializer filter_backends = [filters.SearchFilter] @@ -88,6 +89,41 @@ class ProductViewSet(SoftDeleteMixin, viewsets.ModelViewSet, DynamicSearchMixin) serializer = self.get_serializer(queryset, many=True) return Response(serializer.data) + @transaction.atomic + def create(self, request, *args, **kwargs): + """ + create product & upload image + """ + + serializer = self.serializer_class(data=request.data, context={"request": request}) + if serializer.is_valid(raise_exception=True): + product = serializer.save() + + # upload product image to liara + if 'image' in request.data.keys(): + self.upload_image(request=request, product_id=product.id) + + return Response(serializer.data, status=status.HTTP_201_CREATED) + return Response(serializer.errors, status=status.HTTP_403_FORBIDDEN) + + def update(self, request, pk=None, *args, **kwargs): + """ + update product & upload image + """ + + product = self.get_object() + + serializer = self.serializer_class(product, data=request.data, context={"request": request}) + if serializer.is_valid(raise_exception=True): + product = serializer.save() + + # upload product image to liara + if 'image' in request.data.keys(): + self.upload_image(request=request, product_id=product.id) + + return Response(serializer.data, status=status.HTTP_201_CREATED) + return Response(serializer.errors, status=status.HTTP_403_FORBIDDEN) + @action( methods=['get'], detail=True, diff --git a/common/storage.py b/common/storage.py new file mode 100644 index 0000000..a9d5b5f --- /dev/null +++ b/common/storage.py @@ -0,0 +1,4 @@ +STORAGE_ENDPOINT = 'https://s3.rasadyar.com/rasaddam' +STORAGE_BUCKET_NAME = 'ticket-rasadyar' +STORAGE_ACCESS_KEY = "zG3ewsbYsTqCmuws" +STORAGE_SECRET_KEY = 'RInUMB78zlQZp6CNf8+sRoSh2cNDHcGQhXrLnTJ1AuI='