add: order paths & mds
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
from flask import Blueprint, request, jsonify
|
from flask import Blueprint, request, jsonify
|
||||||
|
|
||||||
import api
|
import api
|
||||||
from api import require_auth, etag_response
|
from api import require_auth, etag_response
|
||||||
from contexts.RequestContext import RequestContext
|
from contexts.RequestContext import RequestContext
|
||||||
@@ -114,3 +115,41 @@ def delete_markdown(markdown_id):
|
|||||||
session.delete(markdown)
|
session.delete(markdown)
|
||||||
session.commit()
|
session.commit()
|
||||||
return jsonify({"message": "deleted"}), 200
|
return jsonify({"message": "deleted"}), 200
|
||||||
|
|
||||||
|
|
||||||
|
@markdown_bp.route('/move_forward/<int:markdown_id>', methods=['PATCH'])
|
||||||
|
@require_auth(roles=['admin'])
|
||||||
|
@limiter.limit(api.get_rate_limit)
|
||||||
|
def move_forward(markdown_id):
|
||||||
|
with get_db() as session:
|
||||||
|
markdown = session.query(Markdown).get(markdown_id)
|
||||||
|
if not markdown:
|
||||||
|
return jsonify({"error": "file not found"}), 404
|
||||||
|
siblings = session.query(Markdown).filter(Markdown.path_id == markdown.path_id).order_by(Markdown.order).all()
|
||||||
|
current_index = siblings.index(markdown)
|
||||||
|
if current_index == 0:
|
||||||
|
return jsonify({"error": "already at the first position"}), 400
|
||||||
|
previous_markdown = siblings[current_index - 1]
|
||||||
|
markdown.order, previous_markdown.order = previous_markdown.order, markdown.order
|
||||||
|
session.commit()
|
||||||
|
return jsonify(markdown.to_dict()), 200
|
||||||
|
|
||||||
|
|
||||||
|
@markdown_bp.route('/move_backward/<int:markdown_id>', methods=['PATCH'])
|
||||||
|
@require_auth(roles=['admin'])
|
||||||
|
@limiter.limit(api.get_rate_limit)
|
||||||
|
def move_backward(markdown_id):
|
||||||
|
with get_db() as session:
|
||||||
|
markdown = session.query(Markdown).get(markdown_id)
|
||||||
|
if not markdown:
|
||||||
|
return jsonify({"error": "file not found"}), 404
|
||||||
|
siblings = session.query(Markdown).filter(Markdown.path_id == markdown.path_id).order_by(Markdown.order).all()
|
||||||
|
current_index = siblings.index(markdown)
|
||||||
|
if current_index == len(siblings) - 1:
|
||||||
|
return jsonify({"error": "already at the last position"}), 400
|
||||||
|
|
||||||
|
next_markdown = siblings[current_index + 1]
|
||||||
|
markdown.order, next_markdown.order = next_markdown.order, next_markdown.order
|
||||||
|
session.commit()
|
||||||
|
return jsonify(markdown.to_dict()), 200
|
||||||
|
|
||||||
|
|||||||
37
api/path.py
37
api/path.py
@@ -110,3 +110,40 @@ def delete_path(path_id):
|
|||||||
session.commit()
|
session.commit()
|
||||||
return jsonify({"message": "path deleted"}), 200
|
return jsonify({"message": "path deleted"}), 200
|
||||||
|
|
||||||
|
|
||||||
|
@path_bp.route('/move_forward/<int:path_id>', methods=['PATCH'])
|
||||||
|
@require_auth(roles=['admin'])
|
||||||
|
@limiter.limit(api.get_rate_limit)
|
||||||
|
def move_forward(path_id):
|
||||||
|
with get_db() as session:
|
||||||
|
path = session.query(Path).get(path_id)
|
||||||
|
if not path:
|
||||||
|
return jsonify({"error": "file not found"}), 404
|
||||||
|
siblings = session.query(Path).filter(Path.parent_id == path.parent_id).order_by(Path.order).all()
|
||||||
|
current_index = siblings.index(path)
|
||||||
|
if current_index == 0:
|
||||||
|
return jsonify({"error": "already at the first position"}), 400
|
||||||
|
previous_path = siblings[current_index - 1]
|
||||||
|
path.order, previous_path.order = previous_path.order, path.order
|
||||||
|
session.commit()
|
||||||
|
return jsonify(path.to_dict()), 200
|
||||||
|
|
||||||
|
|
||||||
|
@path_bp.route('/move_backward/<int:path_id>', methods=['PATCH'])
|
||||||
|
@require_auth(roles=['admin'])
|
||||||
|
@limiter.limit(api.get_rate_limit)
|
||||||
|
def move_backward(path_id):
|
||||||
|
with get_db() as session:
|
||||||
|
path = session.query(Path).get(path_id)
|
||||||
|
if not path:
|
||||||
|
return jsonify({"error": "file not found"}), 404
|
||||||
|
siblings = session.query(Path).filter(Path.parent_id == path.parent_id).order_by(Path.order).all()
|
||||||
|
current_index = siblings.index(path)
|
||||||
|
if current_index == len(siblings) - 1:
|
||||||
|
return jsonify({"error": "already at the last position"}), 400
|
||||||
|
|
||||||
|
next_path = siblings[current_index + 1]
|
||||||
|
path.order, next_path.order = next_path.order, path.order
|
||||||
|
session.commit()
|
||||||
|
return jsonify(path.to_dict()), 200
|
||||||
|
|
||||||
|
|||||||
@@ -45,16 +45,24 @@ def create_all():
|
|||||||
with engine.begin() as conn:
|
with engine.begin() as conn:
|
||||||
Base.metadata.create_all(bind=conn)
|
Base.metadata.create_all(bind=conn)
|
||||||
|
|
||||||
|
def run_scripts():
|
||||||
|
from db.models import table_models
|
||||||
|
with get_db() as session:
|
||||||
|
for model in table_models:
|
||||||
|
if hasattr(model, "__scripts__"):
|
||||||
|
scripts = model.__scripts__
|
||||||
|
for script in scripts:
|
||||||
|
session.execute(script)
|
||||||
|
session.commit()
|
||||||
|
|
||||||
def init_payload():
|
def init_payload():
|
||||||
|
|
||||||
from db.models import table_models
|
from db.models import table_models
|
||||||
with get_db() as session:
|
with get_db() as session:
|
||||||
session.execute(text("SET FOREIGN_KEY_CHECKS = 0;"))
|
session.execute(text("SET FOREIGN_KEY_CHECKS = 0;"))
|
||||||
for model in table_models:
|
for model in table_models:
|
||||||
print(f"MODEL -- {model}, {hasattr(model, '__pay_load__')}")
|
print(f"MODEL -- {model}, {hasattr(model, '__pay_load__')}")
|
||||||
if hasattr(model, "__pay_load__"):
|
if hasattr(model, "__pay_load__"):
|
||||||
payload =model.__pay_load__[ENVIRONMENT]
|
payload = model.__pay_load__[ENVIRONMENT]
|
||||||
print(f"- - [ - ] hasattr, {ENVIRONMENT} - {payload}")
|
print(f"- - [ - ] hasattr, {ENVIRONMENT} - {payload}")
|
||||||
stmt = insert(model.__table__).values(payload).prefix_with("IGNORE")
|
stmt = insert(model.__table__).values(payload).prefix_with("IGNORE")
|
||||||
print(f"- - [ - ] {stmt}\n")
|
print(f"- - [ - ] {stmt}\n")
|
||||||
@@ -74,6 +82,8 @@ def setup_db():
|
|||||||
print("[ x ] db cleared")
|
print("[ x ] db cleared")
|
||||||
create_all()
|
create_all()
|
||||||
print("[ x ] db created")
|
print("[ x ] db created")
|
||||||
|
run_scripts()
|
||||||
|
print("[ x ] db scripts executed")
|
||||||
init_payload()
|
init_payload()
|
||||||
print("[ x ] payload loaded")
|
print("[ x ] payload loaded")
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#db/models/Markdown.py
|
import uuid
|
||||||
from sqlalchemy import Column, Text, Integer, String, DateTime, ForeignKey
|
|
||||||
|
from sqlalchemy import Column, Text, Integer, String, DateTime, ForeignKey, Float, text
|
||||||
from db.models import Base
|
from db.models import Base
|
||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
@@ -10,6 +11,7 @@ class Markdown(Base):
|
|||||||
content = Column(Text, nullable=False)
|
content = Column(Text, nullable=False)
|
||||||
created_at = Column(DateTime, default=datetime.datetime.utcnow)
|
created_at = Column(DateTime, default=datetime.datetime.utcnow)
|
||||||
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()))
|
||||||
|
|
||||||
def to_dict(self):
|
def to_dict(self):
|
||||||
return {
|
return {
|
||||||
@@ -18,6 +20,7 @@ class Markdown(Base):
|
|||||||
'content': self.content,
|
'content': self.content,
|
||||||
'created_at': self.created_at,
|
'created_at': self.created_at,
|
||||||
'path_id': self.path_id,
|
'path_id': self.path_id,
|
||||||
|
'order': self.order,
|
||||||
}
|
}
|
||||||
__pay_load__ = {
|
__pay_load__ = {
|
||||||
'dev': [
|
'dev': [
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#db/models/Path.py
|
import uuid
|
||||||
from sqlalchemy import Column, String, Integer, ForeignKey, UniqueConstraint
|
|
||||||
|
from sqlalchemy import Column, String, Integer, ForeignKey, UniqueConstraint, text
|
||||||
from db.models import Base
|
from db.models import Base
|
||||||
|
|
||||||
|
|
||||||
@@ -8,12 +9,14 @@ class Path(Base):
|
|||||||
id = Column(Integer, primary_key=True, autoincrement=True)
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||||
name = Column(String(50), nullable=False)
|
name = Column(String(50), nullable=False)
|
||||||
parent_id = Column(Integer, ForeignKey("path.id"), nullable=True)
|
parent_id = Column(Integer, ForeignKey("path.id"), nullable=True)
|
||||||
|
order = Column(String(36), default=lambda: str(uuid.uuid4()))
|
||||||
__table_args__ = (UniqueConstraint("parent_id", "name", name="unique_parent_id_name"), )
|
__table_args__ = (UniqueConstraint("parent_id", "name", name="unique_parent_id_name"), )
|
||||||
def to_dict(self):
|
def to_dict(self):
|
||||||
return {
|
return {
|
||||||
"id": self.id,
|
"id": self.id,
|
||||||
"name": self.name,
|
"name": self.name,
|
||||||
"parent_id": self.parent_id,
|
"parent_id": self.parent_id,
|
||||||
|
"order": self.order,
|
||||||
}
|
}
|
||||||
|
|
||||||
__pay_load__ = {
|
__pay_load__ = {
|
||||||
@@ -26,3 +29,5 @@ class Path(Base):
|
|||||||
{'id': 1, 'name': '', 'parent_id': None},
|
{'id': 1, 'name': '', 'parent_id': None},
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user