F(backend): add public media derivatives pipeline
This commit is contained in:
@@ -5,6 +5,7 @@ from typing import Optional
|
||||
|
||||
from apps.blog.api.schemas import AuthorSchema
|
||||
from apps.gallery.models import Gallery
|
||||
from core.media import BLUR_VARIANT, PREVIEW_VARIANT, derivative_url
|
||||
|
||||
|
||||
class GallerySchema(ModelSchema):
|
||||
@@ -12,12 +13,34 @@ class GallerySchema(ModelSchema):
|
||||
uploaded_by: AuthorSchema
|
||||
file_size_mb: float
|
||||
markdown_url: str
|
||||
absolute_image_url: Optional[str] = None
|
||||
absolute_image_preview_url: Optional[str] = None
|
||||
absolute_image_blur_url: Optional[str] = None
|
||||
|
||||
class Config:
|
||||
model = Gallery
|
||||
model_fields = ['id', 'title', 'description', 'image', 'alt_text',
|
||||
'width', 'height', 'is_public', 'created_at']
|
||||
|
||||
@staticmethod
|
||||
def resolve_absolute_image_url(obj, context):
|
||||
request = context["request"]
|
||||
if obj.image and hasattr(obj.image, "url"):
|
||||
return request.build_absolute_uri(obj.image.url)
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def resolve_absolute_image_preview_url(obj, context):
|
||||
request = context["request"]
|
||||
url = derivative_url(obj.image, PREVIEW_VARIANT)
|
||||
return request.build_absolute_uri(url) if url else None
|
||||
|
||||
@staticmethod
|
||||
def resolve_absolute_image_blur_url(obj, context):
|
||||
request = context["request"]
|
||||
url = derivative_url(obj.image, BLUR_VARIANT)
|
||||
return request.build_absolute_uri(url) if url else None
|
||||
|
||||
|
||||
class GalleryCreateSchema(Schema):
|
||||
"""Payload for creating a gallery entry."""
|
||||
|
||||
Reference in New Issue
Block a user