修改老接口的适配

This commit is contained in:
Zixiao Wang 2025-06-11 15:53:02 +08:00
parent ad8676ea64
commit 21fc193a13

View File

@ -292,25 +292,160 @@ def get_match_list(request):
####### 老接口 @csrf_exempt # 仅在开发时使用,生产环境中请使用更安全的方式
def create_project(request):
project_name = request.POST.get('name')
username = request.POST.get('user')
task_type = request.POST.get('taskType')
description = request.POST.get('description')
try:
user = User.objects.get(username=username)
except User.DoesNotExist:
return JsonResponse({'success': False, 'message': '用户不存在'})
project = Project(name=project_name, user=user.username, task_type=task_type, description=description)
project.save()
return JsonResponse({'success': True, 'message': '项目已创建'})
def get_user_projects(request):
username = request.GET.get('user')
try:
user = User.objects.get(username=username)
except User.DoesNotExist:
return JsonResponse({'projects': []})
projects = Project.objects.filter(user=user.username)
return JsonResponse({'projects': list(projects.values())})
@csrf_exempt # 仅在开发时使用,生产环境中请使用更安全的方式
def delete_project(request):
username = request.GET.get('user')
project_name = request.GET.get('projectName')
try:
user = User.objects.get(username=username)
except User.DoesNotExist:
return JsonResponse({'success': False})
project_deleted, _ = Project.objects.filter(user=user.username, name=project_name).delete()
if project_deleted > 0:
return JsonResponse({'success': True})
else:
return JsonResponse({'success': False})
@csrf_exempt # 仅在开发时使用,生产环境中请使用更安全的方式
def delete_dataset(request):
username = request.GET.get('user')
dataset_name = request.GET.get('datasetName')
try:
user = User.objects.get(username=username)
except User.DoesNotExist:
return JsonResponse({'success': False})
project_deleted, _ = Dataset.objects.filter(user=user, name=dataset_name).delete()
delete_object_name = user.username + '/' + dataset_name
## 删除minio中的所有数据
future1 = executor.submit(delete_from_minio, delete_object_name)
## 删除临时文件夹的所有数据
curr_temp_dir = os.path.join(os.path.join(TEMP_ROOT_DIR, user.username), dataset_name)
future2 = executor.submit(delete_temp_dir, curr_temp_dir)
if project_deleted > 0:
return JsonResponse({'success': True})
else:
return JsonResponse({'success': False})
@csrf_exempt # 仅在开发时使用,生产环境中请使用更安全的方式
def get_project(request):
username = request.GET.get('user')
project_name = request.GET.get('projectName')
try:
user = User.objects.get(username=username)
except User.DoesNotExist:
return JsonResponse({'success': False, 'project': None})
project = Project.objects.filter(user=user.username, name=project_name)
if project.exists():
return JsonResponse({'success': True, 'project': list(project.values())[0]})
else:
return JsonResponse({'success': False, 'project': None})
@csrf_exempt # 仅在开发时使用,生产环境中请使用更安全的方式
def get_dataset(request):
username = request.GET.get('user')
dataset_name = request.GET.get('datasetName')
try:
user = User.objects.get(username=username)
except User.DoesNotExist:
return JsonResponse({'success': False, 'dataset': None})
dataset = Dataset.objects.filter(user=user, name=dataset_name)
if dataset.exists():
return JsonResponse({'success': True, 'dataset': list(dataset.values())[0]})
else:
return JsonResponse({'success': False, 'dataset': None})
@csrf_exempt # 仅在开发时使用,生产环境中请使用更安全的方式
def get_minio_links(request):
username = request.GET.get('user')
dataset_name = request.GET.get('datasetName')
next_image = request.GET.get('nextImage')
page_size = request.GET.get('pageSize')
if next_image != '':
next_image = os.path.relpath(next_image, f"http://{LOCAL_IP}:9000/{BUCKET_NAME}")
task_type = request.GET.get('taskType')
try:
user = User.objects.get(username=username)
except User.DoesNotExist:
return JsonResponse({'success': False, 'links': []})
## 生成object_name路径用于访问minio
object_name = user.username + '/' + dataset_name + '/' + task_type + '/images'
http_links = get_dataset_link(object_name, next_image, page_size)
return JsonResponse({'success': True, 'links': http_links})
@csrf_exempt
def get_dataset_is_upload(request):
if request.method == 'GET':
username = request.GET.get('user')
dataset_name = request.GET.get('datasetName')
# 检查用户和数据集名称是否提供
if not username or not dataset_name:
return JsonResponse({'success': False, 'message': '用户或数据集名称未提供'})
# 查询数据库获取数据集
try:
user = User.objects.get(username=username)
except User.DoesNotExist:
return JsonResponse({'success': False, 'message': '用户未找到'})
dataset = Dataset.objects.filter(user=user, name=dataset_name).first()
if dataset is not None:
return JsonResponse({'success': True, 'is_upload': dataset.is_upload})
else:
return JsonResponse({'success': False, 'message': '数据集未找到'})
return JsonResponse({'success': False, 'message': '请求方法错误'})
@csrf_exempt @csrf_exempt
def upload(request): def upload(request):
if request.method == 'POST': if request.method == 'POST':
# 检查文件是否存在 # 检查文件是否存在
if 'file' in request.FILES: if 'file' in request.FILES:
dataset_name = request.POST.get('name') dataset_name = request.POST.get('name')
user = request.POST.get('user') username = request.POST.get('user')
task_type = request.POST.get('taskType') task_type = request.POST.get('taskType')
size = request.POST.get('size') size = request.POST.get('size')
description = request.POST.get('description') description = request.POST.get('description')
uploaded_file = request.FILES.get('file') uploaded_file = request.FILES.get('file')
try:
user = User.objects.get(username=username)
except User.DoesNotExist:
return JsonResponse({'success': False, 'message': f'用户 {username} 不存在'})
## 检查数据库判断该数据集名称是否重复 ## 检查数据库判断该数据集名称是否重复
if Dataset.objects.filter(name=dataset_name, user=user).exists(): if Dataset.objects.filter(name=dataset_name, user=user).exists():
return JsonResponse({'success': False, 'message': f'数据集 {dataset_name} 名称已存在'}) return JsonResponse({'success': False, 'message': f'数据集 {dataset_name} 名称已存在'})
## 创建解压数据集的临时目录 ## 创建解压数据集的临时目录
temp_dir = TEMP_ROOT_DIR + '/' + user + '/' + dataset_name temp_dir = TEMP_ROOT_DIR + '/' + user.username + '/' + dataset_name
# 如果目录已存在且不为空,则删除该目录 # 如果目录已存在且不为空,则删除该目录
if os.path.exists(temp_dir) and os.listdir(temp_dir): if os.path.exists(temp_dir) and os.listdir(temp_dir):
@ -366,7 +501,7 @@ def upload(request):
print(f"数据集 {dataset_name} 结构检查失败") print(f"数据集 {dataset_name} 结构检查失败")
## 删除临时文件夹的所有数据 ## 删除临时文件夹的所有数据
curr_temp_dir = os.path.join(os.path.join(TEMP_ROOT_DIR, user), dataset_name) curr_temp_dir = os.path.join(os.path.join(TEMP_ROOT_DIR, user.username), dataset_name)
delete_temp_dir(curr_temp_dir) delete_temp_dir(curr_temp_dir)
return JsonResponse({'success': False, 'message': f'数据集 {dataset_name} 结构不符合要求: {message}'}) return JsonResponse({'success': False, 'message': f'数据集 {dataset_name} 结构不符合要求: {message}'})
@ -376,7 +511,7 @@ def upload(request):
user=user, # 这里需要替换为实际的用户信息 user=user, # 这里需要替换为实际的用户信息
task_type=task_type, task_type=task_type,
size=size, size=size,
number=0, # number=0,
description=description, description=description,
categories = categories categories = categories
) )
@ -392,139 +527,17 @@ def upload(request):
return JsonResponse({'success': False, 'message': '请求错误'}) return JsonResponse({'success': False, 'message': '请求错误'})
@csrf_exempt
def get_user_datasets(request): def get_user_datasets(request):
user = request.GET.get('user') username = request.GET.get('user')
if not username:
return JsonResponse({'code': 400, 'message': '缺少user参数', 'data': {}})
try:
user = User.objects.get(username=username)
except User.DoesNotExist:
return JsonResponse({'code': 400, 'message': '用户不存在', 'data': {}})
datasets = Dataset.objects.filter(user=user) datasets = Dataset.objects.filter(user=user)
return JsonResponse({'datasets': list(datasets.values())}) return JsonResponse({'datasets': list(datasets.values())})
@csrf_exempt # 仅在开发时使用,生产环境中请使用更安全的方式
def create_project(request):
project_name = request.POST.get('name')
user = request.POST.get('user')
task_type = request.POST.get('taskType')
description = request.POST.get('description')
project = Project(name=project_name, user=user, task_type=task_type, description=description)
project.save()
return JsonResponse({'success': True, 'message': '项目已创建'})
def get_user_projects(request):
user = request.GET.get('user')
projects = Project.objects.filter(user=user)
return JsonResponse({'projects': list(projects.values())})
@csrf_exempt # 仅在开发时使用,生产环境中请使用更安全的方式
def delete_project(request):
user = request.GET.get('user')
project_name = request.GET.get('projectName')
project_deleted, _ = Project.objects.filter(user=user, name=project_name).delete()
if project_deleted > 0:
return JsonResponse({'success': True})
else:
return JsonResponse({'success': False})
@csrf_exempt # 仅在开发时使用,生产环境中请使用更安全的方式
def delete_dataset(request):
user = request.GET.get('user')
dataset_name = request.GET.get('datasetName')
project_deleted, _ = Dataset.objects.filter(user=user, name=dataset_name).delete()
delete_object_name = user + '/' + dataset_name
## 删除minio中的所有数据
future1 = executor.submit(delete_from_minio, delete_object_name)
## 删除临时文件夹的所有数据
curr_temp_dir = os.path.join(os.path.join(TEMP_ROOT_DIR, user), dataset_name)
future2 = executor.submit(delete_temp_dir, curr_temp_dir)
if project_deleted > 0:
return JsonResponse({'success': True})
else:
return JsonResponse({'success': False})
@csrf_exempt # 仅在开发时使用,生产环境中请使用更安全的方式
def get_project(request):
user = request.GET.get('user')
project_name = request.GET.get('projectName')
project = Project.objects.filter(user=user, name=project_name)
if project is not None:
return JsonResponse({'success': True, 'project': list(project.values())[0]})
else:
return JsonResponse({'success': False, 'project': None})
@csrf_exempt # 仅在开发时使用,生产环境中请使用更安全的方式
def get_dataset(request):
user = request.GET.get('user')
dataset_name = request.GET.get('datasetName')
dataset = Dataset.objects.filter(user=user, name=dataset_name)
if dataset is not None:
return JsonResponse({'success': True, 'dataset': list(dataset.values())[0]})
else:
return JsonResponse({'success': False, 'dataset': None})
@csrf_exempt # 仅在开发时使用,生产环境中请使用更安全的方式
def get_minio_links(request):
user = request.GET.get('user')
dataset_name = request.GET.get('datasetName')
next_image = request.GET.get('nextImage')
page_size = request.GET.get('pageSize')
if next_image != '':
next_image = os.path.relpath(next_image, f"http://{LOCAL_IP}:9000/{BUCKET_NAME}")
task_type = request.GET.get('taskType')
## 生成object_name路径用于访问minio
object_name = user + '/' + dataset_name + '/' + task_type + '/images'
http_links = get_dataset_link(object_name, next_image, page_size)
return JsonResponse({'success': True, 'links': http_links})
@csrf_exempt
def get_dataset_is_upload(request):
if request.method == 'GET':
user = request.GET.get('user')
dataset_name = request.GET.get('datasetName')
# 检查用户和数据集名称是否提供
if not user or not dataset_name:
return JsonResponse({'success': False, 'message': '用户或数据集名称未提供'})
# 查询数据库获取数据集
dataset = Dataset.objects.filter(user=user, name=dataset_name).first()
if dataset is not None:
return JsonResponse({'success': True, 'is_upload': dataset.is_upload})
else:
return JsonResponse({'success': False, 'message': '数据集未找到'})
return JsonResponse({'success': False, 'message': '请求方法错误'})
"""根据传入的product_id和creator_id插入一条CreatorProductMatch记录统一返回格式"""
if request.method == 'POST':
try:
data = json.loads(request.body.decode('utf-8'))
except Exception:
return JsonResponse({'code': 400, 'message': '请求体不是有效的JSON', 'data': {}}, status=400)
product_id = data.get('product_id')
creator_id = data.get('creator_id')
if not product_id or not creator_id:
return JsonResponse({'code': 400, 'message': '缺少product_id或creator_id参数', 'data': {}}, status=400)
try:
product = Product.objects.get(id=product_id)
except Product.DoesNotExist:
return JsonResponse({'code': 400, 'message': '产品不存在', 'data': {}}, status=400)
try:
creator = CreatorProfile.objects.get(id=creator_id)
except CreatorProfile.DoesNotExist:
return JsonResponse({'code': 400, 'message': '达人不存在', 'data': {}}, status=400)
# 检查是否已存在
if CreatorProductMatch.objects.filter(product=product, creator=creator).exists():
return JsonResponse({'code': 400, 'message': '该达人和产品的匹配已存在', 'data': {}}, status=400)
match = CreatorProductMatch.objects.create(product=product, creator=creator)
return JsonResponse({'code': 200, 'message': '匹配关系创建成功', 'data': {'match_id': match.id}})
return JsonResponse({'code': 400, 'message': '请求方法错误', 'data': {}}, status=400)