Skip to content

Commit

Permalink
list_entities can now be statically sorted by a field
Browse files Browse the repository at this point in the history
  • Loading branch information
MHendricks committed Jan 10, 2025
1 parent 94edb6f commit 73adcce
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
15 changes: 15 additions & 0 deletions example/config_example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 22 additions & 4 deletions src/sg_archive/html/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
)
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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",
Expand All @@ -251,6 +268,7 @@ async def list_entities(request: Request, entity_type: str):
"fields": show_fields,
"entity_type": entity_type,
"helper": helper,
"order": order,
},
)

Expand Down
2 changes: 1 addition & 1 deletion src/sg_archive/html/templates/list_entities.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ <h1>{{ entities|length }} {{ entity_type }}'s</h1>
<th>Details</th>
{%- for field in fields %}
<th>
<div class="tooltip">{{ helper.field_name(entity_type, field) }}
<div class="tooltip">{{ helper.field_name(entity_type, field, order) }}
<span class="tooltiptext">{{ field }}</span>
</div>
</th>
Expand Down

0 comments on commit 73adcce

Please sign in to comment.