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

View File

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