only allow page creation if parent exists

This commit is contained in:
evilchili 2025-10-04 09:00:50 -07:00
parent c9927656ce
commit 3854a877bf

View File

@ -16,7 +16,7 @@ def relative_uri(path: str = ""):
def get_parent(table: str, uri: str): def get_parent(table: str, uri: str):
try: try:
parent_uri = uri.strip("/").split("/", -1)[0] parent_uri = uri.strip("/").rsplit("/", 1)[0]
except IndexError: except IndexError:
return None return None
@ -84,7 +84,8 @@ def index():
@app.web.route(f"{app.config.VIEW_URI}/<path:table>/<path:path>", methods=["GET"]) @app.web.route(f"{app.config.VIEW_URI}/<path:table>/<path:path>", methods=["GET"])
@app.web.route(f"{app.config.VIEW_URI}/<path:path>", methods=["GET"], defaults={'table': 'Page'}) @app.web.route(f"{app.config.VIEW_URI}/<path:path>", methods=["GET"], defaults={'table': 'Page'})
def view(table, path): def view(table, path):
return rendered(get_page(request.path, table=table, create_okay=True)) parent = get_parent(table, relative_uri())
return rendered(get_page(request.path, table=table, create_okay=parent.doc_id is not None))
@app.web.route(f"{app.config.VIEW_URI}/<path:table>/<path:path>", methods=["POST"]) @app.web.route(f"{app.config.VIEW_URI}/<path:table>/<path:path>", methods=["POST"])
@ -93,7 +94,7 @@ def edit(table, path):
uri = relative_uri() uri = relative_uri()
parent = get_parent(table, uri) parent = get_parent(table, uri)
if not parent: if not parent:
return Response(f"Parent for {uri} does not exist.", status=403) return Response("You cannot create a page at this location.", status=403)
# get or create the docoument at this uri # get or create the docoument at this uri
page = get_page(uri, table=table, create_okay=True) page = get_page(uri, table=table, create_okay=True)