fix: resolve subcategory slugs in category lookup and listing
The /categories.json endpoint only returns top-level categories by default, so subcategory slugs (e.g. vp-techops) failed to resolve and the 'categories' listing hid the entire subcategory tree. Both _resolve_category and cmd_categories now pass include_subcategories=true and traverse the nested subcategory_list.
This commit is contained in:
@@ -121,17 +121,22 @@ def cmd_whoami(args):
|
|||||||
|
|
||||||
def cmd_categories(args):
|
def cmd_categories(args):
|
||||||
api = _client()
|
api = _client()
|
||||||
data = api.get("/categories.json")
|
data = api.get("/categories.json", params={"include_subcategories": "true"})
|
||||||
cats = data.get("category_list", {}).get("categories", [])
|
cats = data.get("category_list", {}).get("categories", [])
|
||||||
print(f"{'ID':<6} {'SLUG':<28} {'TOPICS':>7} NAME")
|
print(f"{'ID':<6} {'SLUG':<36} {'TOPICS':>7} NAME")
|
||||||
print("-" * 80)
|
print("-" * 90)
|
||||||
|
count = 0
|
||||||
for c in cats:
|
for c in cats:
|
||||||
topic_count = c.get("topic_count", 0)
|
topic_count = c.get("topic_count", 0)
|
||||||
print(f"{c.get('id','?'):<6} {str(c.get('slug','')):<28} {topic_count:>6} {c.get('name','')}")
|
print(f"{c.get('id','?'):<6} {str(c.get('slug','')):<36} {topic_count:>6} {c.get('name','')}")
|
||||||
subcats = []
|
count += 1
|
||||||
for c in cats:
|
for s in c.get("subcategory_list") or []:
|
||||||
subcats.extend(c.get("subcategory_ids", []) or [])
|
sub_count = s.get("topic_count", 0)
|
||||||
print(f"\n{len(cats)} category(ies)")
|
slug = " " + str(s.get("slug", ""))
|
||||||
|
name = " " + s.get("name", "")
|
||||||
|
print(f"{s.get('id','?'):<6} {slug:<36} {sub_count:>6} {name}")
|
||||||
|
count += 1
|
||||||
|
print(f"\n{count} category(ies)")
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
@@ -314,7 +319,7 @@ def _resolve_category(api, ref):
|
|||||||
"""Resolve a category id, slug, or name to a numeric id."""
|
"""Resolve a category id, slug, or name to a numeric id."""
|
||||||
if str(ref).isdigit():
|
if str(ref).isdigit():
|
||||||
return int(ref)
|
return int(ref)
|
||||||
data = api.get("/categories.json")
|
data = api.get("/categories.json", params={"include_subcategories": "true"})
|
||||||
cats = data.get("category_list", {}).get("categories", [])
|
cats = data.get("category_list", {}).get("categories", [])
|
||||||
needle = str(ref).strip().lower()
|
needle = str(ref).strip().lower()
|
||||||
for c in cats:
|
for c in cats:
|
||||||
@@ -322,6 +327,11 @@ def _resolve_category(api, ref):
|
|||||||
return c["id"]
|
return c["id"]
|
||||||
if str(c.get("name", "")).lower() == needle:
|
if str(c.get("name", "")).lower() == needle:
|
||||||
return c["id"]
|
return c["id"]
|
||||||
|
for s in c.get("subcategory_list") or []:
|
||||||
|
if str(s.get("slug", "")).lower() == needle:
|
||||||
|
return s["id"]
|
||||||
|
if str(s.get("name", "")).lower() == needle:
|
||||||
|
return s["id"]
|
||||||
_err(f"unknown category '{ref}'. Run 'categories' to list valid slugs/names.")
|
_err(f"unknown category '{ref}'. Run 'categories' to list valid slugs/names.")
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user