Skip to content

Commit

Permalink
Displays thumbnails for Sony Ci Asset models
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
afred committed Aug 28, 2023
1 parent 17ccdcf commit b14a5e7
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 3 deletions.
18 changes: 17 additions & 1 deletion chowda/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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']
4 changes: 4 additions & 0 deletions chowda/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
4 changes: 2 additions & 2 deletions chowda/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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',
]

Expand Down
4 changes: 4 additions & 0 deletions static/js/custom_render.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<img src="${data.location}" style="max-height:150px;">`
},
})
1 change: 1 addition & 0 deletions templates/displays/sony_ci_asset_thumbnail.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<img src="{{data['location']}}" style="max-height:150px;">

0 comments on commit b14a5e7

Please sign in to comment.