26 lines
1.2 KiB
Python
26 lines
1.2 KiB
Python
"""Display order for bindings. Stored positions stay intact when sorting by date."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
def arrange(items: list[dict], mode: str, direction: str) -> list[dict]:
|
|
"""Return a new list. Undated entries stay last in both date directions."""
|
|
if mode == "position":
|
|
ordered = sorted(items, key=lambda item: (item["_raw_position"], item["id"]))
|
|
else:
|
|
dated = [item for item in items if item.get("_document_on")]
|
|
undated = [item for item in items if not item.get("_document_on")]
|
|
if direction == "desc":
|
|
dated.sort(key=lambda item: (item["_raw_position"], item["id"]))
|
|
dated.sort(key=lambda item: item["_document_on"], reverse=True)
|
|
else:
|
|
dated.sort(key=lambda item: (item["_document_on"], item["_raw_position"], item["id"]))
|
|
undated.sort(key=lambda item: (item["_raw_position"], item["id"]))
|
|
ordered = dated + undated
|
|
result = []
|
|
for index, item in enumerate(ordered):
|
|
visible = {key: value for key, value in item.items() if not key.startswith("_")}
|
|
visible["position"] = index
|
|
result.append(visible)
|
|
return result
|