product_management/wwwroot/api/product_create.dspy
yumoqing 43787a63a4 fix: spec compliance pass - ServerEnv removal in .dspy, core.py DBPools singleton, scripts/load_path.py
- All .dspy files: replace ServerEnv() org_id access with await get_userorgid()
  (get_userorgid is registered as global in ahserver processorResource)
- core.py: simplify _get_db to use DBPools() singleton directly (DBPools is @SingletonDecorator)
  remove unnecessary db.databases = config.databases assignment
- core.py: add MODULE_NAME constant, use env.get_module_dbname(MODULE_NAME) pattern
- Create scripts/load_path.py with all RBAC paths per module-development-spec
  Covers: entry pages, feature .ui files, CRUD directories, all API .dspy endpoints
- .gitignore: add __pycache__/ exclusion
- All models/*.json and json/*.json pass spec validation checks
2026-05-25 17:03:08 +08:00

52 lines
1.8 KiB
Python

#!/usr/bin/env python3
import json, time
from appPublic.uniqueID import getID
result = {'widgettype': 'Message', 'options': {'title': 'Error', 'message': 'Invalid', 'type': 'error'}}
try:
user_id = await get_user()
org_id = (await get_userorgid()) or '0'
now = time.strftime('%Y-%m-%d %H:%M:%S')
dbname = get_module_dbname('product_management')
data = dict(params_kw)
data['id'] = getID()
data['org_id'] = org_id
data['created_by'] = user_id
data['created_at'] = now
data['updated_at'] = now
if 'status' not in data:
data['status'] = '1'
if 'sort_order' not in data:
data['sort_order'] = '0'
if 'price_type' not in data:
data['price_type'] = '1'
if 'price' not in data:
data['price'] = '0.00'
if 'currency' not in data:
data['currency'] = 'CNY'
# Verify category belongs to current org
if data.get('category_id'):
async with DBPools().sqlorContext(dbname) as sor:
cat_check = await sor.sqlExe(
"SELECT id, product_type FROM product_category WHERE id = ${category_id}$ AND org_id = ${org_id}$",
{'category_id': data['category_id'], 'org_id': org_id}
)
if not cat_check:
raise ValueError('类别不存在或不属于当前机构')
# Auto-fill product_type from category
if not data.get('product_type'):
data['product_type'] = cat_check[0].get('product_type', '')
async with DBPools().sqlorContext(dbname) as sor:
await sor.C('product', data)
result = {'widgettype': 'Message', 'options': {'title': 'Success', 'message': '产品创建成功', 'type': 'success'}}
except Exception as e:
result['options'] = {'title': 'Error', 'message': '创建失败: ' + str(e), 'type': 'error'}
return json.dumps(result, ensure_ascii=False)