管理员删除公有达人

This commit is contained in:
jlj 2025-06-03 15:34:59 +08:00
parent 006bbb1fc8
commit dcab490420

View File

@ -2822,6 +2822,16 @@ def remove_creator_from_private_pool(request):
# 通过关联ID删除 - 需要验证每个关联的权限 # 通过关联ID删除 - 需要验证每个关联的权限
relations = PrivateCreatorRelation.objects.select_related('private_pool').filter(id__in=relation_ids) relations = PrivateCreatorRelation.objects.select_related('private_pool').filter(id__in=relation_ids)
# 检查是否找到要删除的记录
if not relations.exists():
return JsonResponse({
'code': 404,
'message': '未找到指定的私有库达人关联记录',
'data': {
'relation_ids': relation_ids
}
}, json_dumps_params={'ensure_ascii': False})
# 验证权限 # 验证权限
for relation in relations: for relation in relations:
if relation.private_pool.user_id != current_user.id: if relation.private_pool.user_id != current_user.id:
@ -2852,10 +2862,23 @@ def remove_creator_from_private_pool(request):
'data': None 'data': None
}, json_dumps_params={'ensure_ascii': False}) }, json_dumps_params={'ensure_ascii': False})
# 查询要删除的记录
query = PrivateCreatorRelation.objects.filter( query = PrivateCreatorRelation.objects.filter(
private_pool_id=pool_id, private_pool_id=pool_id,
creator_id__in=creator_ids creator_id__in=creator_ids
) )
# 检查是否找到要删除的记录
if not query.exists():
return JsonResponse({
'code': 404,
'message': '私有库中未找到指定的达人记录',
'data': {
'pool_id': pool_id,
'creator_ids': creator_ids
}
}, json_dumps_params={'ensure_ascii': False})
result_type = 'creator_ids' result_type = 'creator_ids'
result_value = creator_ids result_value = creator_ids
else: else:
@ -3298,11 +3321,20 @@ def filter_private_pool_creators(request):
@csrf_exempt @csrf_exempt
@require_http_methods(["POST"]) @require_http_methods(["POST"])
def remove_from_public_pool(request): def remove_from_public_pool(request):
"""从公有达人库中移除达人,同时更新私有库中相关记录的状态""" """从公有达人库中移除达人,同时删除所有用户私有库中的相关记录(仅管理员可操作)"""
try: try:
from .models import PublicCreatorPool, PrivateCreatorRelation, CreatorProfile from .models import PublicCreatorPool, PrivateCreatorRelation, CreatorProfile
import json import json
# 检查当前用户是否有管理员权限
current_user = request.user
if not current_user.is_staff and not current_user.is_superuser:
return JsonResponse({
'code': 403,
'message': '权限不足,只有管理员可以删除公有库达人',
'data': None
}, json_dumps_params={'ensure_ascii': False})
data = json.loads(request.body) data = json.loads(request.body)
# 获取必要参数 # 获取必要参数
@ -3335,18 +3367,17 @@ def remove_from_public_pool(request):
# 查找所有私有库中引用此达人且标记为从公有库添加的记录 # 查找所有私有库中引用此达人且标记为从公有库添加的记录
private_relations = PrivateCreatorRelation.objects.filter( private_relations = PrivateCreatorRelation.objects.filter(
creator=creator, creator=creator,
added_from_public=True, added_from_public=True
status='active' # 只更新活跃状态的记录
) )
# 更新私有库中相关记录的状态为"已失效" # 记录受影响的用户数量
updated_private_count = 0 affected_users = private_relations.values('private_pool__user_id').distinct().count()
# 删除私有库中相关记录
deleted_private_count = 0
if private_relations.exists(): if private_relations.exists():
# 为了兼容性可以选择添加新的状态或使用现有的archived状态 deleted_private_count = private_relations.count()
# 这里我们添加一个新的状态值来明确表示"公有库已移除" private_relations.delete()
updated_private_count = private_relations.update(
status='public_removed' # 新增状态:公有库已移除
)
# 删除公有库记录 # 删除公有库记录
public_creator.delete() public_creator.delete()
@ -3360,8 +3391,9 @@ def remove_from_public_pool(request):
'name': creator.name 'name': creator.name
}, },
'removed_from_public': True, 'removed_from_public': True,
'updated_private_relations': updated_private_count, 'deleted_private_relations': deleted_private_count,
'note': f'已更新 {updated_private_count} 个私有库中的相关记录状态' 'affected_users': affected_users,
'note': f'已删除 {deleted_private_count} 个私有库中的相关记录,影响了 {affected_users} 个用户'
} }
}, json_dumps_params={'ensure_ascii': False}) }, json_dumps_params={'ensure_ascii': False})
@ -3376,8 +3408,6 @@ def remove_from_public_pool(request):
}, json_dumps_params={'ensure_ascii': False}) }, json_dumps_params={'ensure_ascii': False})
@api_view(['GET']) @api_view(['GET'])
@authentication_classes([CustomTokenAuthentication]) @authentication_classes([CustomTokenAuthentication])
@csrf_exempt @csrf_exempt