Skip to content

Commit

Permalink
📙 page_size_options (#156)
Browse files Browse the repository at this point in the history
* Adds page_size_options to views

* Limit max to 10k rows

* Renames base view to ChowdaModelView

* Allow viewing all on eveything except MediaFiles, SonyCiAssets

* Lazy loads images
  • Loading branch information
mrharpo authored Sep 19, 2023
1 parent dbbc063 commit f8752bd
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
4 changes: 2 additions & 2 deletions chowda/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ class SonyCiAssetThumbnail(BaseField):
label: str = 'Thumbnail'
display_template: str = 'displays/sony_ci_asset_thumbnail.html'
read_only: bool = True
exclude_from_create: bool = True
exclude_from_edit: 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.get('standard')
return obj.thumbnails_by_type.get('small')


@dataclass
Expand Down
22 changes: 16 additions & 6 deletions chowda/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,15 @@
from chowda.utils import validate_media_file_guids


class BaseModelView(ModelView):
class ChowdaModelView(ModelView):
"""Base permissions for all views"""

page_size_options: ClassVar[list[int]] = [10, 25, 100, 1000, -1]


class ClammerModelView(ChowdaModelView):
"""Base Clammer permissions for all protected views"""

def can_create(self, request: Request) -> bool:
return get_user(request).is_clammer

Expand All @@ -43,7 +49,7 @@ def can_edit(self, request: Request) -> bool:
return get_user(request).is_clammer


class AdminModelView(ModelView):
class AdminModelView(ClammerModelView):
"""Base Admin permissions for all protected views"""

def is_accessible(self, request: Request) -> bool:
Expand All @@ -60,7 +66,7 @@ def can_edit(self, request: Request) -> bool:
return get_user(request).is_admin


class CollectionView(BaseModelView):
class CollectionView(ClammerModelView):
exclude_fields_from_list: ClassVar[list[Any]] = [Collection.media_files]
exclude_fields_from_detail: ClassVar[list[Any]] = [Collection.id]

Expand Down Expand Up @@ -152,7 +158,7 @@ async def create_multiple_batches(self, request: Request, pks: List[Any]) -> str
return f'Created Batches from {", ".join(names)}'


class BatchView(BaseModelView):
class BatchView(ClammerModelView):
exclude_fields_from_create: ClassVar[list[Any]] = [Batch.id]
exclude_fields_from_edit: ClassVar[list[Any]] = [Batch.id]
exclude_fields_from_list: ClassVar[list[Any]] = [Batch.media_files]
Expand Down Expand Up @@ -283,8 +289,9 @@ async def combine_batches(self, request: Request, pks: List[Any]) -> str:
return f'Combined {len(pks)} Batch(es)'


class MediaFileView(BaseModelView):
class MediaFileView(ClammerModelView):
pk_attr: str = 'guid'

actions: ClassVar[List[str]] = ['create_new_batch']

fields: ClassVar[list[str]] = [
Expand All @@ -295,6 +302,7 @@ class MediaFileView(BaseModelView):
'mmif_json',
]
exclude_fields_from_list: ClassVar[list[str]] = ['mmif_json']
page_size_options: ClassVar[list[int]] = [10, 25, 100, 500, 2000, 10000]

def can_create(self, request: Request) -> bool:
return get_user(request).is_admin
Expand Down Expand Up @@ -337,7 +345,7 @@ class UserView(AdminModelView):
fields: ClassVar[list[Any]] = ['first_name', 'last_name', 'email']


class ClamsAppView(BaseModelView):
class ClamsAppView(ClammerModelView):
fields: ClassVar[list[Any]] = ['name', 'endpoint', 'description', 'pipelines']


Expand Down Expand Up @@ -387,6 +395,8 @@ class SonyCiAssetView(AdminModelView):
'media_files',
]

page_size_options: ClassVar[list[int]] = [10, 25, 100, 500, 2000, 10000]

def can_create(self, request: Request) -> bool:
"""Sony Ci Assets are ingested from Sony Ci API, not created from the UI."""
return False
Expand Down
4 changes: 2 additions & 2 deletions static/js/custom_render.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Object.assign(render, {
media_file_guid_links: function render(data, type, full, meta, fieldOptions) {
// Render a list of media files to a string of links to media files
return data.map(
(guid) => `<a href="../media-file/detail/${guid}"> ${guid} </a>`
guid => `<a href="../media-file/detail/${guid}"> ${guid} </a>`
)
},
media_file_count: function render(data, type, full, meta, fieldOptions) {
Expand All @@ -18,7 +18,7 @@ Object.assign(render, {
fieldOptions
) {
return data
? `<img src="${data.location}" style="max-height:150px;">`
? `<img src="${data.location}" style="max-height:150px;" loading="lazy">`
: null
},
})

0 comments on commit f8752bd

Please sign in to comment.