add - upload product image
This commit is contained in:
28
apps/product/services/upload_product_image.py
Normal file
28
apps/product/services/upload_product_image.py
Normal file
@@ -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
|
||||
@@ -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,
|
||||
|
||||
4
common/storage.py
Normal file
4
common/storage.py
Normal file
@@ -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='
|
||||
Reference in New Issue
Block a user