trilium/src/notes_move_api.py

70 lines
2.5 KiB
Python
Raw Normal View History

from flask import Blueprint, jsonify
2017-09-29 11:16:36 +08:00
from flask import request
from flask_login import login_required
2017-09-29 11:16:36 +08:00
import audit_category
2017-10-10 07:48:10 +08:00
from sql import execute, commit, add_audit
from sql import getSingleResult
notes_move_api = Blueprint('notes_move_api', __name__)
2017-09-30 22:05:12 +08:00
@notes_move_api.route('/api/notes/<string:note_id>/moveTo/<string:parent_id>', methods = ['PUT'])
@login_required
2017-10-10 07:48:10 +08:00
def move_to_note(note_id, parent_id):
res = getSingleResult('select max(note_pos) as max_note_pos from notes_tree where note_pid = ?', [parent_id])
max_note_pos = res['max_note_pos']
new_note_pos = 0
if max_note_pos is None: # no children yet
new_note_pos = 0
else:
new_note_pos = max_note_pos + 1
execute("update notes_tree set note_pid = ?, note_pos = ? where note_id = ?", [parent_id, new_note_pos, note_id])
2017-10-10 07:48:10 +08:00
add_audit(audit_category.CHANGE_PARENT, request, note_id)
2017-09-29 11:16:36 +08:00
commit()
return jsonify({})
2017-10-10 07:48:10 +08:00
2017-09-30 22:05:12 +08:00
@notes_move_api.route('/api/notes/<string:note_id>/moveBefore/<string:before_note_id>', methods = ['PUT'])
2017-10-10 07:48:10 +08:00
def move_before_note(note_id, before_note_id):
before_note = getSingleResult("select * from notes_tree where note_id = ?", [before_note_id])
if before_note <> None:
execute("update notes_tree set note_pos = note_pos + 1 where note_id = ?", [before_note_id])
execute("update notes_tree set note_pid = ?, note_pos = ? where note_id = ?", [before_note['note_pid'], before_note['note_pos'], note_id])
2017-10-10 07:48:10 +08:00
add_audit(audit_category.CHANGE_POSITION, request, note_id)
2017-09-29 11:16:36 +08:00
commit()
return jsonify({})
2017-10-10 07:48:10 +08:00
2017-09-30 22:05:12 +08:00
@notes_move_api.route('/api/notes/<string:note_id>/moveAfter/<string:after_note_id>', methods = ['PUT'])
2017-10-10 07:48:10 +08:00
def move_after_note(note_id, after_note_id):
after_note = getSingleResult("select * from notes_tree where note_id = ?", [after_note_id])
if after_note <> None:
execute("update notes_tree set note_pos = note_pos + 1 where note_pid = ? and note_pos > ?", [after_note['note_pid'], after_note['note_pos']])
execute("update notes_tree set note_pid = ?, note_pos = ? where note_id = ?", [after_note['note_pid'], after_note['note_pos'] + 1, note_id])
2017-10-10 07:48:10 +08:00
add_audit(audit_category.CHANGE_POSITION, request, note_id)
2017-09-29 11:16:36 +08:00
commit()
return jsonify({})
2017-10-10 07:48:10 +08:00
2017-09-30 22:05:12 +08:00
@notes_move_api.route('/api/notes/<string:note_id>/expanded/<int:expanded>', methods = ['PUT'])
2017-10-10 07:48:10 +08:00
def set_expanded_note(note_id, expanded):
execute("update notes_tree set is_expanded = ? where note_id = ?", [expanded, note_id])
2017-09-29 11:16:36 +08:00
# no audit here, not really important
commit()
return jsonify({})