add: load backup
This commit is contained in:
58
api/tree.py
Normal file
58
api/tree.py
Normal file
@@ -0,0 +1,58 @@
|
||||
from flask import Blueprint, request, jsonify
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
import api
|
||||
from api import require_auth, etag_response
|
||||
from db import get_db
|
||||
from db.models.Markdown import Markdown
|
||||
from db.models.Path import Path
|
||||
from api import limiter
|
||||
import logging
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
tree_bp = Blueprint('tree', __name__, url_prefix='/api/tree')
|
||||
|
||||
|
||||
def build_tree(db: Session, parent_id: int = None):
|
||||
path_nodes = db.query(Path).filter(Path.parent_id == parent_id).all()
|
||||
md_nodes = db.query(Markdown.id, Markdown.title, Markdown.order, Markdown.shortcut).filter(Markdown.path_id == parent_id).all()
|
||||
t0 = [
|
||||
{
|
||||
"type": "markdown",
|
||||
"id": node.id,
|
||||
"title": node.title,
|
||||
"order": node.order,
|
||||
"shortcut": node.shortcut
|
||||
} for node in md_nodes
|
||||
]
|
||||
t1 = [
|
||||
{
|
||||
"type": "path",
|
||||
"id": node.id,
|
||||
"name": node.name,
|
||||
"order": node.order,
|
||||
"children": build_tree(db, node.id)
|
||||
} for node in path_nodes
|
||||
]
|
||||
for node in t1:
|
||||
for child in node["children"]:
|
||||
if "title" in child.keys() and child["title"] == "index":
|
||||
node["index"] = True
|
||||
break
|
||||
return t0 + t1
|
||||
|
||||
@tree_bp.route('/', methods=['GET'])
|
||||
@limiter.limit(api.get_rate_limit)
|
||||
@etag_response
|
||||
def get_tree():
|
||||
with get_db() as session:
|
||||
children = build_tree(session, 1)
|
||||
return jsonify(
|
||||
{
|
||||
"type": "path",
|
||||
"id": 1,
|
||||
"name": "Root",
|
||||
"index": any("title" in child.keys() and child["title"] == "index" for child in children),
|
||||
"children": children
|
||||
}
|
||||
), 200
|
||||
Reference in New Issue
Block a user