This commit is contained in:
jlj 2025-06-10 16:53:50 +08:00
parent 54ad21b952
commit 0d494d1976

View File

@ -2191,37 +2191,42 @@ 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})
result_data = []
success_count = 0
failed_count = 0
failed_ids = []
existed_creators = []
for creator_id in creator_ids:
# 查询达人信息
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})
failed_count += 1
failed_ids.append(creator_id)
continue
# 检查是否已存在于公有库
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()
action = "更新"
existed_creators.append({
'id': creator.id,
'name': creator.name
})
continue
else:
# 创建新的公有库达人
public_creator = PublicCreatorPool.objects.create(
@ -2230,12 +2235,8 @@ def add_to_public_pool(request):
remark=remark
)
action = "添加"
return JsonResponse({
'code': 200,
'message': f'成功{action}达人到公有库',
'data': {
success_count += 1
result_data.append({
'creator': {
'id': creator.id,
'name': creator.name
@ -2245,6 +2246,28 @@ def add_to_public_pool(request):
'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': message,
'data': {
'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})