chuangliangProject/chuangliangBaidu/views.py

131 lines
4.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -*- coding: utf-8 -*-
"""
@Time : 2025/8/15 19:29
@Auth : 九月的海
@File : views.py
@IDE : PyCharm
@Motto : Catch as catch can....
"""
from chuangliangBaidu.migrations.BaidutaskQuery import AsyncTaskDataQuery
from django.http import JsonResponse
from django.views.decorators.http import require_GET
from django.utils import timezone
from django.shortcuts import render
import json
import logging
logger = logging.getLogger('chuangliang')
def get_task_details(request):
"""获取任务详情的API接口优化版"""
# 获取任务ID参数
task_id = request.GET.get('task_id')
current_time = timezone.now().strftime("%Y-%m-%d %H:%M:%S")
# 处理浏览器请求的Accept头
is_browser_request = 'text/html' in request.META.get('HTTP_ACCEPT', '')
# 参数验证
if not task_id:
error_data = {
"code": 400,
"message": "缺少参数: task_id",
"time": current_time,
"endpoint": request.path,
"method": request.method
}
if is_browser_request:
logger.error(error_data)
return render(request, 'json_formatter3.html', {
"json_data": json.dumps(error_data, ensure_ascii=False),
"status": 400
})
logger.error(error_data)
return JsonResponse(error_data, status=400, json_dumps_params={'ensure_ascii': False})
try:
# 转换为整数
task_id = int(task_id)
except ValueError:
error_data = {
"code": 400,
"message": "task_id 必须是整数",
"time": current_time,
"provided_value": task_id
}
if is_browser_request:
logger.error(error_data)
return render(request, 'json_formatter3.html', {
"json_data": json.dumps(error_data, ensure_ascii=False),
"status": 400
})
logger.error(error_data)
return JsonResponse(error_data, status=400, json_dumps_params={'ensure_ascii': False})
# 创建查询实例并获取数据
try:
logger.info(f'查询的任务ID: {task_id}')
task_query = AsyncTaskDataQuery(task_id)
result = task_query.get_task_details()
# 处理任务日志并解码中文
task_logs = result.get("task_logs", [])
for log in task_logs:
# 处理 task_param
if isinstance(log.get('task_param'), str):
try:
log['task_param'] = json.loads(log['task_param'])
except json.JSONDecodeError:
pass
# 处理 result_data
if isinstance(log.get('result_data'), str):
try:
log['result_data'] = json.loads(log['result_data'])
except json.JSONDecodeError:
pass
# 准备响应数据
response_data = {
"code": 200,
"message": "查询成功",
"check_time": current_time,
"task_id": task_id,
"batch_id": result.get("batch_id", ""),
"total_logs": len(task_logs),
"task_logs": task_logs,
"create_status": result.get("create_status", "")
}
# 如果是浏览器请求返回HTML视图
if is_browser_request:
logger.info(json.dumps(response_data))
return render(request, 'json_formatter3.html', {
# "json_data": response_data,
"json_data": json.dumps(response_data, ensure_ascii=False, indent=2),
"status": 200,
"task_id": task_id
})
logger.info(response_data)
return JsonResponse(
response_data,
json_dumps_params={'ensure_ascii': False}
)
except Exception as e:
error_data = {
"code": 500,
"message": "服务器内部错误",
"error": str(e),
"time": current_time,
"task_id": task_id
}
if is_browser_request:
logger.error(error_data)
return render(request, 'json_formatter3.html', {
"json_data": json.dumps(error_data, ensure_ascii=False),
"status": 500
})
logger.error(error_data)
return JsonResponse(error_data, status=500, json_dumps_params={'ensure_ascii': False})