kc token public key/token issue, path root set to 1
This commit is contained in:
43
api/path.py
43
api/path.py
@@ -12,15 +12,15 @@ path_bp = Blueprint('path', __name__, url_prefix='/api/path')
|
||||
@path_bp.route('/', methods=['GET'])
|
||||
@limiter.limit('5 per minute')
|
||||
def get_root_paths():
|
||||
with get_db() as db:
|
||||
paths = db.query(Path).filter(Path.parent_id == 0)
|
||||
with get_db() as session:
|
||||
paths = session.query(Path).filter(Path.parent_id == 1)
|
||||
return jsonify([pth.to_dict() for pth in paths]), 200
|
||||
|
||||
@path_bp.route('/<int:path_id>', methods=['GET'])
|
||||
@limiter.limit('5 per minute')
|
||||
def get_path(path_id):
|
||||
with get_db() as db:
|
||||
path = db.query(Path).get(path_id)
|
||||
with get_db() as session:
|
||||
path = session.query(Path).get(path_id)
|
||||
if path is None:
|
||||
return jsonify({"error": "file not found"}), 404
|
||||
return jsonify(path.to_dict()), 200
|
||||
@@ -28,8 +28,8 @@ def get_path(path_id):
|
||||
@path_bp.route('/parent/<int:parent_id>', methods=['GET'])
|
||||
@limiter.limit('5 per minute')
|
||||
def get_path_by_parent(parent_id):
|
||||
with get_db() as db:
|
||||
paths = db.query(Path).filter(Path.parent_id == parent_id).all()
|
||||
with get_db() as session:
|
||||
paths = session.query(Path).filter(Path.parent_id == parent_id).all()
|
||||
return jsonify([pth.to_dict() for pth in paths]), 200
|
||||
|
||||
@path_bp.route('/', methods=['POST'])
|
||||
@@ -39,15 +39,14 @@ def create_path():
|
||||
data = request.json
|
||||
if not data or 'name' not in data or 'parent_id' not in data:
|
||||
return jsonify({"error": "bad request"}), 400
|
||||
with get_db() as db:
|
||||
if data['parent_id'] != 0 and not db.query(Path).get(data['parent_id']):
|
||||
with get_db() as session:
|
||||
if data['parent_id'] != 0 and not session.query(Path).get(data['parent_id']):
|
||||
return jsonify({"error": "path not found"}), 404
|
||||
if db.query(Path).filter_by(name=data['name'], parent_id=data['parent_id']).first():
|
||||
if session.query(Path).filter_by(name=data['name'], parent_id=data['parent_id']).first():
|
||||
return jsonify({"error": "Path already exists under the parent"}), 409
|
||||
|
||||
new_path = Path(name=data['name'], parent_id=data['parent_id'])
|
||||
db.add(new_path)
|
||||
db.commit()
|
||||
session.add(new_path)
|
||||
session.commit()
|
||||
return jsonify(new_path.to_dict()), 201
|
||||
|
||||
@path_bp.route('/<int:path_id>', methods=['PUT'])
|
||||
@@ -57,30 +56,30 @@ def update_path(path_id):
|
||||
data = request.json
|
||||
if not data or 'name' not in data or 'parent_id' not in data:
|
||||
return jsonify({"error": "bad request"}), 400
|
||||
with get_db() as db:
|
||||
path = db.query(Path).get(path_id)
|
||||
with get_db() as session:
|
||||
path = session.query(Path).get(path_id)
|
||||
if path is None:
|
||||
return jsonify({"error": "path not found"}), 404
|
||||
if db.query(Path).filter_by(name=data['name'], parent_id=data['parent_id']).first():
|
||||
if session.query(Path).filter_by(name=data['name'], parent_id=data['parent_id']).first():
|
||||
return jsonify({"error": "Path already exists under the parent"}), 409
|
||||
path.name = data['name']
|
||||
path.parent_id = data['parent_id']
|
||||
db.commit()
|
||||
session.commit()
|
||||
return jsonify(path.to_dict()), 200
|
||||
|
||||
@path_bp.route('/<int:path_id>', methods=['DELETE'])
|
||||
@limiter.limit('60 per minute')
|
||||
@require_auth(roles=['admin'])
|
||||
def delete_path(path_id):
|
||||
with get_db() as db:
|
||||
path = db.query(Path).get(path_id)
|
||||
with get_db() as session:
|
||||
path = session.query(Path).get(path_id)
|
||||
if not path:
|
||||
return jsonify({"error": "path not found"}), 404
|
||||
if db.query(Path).filter_by(parent_id=path_id).first():
|
||||
if session.query(Path).filter_by(parent_id=path_id).first():
|
||||
return jsonify({"error": "can not delete non empty path"}), 409
|
||||
if db.query(Markdown).filter_by(path_id=path_id).first():
|
||||
if session.query(Markdown).filter_by(path_id=path_id).first():
|
||||
return jsonify({"error": "can not delete non empty path"}), 409
|
||||
db.delete(path)
|
||||
db.commit()
|
||||
session.delete(path)
|
||||
session.commit()
|
||||
return jsonify({"message": "path deleted"}), 200
|
||||
|
||||
|
||||
Reference in New Issue
Block a user