Python novice, how to write this code more elegantly?

I have just studied python2 for a month, and the basic function of this code has been realized. However, there is such a mess in the view function. How to optimize this code? the following code is mainly through the data submitted by the form, sending post requests to the backend through ajax to modify and add

. < hr >
@admin.route("/set", methods=["GET", "POST"])
def set_task():
    if request.method == "GET":
        req = request.args
        task_id = int(req.get("id", 0))
        task_item = None
        if task_id:
            task_item = TaskItem.find(task_id)
        task_cats = TaskCategory.find_all()
        return render_template("admin/task/edit.html", task_item=task_item, task_cats=task_cats)

    req = request.values
    task_id = req["id"] if "id" in req else ""
    task_name = req["task_name"] if "task_name" in req else ""
    cat_id = int(req["cat_id"]) if "cat_id" in req else ""
    price = req["price"] if "price" in req else ""
    quantity = int(req["quantity"]) if "quantity" in req else ""
    if task_id:
        task_item = TaskItem.find(task_id)
        model_task_item = task_item
        model_task_item.update_time = get_current_time()

    else:
        model_task_item = TaskItem()
        model_task_item.create_time = get_current_time()

    model_task_item.name = task_name
    model_task_item.category_id = cat_id
    model_task_item.price = price
    model_task_item.quantity = quantity
    model_task_item.summary = ""
    model_task_item.main_image = ""

    db.session.add(model_task_item)
    db.session.commit()
    return Success()
Jul.04,2022

simply can be written like this.
to make it clear is to extract form validation and model processing in addition to grammar skills, and the interface is provided in the form of restful.


is good, there is nothing to optimize, clear and easy to understand.
uses a bunch of so-called "optimizations" to make simple things complicated and pointless.


A simple idea:

@admin.route("/set", methods=['GET', 'POST'])
def set_task():
    if request.method == 'GET':
        req = request.args
        task_id = int(req.get('id', 0))
        task_item = None
        if task_id:
            task_item = TaskItem.find(task_id)
        task_cats = TaskCategory.find_all()
        return render_template('admin/task/edit.html', task_item=task_item, task_cats=task_cats)

    else:
        req = request.values

        task_id = req.get('id', '')
        -sharp task_id = req['id'] if 'id' in req else ''
        -sharp get
        task_name = req['task_name'] if 'task_name' in req else ''
        cat_id = int(req['cat_id']) if 'cat_id' in req else ''
        price = req['price'] if 'price' in req else ''
        quantity = int(req['quantity']) if 'quantity' in req else ''

    -sharp --------------
    -sharp TaskItem
    -sharp  TaskItem.add(task_id=xxx, name=xxx, category_id=xxx, ...)

    if task_id:
        task_item = TaskItem.find(task_id)
        model_task_item = task_item
        model_task_item.update_time = get_current_time()

    else:
        model_task_item = TaskItem()
        model_task_item.create_time = get_current_time()

    model_task_item.name = task_name
    model_task_item.category_id = cat_id
    model_task_item.price = price
    model_task_item.quantity = quantity
    model_task_item.summary = ''
    model_task_item.main_image = ''

    db.session.add(model_task_item)
    db.session.commit()
    return Success()
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e445a9-44aa8.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e445a9-44aa8.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?