Files
SMANAGE/_api_backup/TempLateAPI/GERP/API/FormAPI/CodeDisplay.api
T
2026-06-16 11:55:14 +08:00

95 lines
3.0 KiB
Plaintext

from PDAPI import *
import TempLateAPI.GERP.API.FormAPI.FormData as FormData
from Utils.Models import GERPUser, GERPRole, GERPPermission, GERPDept
_cache = {}
def _get_display_map(table, model, uuid_col, name_field):
result = {}
items = db.session.query(model).all()
for item in items:
try:
if isinstance(uuid_col, str):
uuid_bytes = getattr(item, uuid_col, None)
else:
uuid_bytes = uuid_col(item)
uuid_str = str(uuid.UUID(bytes=uuid_bytes)) if isinstance(uuid_bytes, bytes) else str(uuid_bytes)
name = getattr(item, name_field, None)
if name:
result[uuid_str] = name
except:
pass
return result
def _ensure_cache():
global _cache
if _cache:
return
_cache["User"] = _get_display_map("User", GERPUser, "UserUUID", "UserName")
_cache["Role"] = _get_display_map("Role", GERPRole, "RoleUUID", "RoleName")
_cache["Permission"] = _get_display_map("Permission", GERPPermission, "PermUUID", "PermName")
_cache["Dept"] = _get_display_map("Dept", GERPDept, "DeptUUID", "DeptName")
__metadata__ = {
"route": "{DEFAULT_ROUTE}",
"methods": ["GET"],
"blueprint": "GERP",
"permission": 1.0,
"attributes": [],
}
def HandleRequest(_CleanedData):
_ensure_cache()
table_configs = FormData.TABLE_CODE_DISPLAY
translations = {
"zh_CN": {},
"en_US": {}
}
for table_name, fields in table_configs.items():
for field_name, config in fields.items():
key_prefix = f"{table_name}.{field_name}"
value_field = config.get("valueField", "id")
label_field = config.get("labelField", "name")
ref_table = None
ref_map = None
if value_field == "UserUUID" or field_name == "UserUUID":
ref_table = "User"
elif value_field == "RoleUUID" or field_name == "RoleUUID":
ref_table = "Role"
elif value_field == "PermUUID" or field_name == "PermUUID":
ref_table = "Permission"
elif value_field == "DeptUUID" or field_name == "DeptUUID":
ref_table = "Dept"
if ref_table and ref_table in _cache:
ref_map = _cache[ref_table]
if ref_map:
for uuid_val, name in ref_map.items():
translations["zh_CN"][f"{key_prefix}.{uuid_val}"] = name
translations["en_US"][f"{key_prefix}.{uuid_val}"] = name
enum_labels = FormData.TABLE_ENUM_LABELS
enum_translations = {}
for table_name, fields in enum_labels.items():
for field_name, value_map in fields.items():
key_prefix = f"{table_name}.{field_name}"
for val, label in value_map.items():
enum_translations[f"{key_prefix}.{val}"] = label
return {
"code": 0,
"translations": translations,
"cache": _cache,
"enumLabels": enum_translations,
}, 200