def save_model(self, request, obj, form, change):
oapi = OfficialApi()
try:
rankedwars_data = oapi.get_info('torn', 'rankedwars', obj.id)
except ApiException as e:
self.message_user(request, f'网络错误,未收到api的返回数据,请重试:{obj.id}, error:{e}', messages.ERROR)
return False
# 检查是否有rw_id有效性
if rankedwars_data['rankedwars'].get(str(obj.id), None) is None:
logger.error(f'rw_id错误,请检查,rw_id:{obj.id}')
self.message_user(request, f'rw_id错误,请检查,rw_id:{obj.id}', messages.ERROR)
# todo 能成功阻止数据保存,但是前端页面任然提示保存成功,需要修复
return False
# 选择帮派和rw_id不匹配
if rankedwars_data['rankedwars'][str(obj.id)]['factions'].get(str(obj.faction_id.id), None) is None:
logger.error(f'选择帮派和rw_id不匹配,rw_id:{obj.id}')
self.message_user(request, f'选择帮派和rw_id不匹配,rw_id:{obj.id}', messages.ERROR)
return False
else:
super().save_model(request, obj, form, change)