コード例 #1
0
def delete_category(category_id):
    def callback(conn, cursor):
        cursor.execute('delete from category where id = %s', (category_id, ))
        conn.commit()
        return 'success'

    return run(callback)
コード例 #2
0
def modify_note(note_id,content):
    def callback(conn,cursor):
        now = time.time()
        cursor.execute('UPDATE note set content = %s, modify_time = %s where id = %s', [
            content, now, note_id])
        conn.commit()
        return 'success'
    return run(callback)
コード例 #3
0
def colorChange(category_id, color):
    def callback(conn, cursor):
        cursor.execute('UPDATE category set color = %s where id = %s',
                       [color, category_id])
        conn.commit()
        return 'success'

    return run(callback)
コード例 #4
0
def modify_category(category_id, name):
    def callback(conn, cursor):
        cursor.execute('UPDATE category set name = %s where id = %s',
                       [name, category_id])
        conn.commit()
        return 'success'

    return run(callback)
コード例 #5
0
def create_category(name):
    def callback(conn, cursor):
        cursor.execute('insert into category (name) values (%s)', [name])
        insert_id = cursor.lastrowid  # 一定要在conn.commit()之前,否则会返回0
        conn.commit()
        return {'insert_id': insert_id}

    return run(callback)
コード例 #6
0
def query_categories():
    def callback(conn, cursor):
        cursor.execute("select * from category order by order_number desc")
        values = cursor.fetchall()
        columes = ['id', 'name', 'prediction', 'order_number', 'color']
        return [dict(zip(columes, value)) for value in values]

    return run(callback)
コード例 #7
0
def orderChange(category_id, order):
    def callback(conn, cursor):
        cursor.execute('UPDATE category set order_number = %s where id = %s',
                       [order, category_id])
        conn.commit()
        return 'success'

    return run(callback)
コード例 #8
0
def create_note(content,category_id):
    if category_id == None:
        category_id = 0
    def callback(conn,cursor):
        now = time.time()
        cursor.execute('INSERT into note (content,category,create_time,modify_time) values (%s, %s,%s, %s)', [content,category_id,now,now])
        insert_id = cursor.lastrowid # 一定要在conn.commit()之前,否则会返回0
        conn.commit()
        return {'insert_id':insert_id}
    return run(callback)
コード例 #9
0
def classify(note_id, cate_id):
    def callback(conn, cursor):
        now = time.time()
        cursor.execute(
            'UPDATE note set category = %s, create_time = %s, training_mark=0 where id = %s',
            [cate_id, now, note_id])
        conn.commit()
        return 'success'

    return run(callback)
コード例 #10
0
def unclassify(note_id):
    def callback(conn, cursor):
        now = time.time()
        cursor.execute(
            'UPDATE note set category = %s, modify_time = %s where id = %s',
            [0, now, note_id])
        conn.commit()
        return 'success'

    return run(callback)
コード例 #11
0
def find_notes(content):
    def callback(conn, cursor):
        cursor.execute("select * from note where content like %s ",
                       ['%' + content + '%'])
        values = cursor.fetchall()
        columns = [
            'id', 'content', 'category', 'create_time', 'modify_time', 'status'
        ]
        return [dict(zip(columns, value)) for value in values]

    return run(callback)
コード例 #12
0
def savePrediction(category_id, noteIds, mode='replace'):
    def callback(conn, cursor):
        if mode == 'append':
            cursor.execute("select * from category where id=%s", [category_id])
            values = cursor.fetchall()
            newIds = json.loads(values[0][2]) + noteIds

            cursor.execute('UPDATE category set prediction = %s where id = %s',
                           [json.dumps(newIds), category_id])
            conn.commit()
        else:
            cursor.execute('UPDATE category set prediction = %s where id = %s',
                           [json.dumps(noteIds), category_id])
            conn.commit()
        return 'success'

    return run(callback)
コード例 #13
0
def delete_note(note_id):
    def callback(conn,cursor):
        cursor.execute('UPDATE note set status = 1 where id = %s', (note_id,))
        conn.commit()
        return 'success'
    return run(callback)
コード例 #14
0
def query_notes(categoryId, pageSize, start):
    if start == None:
        start = 1
    if pageSize == None:
        pageSize = 10
    if categoryId == None:
        categoryId = 'all'

    start = int(start)
    pageSize = int(pageSize)

    def callback(conn, cursor):
        notes = []
        if categoryId == 'all' or categoryId == '0':
            if categoryId == '0':
                cursor.execute(
                    "SELECT * from note where category=%s and status=0 Order By modify_time Desc limit %s,%s",
                    [categoryId, start, pageSize])
            else:
                cursor.execute(
                    "SELECT * from note where status=0 Order By modify_time Desc limit %s,%s",
                    [start, pageSize])
            notes = cursor.fetchall()
        else:
            cursor.execute("SELECT * from category where id = %s",
                           [categoryId])
            values = cursor.fetchall()
            prediction = values[0][2]
            predictionList = []
            if prediction != None:
                predictionList = json.loads(prediction)

            cursor.execute("SELECT * from note where status=0")
            values = cursor.fetchall()
            predictionNotes = list(
                filter(lambda x: x[0] in predictionList, values))

            cursor.execute(
                "SELECT * from note where category=%s and status=0 Order By modify_time Desc",
                [categoryId])
            # [categoryId,(pageNum-1)*pageSize,pageSize]
            hardCateNotes = cursor.fetchall()
            # 把硬分类的文章加上去, 并去重
            for hNote in hardCateNotes:
                if hNote[0] not in predictionList:
                    predictionNotes.append(hNote)
            # 判断predictionNotes中是否有 硬分类不属于这个分类的 剔除
            filterdNotes = list(
                filter(lambda x: x[2] == 0 or str(x[2]) == categoryId,
                       predictionNotes))
            filterdNotes = sorted(filterdNotes, key=lambda x: -x[4])
            # 加上分页
            notes = filterdNotes[start:start + pageSize]

        # values = cursor.fetchall()
        columes = [
            'id', 'content', 'category', 'create_time', 'modify_time', 'status'
        ]
        return [dict(zip(columes, note)) for note in notes]

    return run(callback)