From 73adcce0bdced64042eab376b547b5ae1682deb1 Mon Sep 17 00:00:00 2001 From: Mike Hendricks Date: Fri, 10 Jan 2025 13:54:53 -0800 Subject: [PATCH] list_entities can now be statically sorted by a field --- README.md | 1 + example/config_example.yaml | 15 +++++++++++ src/sg_archive/html/main.py | 26 ++++++++++++++++--- .../html/templates/list_entities.html | 2 +- 4 files changed, 39 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index a73de32..81db007 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,7 @@ and points to the secret file. Here is a list of handled keys: - `html`: Used to configure the "SG at home" website. - `html:exclude_details`: Don't show these fields in a entity details webpage. Respects a special `global` entity type that is are applied to all entity_types if it exists. - `html:list_fields`: Only show these fields in the entity list view in this order. The current list html isn't optimized to show extremely large data sets so showing too many fields in the list view may cause the browser tab to crash. +- `html:sort_field`: Control the order entities are shown in list views. Passed to the `sg.find` command as the order argument. Sorting is static and the user can't change it. In the html a sort direction icon is shown if you only use a single sort rule. Note: Currently this requires modifying [shotgun_api3's mockgun](https://github.com/blurstudio-forks/python-api/pull/1). To configure the cli you need to pass the `--config` path pointing to your copy of `config_example.yml`. diff --git a/example/config_example.yaml b/example/config_example.yaml index da4e035..db7f621 100644 --- a/example/config_example.yaml +++ b/example/config_example.yaml @@ -123,6 +123,21 @@ html: - id - sg_version_type + sort_field: + # Sort entities in the list view. The value is passed as order to the + # shotgun_api3 `find` method. + Asset: + - field_name: code + direction: asc + Note: + - field_name: subject + direction: asc + - field_name: content + direction: asc + Version: + - field_name: code + direction: asc + # Filters can be used to define shotgun_api3 filters for each entity. # https://developers.shotgridsoftware.com/python-api/reference.html#shotgun_api3.shotgun.Shotgun.find # These are stored per entity_type diff --git a/src/sg_archive/html/main.py b/src/sg_archive/html/main.py index 1ca5539..eef974a 100644 --- a/src/sg_archive/html/main.py +++ b/src/sg_archive/html/main.py @@ -44,7 +44,7 @@ def load_entity_type(entity_type, force=False): loaded_entity_types.add(entity_type) -def sg_find(entity_type, keys=None, query=None, fields=None): +def sg_find(entity_type, keys=None, query=None, fields=None, order=None): # Call out to sg api against your local files if fields is None: fields = sg.field_names_for_entity_type(entity_type) @@ -59,6 +59,7 @@ def sg_find(entity_type, keys=None, query=None, fields=None): entity_type, query, sg.field_names_for_entity_type(entity_type), + order=order, ), fields, ) @@ -91,13 +92,26 @@ def field_data_type(self, entity_type, field): .get("value", field) ) - def field_name(self, entity_type, field): - return ( + def field_order(self, field, order): + if not order or len(order) != 1: + # Don't show the order if not defined or its a complex order + return "" + + if order and field == order[0].get("field_name"): + # 25B2: up arrow, 25BC: down arrow + arrow = "\u25B2" if order[0].get("direction") == "asc" else "\u25BC" + return f" {arrow}" + return "" + + def field_name(self, entity_type, field, order=None): + name = ( filtered_schema.get(entity_type, {}) .get(field, {}) .get("name", {}) .get("value", field) ) + order_text = self.field_order(field, order) + return name + order_text def fmt_sg_value(self, entity, field): if isinstance(entity, int): @@ -167,6 +181,8 @@ def sg_request_query(self, entity_type: str): # If the entity doesn't have this field, ignore it in selection continue if key == "project": + if not value: + continue value = int(value) query.append(["project", "is", {"type": "Project", "id": value}]) load_entity_type("Project") @@ -240,8 +256,9 @@ async def details_entity(request: Request, entity_type: str, key: int): @app.get("/list_entities/{entity_type}", response_class=HTMLResponse) async def list_entities(request: Request, entity_type: str): helper = Helper(request) + order = html_cfg.get("sort_field", {}).get(entity_type, None) query = helper.sg_request_query(entity_type) - entities, fields = sg_find(entity_type, query=query) + entities, fields = sg_find(entity_type, query=query, order=order) show_fields = helper.list_fields(entity_type) return templates.TemplateResponse( "list_entities.html", @@ -251,6 +268,7 @@ async def list_entities(request: Request, entity_type: str): "fields": show_fields, "entity_type": entity_type, "helper": helper, + "order": order, }, ) diff --git a/src/sg_archive/html/templates/list_entities.html b/src/sg_archive/html/templates/list_entities.html index 207e6e5..00fe174 100644 --- a/src/sg_archive/html/templates/list_entities.html +++ b/src/sg_archive/html/templates/list_entities.html @@ -14,7 +14,7 @@

{{ entities|length }} {{ entity_type }}'s

Details {%- for field in fields %} -
{{ helper.field_name(entity_type, field) }} +
{{ helper.field_name(entity_type, field, order) }} {{ field }}