task_update.py 1.7 KB
# coding=utf-8
#author:        4N
#createtime:    2020/9/4
#email:         nheweijun@sina.com

from ..models import db,Task,Process
from app.util.component.ApiTemplate import ApiTemplate
import datetime
import json
import uuid
class Api(ApiTemplate):
    api_name = "任务更新"
    def para_check(self):
        if not self.para.get("task_guid"):
            raise Exception("缺乏task_guid参数。")

    def process(self):

        res = {}
        try:
            task_guid = self.para.get("task_guid")
            msg = self.para.get("msg")
            task:Task = Task.query.filter_by(guid=task_guid)
            update = json.loads(self.para.get("update"))
            update["update_time"] = datetime.datetime.now()
            task.update(update)

            if msg:
                message = "{} {}".format(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), msg)
                task_process_guid = uuid.uuid1().__str__()
                task_process = Process(guid=task_process_guid, message=message, time=datetime.datetime.now(),
                                       task_guid=task_guid)
                db.session.add(task_process)
            db.session.commit()
        except Exception as e:
            raise e
        return res

    api_doc={
    "tags":["任务接口"],
    "parameters":[
        {"name": "task_guid",
         "in": "formData",
         "type": "string"},
        {"name": "update",
         "in": "formData",
         "type": "string"},
        {"name": "msg",
         "in": "formData",
         "type": "string"},

    ],
    "responses":{
        200:{
            "schema":{
                "properties":{
                }
            }
            }
        }
    }