From b14a5e792666b01c4f456e4cbfd43743bfeb0c36 Mon Sep 17 00:00:00 2001 From: Andrew Myers Date: Mon, 28 Aug 2023 14:57:15 -0400 Subject: [PATCH] Displays thumbnails for Sony Ci Asset models * Adds custom field for displaying thumbnails in Sony Ci Asset detail view. * Adds method 'thumbnails_by_type' to Sony Ci Asset model, which organizes thumbnails into a dict keyed by 'type' property. * Adds custom render JS function for displaying thumbnails in Sony Ci Asset list view using Datatables library. --- chowda/fields.py | 18 +++++++++++++++++- chowda/models.py | 4 ++++ chowda/views.py | 4 ++-- static/js/custom_render.js | 4 ++++ .../displays/sony_ci_asset_thumbnail.html | 1 + 5 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 templates/displays/sony_ci_asset_thumbnail.html diff --git a/chowda/fields.py b/chowda/fields.py index 1442a9f9..70a71afe 100644 --- a/chowda/fields.py +++ b/chowda/fields.py @@ -4,7 +4,7 @@ from starlette.datastructures import FormData from starlette.requests import Request from starlette_admin._types import RequestAction -from starlette_admin.fields import IntegerField, TextAreaField +from starlette_admin.fields import BaseField, IntegerField, TextAreaField @dataclass @@ -39,3 +39,19 @@ class MediaFileCount(IntegerField): async def parse_obj(self, request: Request, obj: Any) -> Any: return len(obj.media_files) + + +@dataclass +class SonyCiAssetThumbnail(BaseField): + """A the thumbnails for a SonyCiAsset mdoel""" + + name: str = 'sony_ci_assest_thumbnail' + label: str = 'Thumbnail' + display_template: str = 'displays/sony_ci_asset_thumbnail.html' + read_only: bool = True + exclude_from_create: bool = True + + render_function_key: str = 'sony_ci_asset_thumbnail' + + async def parse_obj(self, request: Request, obj: Any) -> Any: + return obj.thumbnails_by_type['standard'] diff --git a/chowda/models.py b/chowda/models.py index c19bc358..b029866e 100644 --- a/chowda/models.py +++ b/chowda/models.py @@ -145,6 +145,10 @@ class SonyCiAsset(SQLModel, table=True): back_populates='assets', link_model=MediaFileSonyCiAssetLink ) + @property + def thumbnails_by_type(self): + return {thumbnail['type']: thumbnail for thumbnail in self.thumbnails} + class Collection(SQLModel, table=True): __tablename__ = 'collections' diff --git a/chowda/views.py b/chowda/views.py index b26f2e91..2da07d36 100644 --- a/chowda/views.py +++ b/chowda/views.py @@ -15,7 +15,7 @@ from chowda.auth.utils import get_user from chowda.db import engine -from chowda.fields import MediaFileCount, MediaFilesGuidsField +from chowda.fields import MediaFileCount, MediaFilesGuidsField, SonyCiAssetThumbnail from chowda.models import Batch, Collection from chowda.utils import validate_media_file_guids @@ -342,11 +342,11 @@ async def render(self, request: Request, templates: Jinja2Templates) -> Response class SonyCiAssetView(AdminModelView): fields: ClassVar[list[Any]] = [ + SonyCiAssetThumbnail(), 'name', 'size', 'type', 'format', - 'thumbnails', 'media_files', ] diff --git a/static/js/custom_render.js b/static/js/custom_render.js index 78a75a8c..9219f790 100644 --- a/static/js/custom_render.js +++ b/static/js/custom_render.js @@ -9,4 +9,8 @@ Object.assign(render, { // Render a count of media files return data.length }, + + sony_ci_asset_thumbnail: function render(data, type, full, meta, fieldOptions) { + return `` + }, }) diff --git a/templates/displays/sony_ci_asset_thumbnail.html b/templates/displays/sony_ci_asset_thumbnail.html new file mode 100644 index 00000000..97cd636b --- /dev/null +++ b/templates/displays/sony_ci_asset_thumbnail.html @@ -0,0 +1 @@ + \ No newline at end of file