{{breadcrumbs}}
HUB / lib_cms_taxonomy.py
lib_cms_taxonomy.py
- Runtime
- Python
- Category
- CMS
- Path
- /storage/emulated/0/Projects/Management/Libraries/py/CMS/lib_cms_taxonomy.py
FILE // lib_cms_taxonomy.py
"""
Library: lib_cms_taxonomy.py
MFDB Version: 1.3.1
Format_Creator: Elton Boehnen
Status: OFFICIAL - v1.3.1
Date: 2026-05-06
"""
"""
Library: lib_cms_taxonomy.py
Family: CMS
Jurisdiction: ["PYTHON", "BEJSON_LIBRARIES"]
Status: OFFICIAL — BEJSON/Lib (v1.4)
Author: Elton Boehnen
Version: 1.3 OFFICIAL
Date: 2026-05-01
Description: Backend library for managing CMS categories, tags, and authors.
Part of the Modular CMS Backend Framework.
"""
import os
import sys
import re
from typing import List, Dict, Optional
# Ensure local directory is in path for relative imports
LIB_DIR = os.path.dirname(os.path.abspath(__file__))
if LIB_DIR not in sys.path:
sys.path.append(LIB_DIR)
import lib_bejson_core as BEJSONCore
def _slugify(text: str) -> str:
"""Helper to convert text to a URL-safe slug."""
text = text.lower()
text = re.sub(r'[^a-z0-9]+', '-', text)
return text.strip('-')
# ---------------------------------------------------------------------------
# CATEGORY OPERATIONS
# ---------------------------------------------------------------------------
def cms_taxonomy_get_categories(db_path: str) -> List[Dict]:
"""
List all categories from the master database.
"""
doc = BEJSONCore.bejson_core_load_file(db_path)
records = BEJSONCore.bejson_core_get_records_by_type(doc, "Category")
fields = BEJSONCore.bejson_core_get_fields(doc)
cat_name_idx = BEJSONCore.bejson_core_get_field_index(doc, "category_name")
cat_slug_idx = BEJSONCore.bejson_core_get_field_index(doc, "category_slug")
categories = []
for row in records:
categories.append({
"name": row[cat_name_idx],
"slug": row[cat_slug_idx]
})
return categories
def cms_taxonomy_add_category(db_path: str, name: str, slug: Optional[str] = None) -> None:
"""
Add a new category to the database.
"""
if not slug:
slug = _slugify(name)
doc = BEJSONCore.bejson_core_load_file(db_path)
# Check if exists
cats = cms_taxonomy_get_categories(db_path)
if any(c['slug'] == slug for c in cats):
return # Already exists
cat_name_idx = BEJSONCore.bejson_core_get_field_index(doc, "category_name")
cat_slug_idx = BEJSONCore.bejson_core_get_field_index(doc, "category_slug")
t_idx = 0
field_count = len(doc["Fields"])
new_row = [None] * field_count
new_row[t_idx] = "Category"
new_row[cat_name_idx] = name
new_row[cat_slug_idx] = slug
doc = BEJSONCore.bejson_core_add_record(doc, new_row)
BEJSONCore.bejson_core_atomic_write(db_path, doc)
def cms_taxonomy_delete_category(db_path: str, slug: str) -> bool:
"""
Delete a category by slug.
"""
doc = BEJSONCore.bejson_core_load_file(db_path)
t_idx = 0
slug_idx = BEJSONCore.bejson_core_get_field_index(doc, "category_slug")
new_values = []
found = False
for row in doc["Values"]:
if row[t_idx] == "Category" and row[slug_idx] == slug:
found = True
continue
new_values.append(row)
if found:
doc["Values"] = new_values
BEJSONCore.bejson_core_atomic_write(db_path, doc)
return found
# ---------------------------------------------------------------------------
# AUTHOR OPERATIONS
# ---------------------------------------------------------------------------
def cms_taxonomy_get_authors(db_path: str) -> List[Dict]:
"""
List all authors from the master database.
"""
doc = BEJSONCore.bejson_core_load_file(db_path)
records = BEJSONCore.bejson_core_get_records_by_type(doc, "Author")
name_idx = BEJSONCore.bejson_core_get_field_index(doc, "author_name")
bio_idx = BEJSONCore.bejson_core_get_field_index(doc, "author_bio")
img_idx = BEJSONCore.bejson_core_get_field_index(doc, "author_image")
authors = []
for row in records:
authors.append({
"name": row[name_idx],
"bio": row[bio_idx],
"image": row[img_idx]
})
return authors
def cms_taxonomy_add_author(db_path: str, name: str, bio: str = "", image: str = "") -> None:
"""
Add a new author to the database.
"""
doc = BEJSONCore.bejson_core_load_file(db_path)
name_idx = BEJSONCore.bejson_core_get_field_index(doc, "author_name")
bio_idx = BEJSONCore.bejson_core_get_field_index(doc, "author_bio")
img_idx = BEJSONCore.bejson_core_get_field_index(doc, "author_image")
t_idx = 0
field_count = len(doc["Fields"])
new_row = [None] * field_count
new_row[t_idx] = "Author"
new_row[name_idx] = name
new_row[bio_idx] = bio
new_row[img_idx] = image
doc = BEJSONCore.bejson_core_add_record(doc, new_row)
BEJSONCore.bejson_core_atomic_write(db_path, doc)