Compare commits
1 Commits
e7000f0b2e
...
6626fac452
| Author | SHA1 | Date | |
|---|---|---|---|
| 6626fac452 |
19
api/tree.py
19
api/tree.py
@@ -15,24 +15,18 @@ tree_bp = Blueprint('tree', __name__, url_prefix='/api/tree')
|
|||||||
|
|
||||||
def build_tree(db: Session, parent_id: int = None):
|
def build_tree(db: Session, parent_id: int = None):
|
||||||
path_nodes = db.query(Path).filter(Path.parent_id == parent_id).all()
|
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()
|
md_nodes = db.query(Markdown.id, Markdown.title, Markdown.order, Markdown.shortcut, Markdown.setting_id).filter(Markdown.path_id == parent_id).all()
|
||||||
t0 = [
|
t0 = [
|
||||||
{
|
{
|
||||||
"type": "markdown",
|
|
||||||
"id": node.id,
|
"id": node.id,
|
||||||
"title": node.title,
|
"title": node.title,
|
||||||
"order": node.order,
|
"order": node.order,
|
||||||
"shortcut": node.shortcut
|
"setting_id": node.setting_id,
|
||||||
|
"type": "markdown"
|
||||||
} for node in md_nodes
|
} for node in md_nodes
|
||||||
]
|
]
|
||||||
t1 = [
|
t1 = [
|
||||||
{
|
{**node.to_dict(), "type": "path", "children": build_tree(db, node.id)} for node in path_nodes
|
||||||
"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 node in t1:
|
||||||
for child in node["children"]:
|
for child in node["children"]:
|
||||||
@@ -47,11 +41,12 @@ def build_tree(db: Session, parent_id: int = None):
|
|||||||
def get_tree():
|
def get_tree():
|
||||||
with get_db() as session:
|
with get_db() as session:
|
||||||
children = build_tree(session, 1)
|
children = build_tree(session, 1)
|
||||||
|
root = session.query(Path).get(1)
|
||||||
|
|
||||||
return jsonify(
|
return jsonify(
|
||||||
{
|
{
|
||||||
|
**root.to_dict(),
|
||||||
"type": "path",
|
"type": "path",
|
||||||
"id": 1,
|
|
||||||
"name": "Root",
|
|
||||||
"index": any("title" in child.keys() and child["title"] == "index" for child in children),
|
"index": any("title" in child.keys() and child["title"] == "index" for child in children),
|
||||||
"children": children
|
"children": children
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ class Markdown(Base):
|
|||||||
path_id = Column(Integer, ForeignKey('path.id'), nullable=False)
|
path_id = Column(Integer, ForeignKey('path.id'), nullable=False)
|
||||||
order = Column(String(36), default=lambda: str(uuid.uuid4()))
|
order = Column(String(36), default=lambda: str(uuid.uuid4()))
|
||||||
shortcut = Column(String(36), default="")
|
shortcut = Column(String(36), default="")
|
||||||
template_id = Column(Integer, ForeignKey("markdown_template.id"), nullable=True)
|
setting_id = Column(Integer, ForeignKey("markdown_setting.id"), nullable=True)
|
||||||
__table_args__ = (UniqueConstraint('path_id', 'title', name="unique_path_id_title"),)
|
__table_args__ = (UniqueConstraint('path_id', 'title', name="unique_path_id_title"),)
|
||||||
def to_dict(self):
|
def to_dict(self):
|
||||||
return {
|
return {
|
||||||
@@ -24,7 +24,7 @@ class Markdown(Base):
|
|||||||
'path_id': self.path_id,
|
'path_id': self.path_id,
|
||||||
'order': self.order,
|
'order': self.order,
|
||||||
'shortcut': self.shortcut,
|
'shortcut': self.shortcut,
|
||||||
'template_id': self.template_id,
|
'setting_id': self.setting_id,
|
||||||
}
|
}
|
||||||
__payload__ = {
|
__payload__ = {
|
||||||
'dev': [
|
'dev': [
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ from db.models import Base
|
|||||||
|
|
||||||
|
|
||||||
class MarkdownSetting(Base):
|
class MarkdownSetting(Base):
|
||||||
__tablename__ = 'markdown_settings'
|
__tablename__ = 'markdown_setting'
|
||||||
id = Column(Integer, primary_key=True, autoincrement=True)
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||||
template_setting_id = Column(Integer, ForeignKey('markdown_template.id'), nullable=True)
|
template_setting_id = Column(Integer, ForeignKey('markdown_template.id'), nullable=True)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user