Compare commits
1 Commits
c10c690149
...
a96e833aa6
| Author | SHA1 | Date | |
|---|---|---|---|
| a96e833aa6 |
@@ -1,4 +1,5 @@
|
||||
from flask import Blueprint, request, jsonify
|
||||
|
||||
import api
|
||||
from api import require_auth, etag_response
|
||||
from contexts.RequestContext import RequestContext
|
||||
@@ -113,4 +114,42 @@ def delete_markdown(markdown_id):
|
||||
return jsonify({"error": f"file not found - {errno}"}), 404
|
||||
session.delete(markdown)
|
||||
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()
|
||||
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:
|
||||
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():
|
||||
|
||||
from db.models import table_models
|
||||
with get_db() as session:
|
||||
session.execute(text("SET FOREIGN_KEY_CHECKS = 0;"))
|
||||
for model in table_models:
|
||||
print(f"MODEL -- {model}, {hasattr(model, '__pay_load__')}")
|
||||
if hasattr(model, "__pay_load__"):
|
||||
payload =model.__pay_load__[ENVIRONMENT]
|
||||
payload = model.__pay_load__[ENVIRONMENT]
|
||||
print(f"- - [ - ] hasattr, {ENVIRONMENT} - {payload}")
|
||||
stmt = insert(model.__table__).values(payload).prefix_with("IGNORE")
|
||||
print(f"- - [ - ] {stmt}\n")
|
||||
@@ -74,6 +82,8 @@ def setup_db():
|
||||
print("[ x ] db cleared")
|
||||
create_all()
|
||||
print("[ x ] db created")
|
||||
run_scripts()
|
||||
print("[ x ] db scripts executed")
|
||||
init_payload()
|
||||
print("[ x ] payload loaded")
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#db/models/Markdown.py
|
||||
from sqlalchemy import Column, Text, Integer, String, DateTime, ForeignKey
|
||||
import uuid
|
||||
|
||||
from sqlalchemy import Column, Text, Integer, String, DateTime, ForeignKey, Float, text
|
||||
from db.models import Base
|
||||
import datetime
|
||||
|
||||
@@ -10,6 +11,7 @@ class Markdown(Base):
|
||||
content = Column(Text, nullable=False)
|
||||
created_at = Column(DateTime, default=datetime.datetime.utcnow)
|
||||
path_id = Column(Integer, ForeignKey('path.id'), nullable=False)
|
||||
order = Column(String(36), default=lambda: str(uuid.uuid4()))
|
||||
|
||||
def to_dict(self):
|
||||
return {
|
||||
@@ -18,6 +20,7 @@ class Markdown(Base):
|
||||
'content': self.content,
|
||||
'created_at': self.created_at,
|
||||
'path_id': self.path_id,
|
||||
'order': self.order,
|
||||
}
|
||||
__pay_load__ = {
|
||||
'dev': [
|
||||
@@ -26,4 +29,4 @@ class Markdown(Base):
|
||||
'prod': [
|
||||
{'id': 1, 'title': 'index', 'content': ' ', 'created_at': datetime.datetime.utcnow, 'path_id': 1},
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#db/models/Path.py
|
||||
from sqlalchemy import Column, String, Integer, ForeignKey, UniqueConstraint
|
||||
import uuid
|
||||
|
||||
from sqlalchemy import Column, String, Integer, ForeignKey, UniqueConstraint, text
|
||||
from db.models import Base
|
||||
|
||||
|
||||
@@ -8,12 +9,14 @@ class Path(Base):
|
||||
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||
name = Column(String(50), nullable=False)
|
||||
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"), )
|
||||
def to_dict(self):
|
||||
return {
|
||||
"id": self.id,
|
||||
"name": self.name,
|
||||
"parent_id": self.parent_id,
|
||||
"order": self.order,
|
||||
}
|
||||
|
||||
__pay_load__ = {
|
||||
@@ -25,4 +28,6 @@ class Path(Base):
|
||||
'prod': [
|
||||
{'id': 1, 'name': '', 'parent_id': None},
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user