media update route
This commit is contained in:
parent
258e13c808
commit
4388253bf9
|
|
@ -1,5 +1,9 @@
|
|||
from django.db import models
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from botocore.exceptions import ClientError
|
||||
from django.conf import settings
|
||||
from project.storage_backends import PublicMediaStorage
|
||||
import boto3
|
||||
from sorl import thumbnail
|
||||
from sorl.thumbnail.fields import ImageField as SORLImageField
|
||||
|
||||
|
|
@ -51,6 +55,20 @@ class Image(BaseAttributes, SORLImageMixin, PlatformMixin):
|
|||
"""String representation"""
|
||||
return f'{self.id}'
|
||||
|
||||
def set_pubic(self, is_public=True):
|
||||
if not settings.AWS_STORAGE_BUCKET_NAME:
|
||||
"""Backend doesn't use aws s3"""
|
||||
return
|
||||
s3 = boto3.resource('s3', region_name=settings.AWS_S3_REGION_NAME, aws_access_key_id=settings.AWS_ACCESS_KEY_ID,
|
||||
aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY)
|
||||
bucket = s3.Bucket(settings.AWS_STORAGE_BUCKET_NAME)
|
||||
if self.image:
|
||||
file_object = bucket.Object(f'{PublicMediaStorage.location}/{str(self.image.file)}')
|
||||
if is_public:
|
||||
file_object.Acl().put(ACL='public-read')
|
||||
else:
|
||||
file_object.Acl().put(ACL='authenticated-read')
|
||||
|
||||
@property
|
||||
def type(self) -> str:
|
||||
if self.image:
|
||||
|
|
@ -62,8 +80,8 @@ class Image(BaseAttributes, SORLImageMixin, PlatformMixin):
|
|||
@property
|
||||
def image_size_in_KB(self):
|
||||
try:
|
||||
return self.image.size / 1024 if self.image else None
|
||||
except FileNotFoundError:
|
||||
return self.image.size / 1000 if self.image else None
|
||||
except (FileNotFoundError, ClientError):
|
||||
return None
|
||||
|
||||
def delete_image(self, completely: bool = True):
|
||||
|
|
|
|||
|
|
@ -89,6 +89,12 @@ class EstablishmentGallerySerializer(serializers.ModelSerializer):
|
|||
instance.save()
|
||||
return instance
|
||||
|
||||
def update(self, instance: models.Image, validated_data):
|
||||
if instance.is_public != validated_data.get('is_public'):
|
||||
instance.set_pubic(validated_data.get('is_public', True))
|
||||
return super().update(instance, validated_data)
|
||||
|
||||
|
||||
|
||||
class CropImageSerializer(ImageSerializer):
|
||||
"""Serializers for image crops."""
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ urlpatterns = [
|
|||
path('', views.ImageListCreateView.as_view(), name='list-create'),
|
||||
path('for_establishment/<int:establishment_id>/', views.MediaForEstablishmentView.as_view(),
|
||||
name='establishment-media'),
|
||||
path('media/<int:pk>/', views.MediaUpdateView.as_view(), name='media-update'),
|
||||
path('<int:pk>/', views.ImageRetrieveDestroyView.as_view(), name='retrieve-destroy'),
|
||||
path('<int:pk>/crop/', views.CropImageCreateView.as_view(), name='create-crop'),
|
||||
]
|
||||
|
|
|
|||
|
|
@ -8,7 +8,6 @@ from utils.permissions import IsContentPageManager, IsCountryAdmin, IsEstablishm
|
|||
IsProducerFoodInspector, IsEstablishmentAdministrator
|
||||
from . import tasks, models, serializers
|
||||
|
||||
|
||||
class ImageBaseView(generics.GenericAPIView):
|
||||
"""Base Image view."""
|
||||
model = models.Image
|
||||
|
|
@ -34,6 +33,12 @@ class MediaForEstablishmentView(ImageBaseView, generics.ListCreateAPIView):
|
|||
.order_by('-order').prefetch_related('created_by')
|
||||
|
||||
|
||||
class MediaUpdateView(ImageBaseView, generics.UpdateAPIView):
|
||||
"""View for updating media data"""
|
||||
serializer_class = serializers.EstablishmentGallerySerializer
|
||||
permission_classes = ()
|
||||
|
||||
|
||||
class ImageRetrieveDestroyView(ImageBaseView, generics.RetrieveDestroyAPIView):
|
||||
"""Destroy view for model Image"""
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user