add category auto-linking with parent support
This commit is contained in:
parent
4d6785dc9f
commit
e75e2d0f75
@ -753,6 +753,7 @@ def get_or_create_category(category_name: str, category_pagetype: str, site: str
|
||||
"""查找或创建分类,返回分类记录的name,失败返回None
|
||||
统一使用title字段查找和创建分类
|
||||
如果提供了site,会在查找和创建时使用site字段过滤
|
||||
创建分类时会自动设置父分类为"Products"
|
||||
"""
|
||||
if not category_name or not category_pagetype:
|
||||
return None
|
||||
@ -766,10 +767,22 @@ def get_or_create_category(category_name: str, category_pagetype: str, site: str
|
||||
if existing:
|
||||
return existing[0].get("name")
|
||||
|
||||
# 不存在则创建(使用title字段,如果提供了site则设置site字段)
|
||||
# 不存在则创建
|
||||
category_data = {"title": category_name}
|
||||
if site:
|
||||
category_data["site"] = site
|
||||
|
||||
# 查找父分类"Products"并设置
|
||||
parent_filters = [["title", "=", "Products"]]
|
||||
if site:
|
||||
parent_filters.append(["site", "=", site])
|
||||
parent_categories = jingrow.get_list(category_pagetype, filters=parent_filters, limit=1)
|
||||
if parent_categories:
|
||||
parent_name = parent_categories[0].get("name")
|
||||
# 父分类字段名格式:parent_分类pagetype名称(小写下划线格式)
|
||||
parent_field = "parent_" + category_pagetype.lower().replace(" ", "_")
|
||||
category_data[parent_field] = parent_name
|
||||
|
||||
created = jingrow.create_pg(category_pagetype, category_data)
|
||||
if created:
|
||||
return created.get("name")
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user