forked from erp-dev/erp
feat: refactor flower.utils to sub package, added some sync api in package, added plate_order sync result model but relation of plate_order was not query (do it next step)
This commit is contained in:
@@ -175,7 +175,40 @@ def advance_to_next_state(business_object: 'models.BusinessObject', user, **para
|
||||
- state_log: 创建的状态日志(成功时)
|
||||
"""
|
||||
# 业务约束:BusinessObject 必须能追溯到真实业务对象,否则状态流转记录没有业务意义。
|
||||
# DB 允许为空仅为历史兼容;但新增数据一律禁止。
|
||||
# DB 允许为空仅为历史兼容;但实际运行中存在“先创建 BO、后绑定到业务模型”的非标准路径。
|
||||
# 为避免这类数据导致推进失败,这里做一次自愈:
|
||||
# - 若 BO 未绑定 content_type/object_id(或 content_object 解析不到实例)
|
||||
# - 则尝试通过常见的一对一反向关系(如 printing_job/plate_order)推断真实业务对象并补齐绑定
|
||||
if business_object.content_type_id is None or business_object.object_id is None or business_object.content_object is None:
|
||||
inferred_instance = None
|
||||
inferred_default_name = None
|
||||
|
||||
# PrintingJob.business_object -> related_name='printing_job'
|
||||
try:
|
||||
inferred_instance = getattr(business_object, 'printing_job', None)
|
||||
if inferred_instance is not None:
|
||||
inferred_default_name = f"PrintingJob-{getattr(inferred_instance, 'pk', '')}"
|
||||
except (ObjectDoesNotExist, AttributeError):
|
||||
inferred_instance = None
|
||||
|
||||
# PlateOrder.business_object -> related_name='plate_order'
|
||||
if inferred_instance is None:
|
||||
try:
|
||||
inferred_instance = getattr(business_object, 'plate_order', None)
|
||||
if inferred_instance is not None:
|
||||
inferred_default_name = f"PlateOrder-{getattr(inferred_instance, 'pk', '')}"
|
||||
except (ObjectDoesNotExist, AttributeError):
|
||||
inferred_instance = None
|
||||
|
||||
if inferred_instance is not None:
|
||||
ensure_business_object_bound_to_instance(
|
||||
business_object,
|
||||
inferred_instance,
|
||||
default_name=inferred_default_name,
|
||||
)
|
||||
# GenericForeignKey 可能缓存了旧值,刷新以确保后续 content_object 判断正确
|
||||
business_object.refresh_from_db(fields=['content_type', 'object_id', 'name'])
|
||||
|
||||
if business_object.content_type_id is None or business_object.object_id is None:
|
||||
return False, "业务对象未绑定关联对象(content_type/object_id),禁止推进", None
|
||||
if business_object.content_object is None:
|
||||
|
||||
Reference in New Issue
Block a user