优化
This commit is contained in:
parent
54ad21b952
commit
0d494d1976
@ -2191,60 +2191,83 @@ def add_to_public_pool(request):
|
||||
data = json.loads(request.body)
|
||||
|
||||
# 获取必要参数
|
||||
creator_id = data.get('creator_id')
|
||||
creator_ids = data.get('creator_ids') # 支持多个达人ID
|
||||
category = data.get('category')
|
||||
remark = data.get('remark')
|
||||
|
||||
if not creator_id:
|
||||
if not creator_ids:
|
||||
return JsonResponse({
|
||||
'code': 400,
|
||||
'message': '缺少必要参数: creator_id',
|
||||
'message': '缺少必要参数: creator_ids',
|
||||
'data': None
|
||||
}, json_dumps_params={'ensure_ascii': False})
|
||||
|
||||
# 查询达人信息
|
||||
try:
|
||||
creator = CreatorProfile.objects.get(id=creator_id)
|
||||
except CreatorProfile.DoesNotExist:
|
||||
return JsonResponse({
|
||||
'code': 404,
|
||||
'message': f'找不到ID为 {creator_id} 的达人',
|
||||
'data': None
|
||||
}, json_dumps_params={'ensure_ascii': False})
|
||||
result_data = []
|
||||
success_count = 0
|
||||
failed_count = 0
|
||||
failed_ids = []
|
||||
existed_creators = []
|
||||
|
||||
# 检查是否已存在于公有库
|
||||
exists = PublicCreatorPool.objects.filter(creator=creator).exists()
|
||||
if exists:
|
||||
# 如果已存在,则更新信息
|
||||
public_creator = PublicCreatorPool.objects.get(creator=creator)
|
||||
public_creator.category = category if category else public_creator.category
|
||||
public_creator.remark = remark if remark else public_creator.remark
|
||||
public_creator.save()
|
||||
for creator_id in creator_ids:
|
||||
# 查询达人信息
|
||||
try:
|
||||
creator = CreatorProfile.objects.get(id=creator_id)
|
||||
except CreatorProfile.DoesNotExist:
|
||||
failed_count += 1
|
||||
failed_ids.append(creator_id)
|
||||
continue
|
||||
|
||||
action = "更新"
|
||||
else:
|
||||
# 创建新的公有库达人
|
||||
public_creator = PublicCreatorPool.objects.create(
|
||||
creator=creator,
|
||||
category=category,
|
||||
remark=remark
|
||||
)
|
||||
# 检查是否已存在于公有库
|
||||
exists = PublicCreatorPool.objects.filter(creator=creator).exists()
|
||||
if exists:
|
||||
# 如果已存在,不进行更新,只记录已存在状态
|
||||
public_creator = PublicCreatorPool.objects.get(creator=creator)
|
||||
existed_creators.append({
|
||||
'id': creator.id,
|
||||
'name': creator.name
|
||||
})
|
||||
continue
|
||||
else:
|
||||
# 创建新的公有库达人
|
||||
public_creator = PublicCreatorPool.objects.create(
|
||||
creator=creator,
|
||||
category=category,
|
||||
remark=remark
|
||||
)
|
||||
|
||||
action = "添加"
|
||||
success_count += 1
|
||||
result_data.append({
|
||||
'creator': {
|
||||
'id': creator.id,
|
||||
'name': creator.name
|
||||
},
|
||||
'public_pool': {
|
||||
'id': public_creator.id,
|
||||
'category': public_creator.category,
|
||||
'remark': public_creator.remark
|
||||
}
|
||||
})
|
||||
|
||||
# 生成响应消息
|
||||
message_parts = []
|
||||
if success_count > 0:
|
||||
message_parts.append(f'成功添加{success_count}个达人到公有库')
|
||||
if len(existed_creators) > 0:
|
||||
message_parts.append(f'{len(existed_creators)}个达人已存在于公有库')
|
||||
if failed_count > 0:
|
||||
message_parts.append(f'{failed_count}个达人不存在')
|
||||
|
||||
message = ','.join(message_parts)
|
||||
|
||||
return JsonResponse({
|
||||
'code': 200,
|
||||
'message': f'成功{action}达人到公有库',
|
||||
'message': message,
|
||||
'data': {
|
||||
'creator': {
|
||||
'id': creator.id,
|
||||
'name': creator.name
|
||||
},
|
||||
'public_pool': {
|
||||
'id': public_creator.id,
|
||||
'category': public_creator.category,
|
||||
'remark': public_creator.remark
|
||||
}
|
||||
'success_count': success_count,
|
||||
'failed_count': failed_count,
|
||||
'failed_ids': failed_ids,
|
||||
'existed_creators': existed_creators,
|
||||
'results': result_data
|
||||
}
|
||||
}, json_dumps_params={'ensure_ascii': False})
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user