Consult the simplified implementation of the same operation of multiple Model classes in Python

now there is a problem in the project. The project is completed in flask. Now there are several data tables with the same order field, and all need to query the data of these tables and adjust the value of the order field.

the inefficient way is to implement the operation functions for the Model classes of these tables respectively, but in the code written in this way, only a few keywords are different between the functions, which makes the code feel very redundant

.

so ask the bosses, is there any concise way to achieve it?
post my code snippet first. The following is the operation code for the DocItem class, and other Model classes need the same processing logic. The only difference between the final code is that DocItem is replaced with the corresponding class name, which is very redundant.

def change_docorder():
    reqdata = request.json
    op = reqdata["op"]
    docid = reqdata["docid"]

    docitem = DocItem.query.filter(DocItem.id==docid).one_or_none()
    if docitem is None:
        return error_response(u"")
    -sharp 
    if op == "up":
        another = DocItem.query.filter(and_(DocItem.category_id == docitem.category_id,
                DocItem.order < docitem.order)).order_by(DocItem.order.desc()).first()
        if another is None:
            return error_response(u"")
        tmp = another.order
        another.order = docitem.order
        docitem.order = tmp
        another.save()
        docitem.save()
    elif op == "down":
        another = DocItem.query.filter(and_(DocItem.category_id == docitem.category_id,
                DocItem.order>docitem.order)).order_by(DocItem.order.asc()).first()
        if another is None:
            return error_response(u"")
        tmp = another.order
        another.order = docitem.order
        docitem.order = tmp
        another.save()
        docitem.save()
    else:
        return make_response(u"!")
    return make_response(json.dumps({
        "status": "success",
        "msg": u""
    }))
Mar.02,2021

pass the model name as a parameter directly

def change_docorder(item):
    result = item.query.filter(item.id==docid).one_or_none()

the same logic is best modularized, either as a function or as a class. I'll follow the idea of @ prolifes and refine it a little further. It is recommended that you decompose the same part of the above function, decompose it into several different functions, and then call it in the class.


if I do it, I will follow the @ prolifes mentality, as follows:
docitem = DocItem.query.filter (DocItem.id==docid). One_or_none ()

encapsulate the model object docitem as a parameter into a function call

def handle_model (model=None):

if not model:
    return error_response(u"")

the following can copy directly. If the part is different, you can try to use it as a parameter

.
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-1e9ac4a-475d6.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-1e9ac4a-475d6.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?