35 lines
1.3 KiB
Python
35 lines
1.3 KiB
Python
#!/usr/bin/env python3
|
|
import json, time
|
|
|
|
result = {'widgettype': 'Message', 'options': {'title': 'Error', 'message': 'Invalid', 'type': 'error'}}
|
|
|
|
try:
|
|
dbname = get_module_dbname('product_management')
|
|
now = time.strftime('%Y-%m-%d %H:%M:%S')
|
|
|
|
data = dict(params_kw)
|
|
record_id = data.pop('id', None)
|
|
if not record_id:
|
|
raise ValueError('Missing id')
|
|
|
|
data['updated_at'] = now
|
|
|
|
# If category_id changed, update product_table_name from new category
|
|
if data.get('category_id') and not data.get('product_table_name'):
|
|
sql = """SELECT product_table_name FROM product_category
|
|
WHERE id = ${category_id}$ AND has_product='1'"""
|
|
async with DBPools().sqlorContext(dbname) as sor:
|
|
rows = await sor.sqlExe(sql, {'category_id': data['category_id']})
|
|
if rows:
|
|
data['product_table_name'] = rows[0]['product_table_name']
|
|
|
|
async with DBPools().sqlorContext(dbname) as sor:
|
|
await sor.U('product', data, {'id': record_id})
|
|
|
|
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)
|