51 lines
1.6 KiB
Python
51 lines
1.6 KiB
Python
"""Schemas for gallery resources."""
|
|
|
|
from ninja import Schema, ModelSchema
|
|
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):
|
|
"""Serialized representation of a gallery image."""
|
|
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."""
|
|
title: str
|
|
description: Optional[str] = None
|
|
alt_text: Optional[str] = None
|
|
is_public: bool = True
|