Skip to content

Commit

Permalink
Fix disorder when filtering multiple groups that having same order
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlatwe committed Jul 8, 2019
1 parent 98d2f85 commit 1af5e8d
Showing 1 changed file with 31 additions and 16 deletions.
47 changes: 31 additions & 16 deletions avalon/tools/cbloader/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,33 +125,48 @@ def get_active_group_config(asset_id, include_predefined=False):
for config in predefineds.values():
_orders.add(config["order"])

# Remap order to list index
orders = sorted(_orders)
order_temp = "%0{}d".format(len(str(len(orders))))

# Collect groups from subsets
active_groups = list()

existed = set(io.distinct("data.subsetGroup",
{"type": "subset", "parent": asset_id}))
group_names = set(io.distinct("data.subsetGroup",
{"type": "subset", "parent": asset_id}))
if include_predefined:
# Ensure all predefined group configs will be included
existed.update(predefineds.keys())
group_names.update(predefineds.keys())

groups = list()

for group_name in existed:
for name in group_names:
# Get group config
config = predefineds.get(group_name, default_group_config)
# Calculate order
config = predefineds.get(name, default_group_config)
# Base order
remapped_order = orders.index(config["order"])
inverse_order = len(orders) - remapped_order

data = {
"name": group_name,
"name": name,
"icon": config["icon"],
# Format orders into fixed length string for groups sorting
"order": order_temp % remapped_order,
"inverseOrder": order_temp % inverse_order,
"_order": remapped_order,
}

active_groups.append(data)
groups.append(data)

# Sort by tuple (base_order, name)
# If there are multiple groups in same order, will sorted by name.
ordered = sorted(groups, key=lambda dat: (dat.pop("_order"), dat["name"]))

total = len(ordered)
order_temp = "%0{}d".format(len(str(total)))

# Update sorted order to config
for index, data in enumerate(ordered):
order = index
inverse_order = total - order

data.update({
# Format orders into fixed length string for groups sorting
"order": order_temp % order,
"inverseOrder": order_temp % inverse_order,
})

return active_groups
return ordered

0 comments on commit 1af5e8d

Please sign in to comment.