【项目实训】个人进度博客之后端部分代码实现(补)

本文档详细介绍了三个后端接口的实现:任务展示、任务取消和任务发布。任务展示接口返回所有任务的JSON数组;任务取消接口验证发起人身份和任务状态,只有未开始的任务才能被取消;任务发布接口接收参数并保存任务信息,包括任务类型、数据集类型、需求用户、任务名称等,并将Python文件存储到指定目录。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

概述

近期主要完成了任务展示、任务取消以及任务发布的后端接口

任务展示后端接口

# 此方法需要返回一个json对象的数组
# 对象示例:{"mission_id":1,"mission_type":"DTA","dataset":
@require_http_methods(["POST"])
def showMissions(request):
    response = {}
    missions = []
    # jsonData = json.loads(request.body)
    res = Mission.objects.all()
    for i in res:
        mission_obj = {}
        mission_obj["mission_id"] = i.mission_id
        mission_obj["mission_type"] = i.mission_type
        mission_obj["dataset_type"] = i.dataset_type
        mission_obj["demand_user"] = i.demand_user
        mission_obj["current_user"] = i.current_user
        mission_obj["user_accounts"] = i.user_accounts
        mission_obj["mission_launcher"] = i.mission_launcher
        mission_obj["mission_state"] = i.mission_state
        missions.append(mission_obj)
    response["results"] = missions
    return JsonResponse(response)

任务取消后端接口

# 取消任务,条件,需要任务id以及发起人账号,以及任务状态为未开始
# 发现为非发起人则返回仅任务发起人有取消权限
# 发现任务状态不对返回该任务状态下不可取消
# 取消后删除对应任务表项
@require_http_methods(["GET"])
def cancelMission(request):
    mission_id = request.GET.get("mission_id")
    mission_launcher = request.GET.get("mission_launcher")

    response = {}
    try:
        res = Mission.objects.get(mission_id=mission_id)
    except Exception:
        response = {"results": "该任务不存在"}
        return JsonResponse(response)
    else:
        if res.mission_launcher != mission_launcher:
            response["results"] = "仅任务发起人有取消权限"
            return JsonResponse(response)
        else:
            if res.mission_state != 'waiting':
                response["results"] = "任务仅可在waiting状态下取消"
                return JsonResponse(response)
            else:
                # 删除对应项
                Mission.objects.get(mission_id=mission_id).delete()
                response["results"] = "任务取消成功"
                return JsonResponse(response)

任务发布后端接口

@require_http_methods(["POST"])
def launchMission(request):
    try:
        mission_type = request.POST.get("mission_type")
        dataset_type = request.POST.get("dataset_type")
        demand_user = request.POST.get("demand_user")
        mission_name = request.POST.get("mission_name")
        # python_file = request.POST.get("python_file")
        python_file = request.FILES.get("python_file")
        mission_launcher = request.POST.get("mission_launcher")

        mkdir("rear_core/uploads")

        # 把python file写入到upload里
        with open('rear_core/uploads/%s' % python_file.name, 'wb') as f:
            for i in python_file.chunks():
                con = python_file.chunks()
                con2 = i
                f.write(i)

        fileBytes = ''
        fileBytes = fileBytes.encode()
        with open('rear_core/uploads/%s' % python_file.name, "rb") as f:
            # 循环读取一张图片,一次性读取1024个字节
            while True:
                a = f.read(1024)
                fileBytes += a
                if a == b"":
                    break

        if mission_type is None:
            response = {'result': 'mission_type参数缺失'}
            return JsonResponse(response)
        if dataset_type is None:
            response = {'result': 'dataset_type参数缺失'}
            return JsonResponse(response)
        if demand_user is None:
            response = {'result': 'demand_user参数缺失'}
            return JsonResponse(response)
        if python_file is None:
            response = {'result': 'python_file参数缺失'}
            return JsonResponse(response)
        if mission_launcher is None:
            response = {'result': 'mission_launcher参数缺失'}
            return JsonResponse(response)

        Mission(mission_type=mission_type, dataset_type=dataset_type,
                demand_user=demand_user, python_file=fileBytes,
                mission_launcher=mission_launcher, mission_state='waiting',
                current_user=1, user_accounts=mission_launcher).save()

        response = {'result': '任务创建成功'}
        return JsonResponse(response)
    except Exception:
        response = {'result': '任务创建失败'}
        return JsonResponse(response)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

芜湖大司码丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值