diff --git a/redstring/server.py b/redstring/server.py index d699bc1..f4f9513 100644 --- a/redstring/server.py +++ b/redstring/server.py @@ -85,19 +85,23 @@ def edit(document_id): doc_path = safe_join(current_app.config['root'], f'{document_id}.json') + # Load the document to edit + if not os.path.isfile(doc_path): + return abort(404) + with open(doc_path) as f: + doc: Document = load(f) + # Check for content updates if request.method == 'POST': sent_json = request.json new_doc: Document = parse_document_from_json(sent_json) with open(doc_path, 'w') as f: dump(new_doc, f) - return {} - - # Load the document to edit - if not os.path.isfile(doc_path): - return abort(404) - with open(doc_path) as f: - doc: Document = load(f) + new_id = new_doc.get_tag_value('id') + if document_id != new_id: + new_path = safe_join(current_app.config['root'], f'{new_id}.json') + os.rename(doc_path, new_path) + return { 'href': url_for('edit', document_id=new_id) } # Check for structural updates if add := request.args.get('add'): diff --git a/redstring/templates/edit.jinja b/redstring/templates/edit.jinja index 65f3f27..8de3989 100644 --- a/redstring/templates/edit.jinja +++ b/redstring/templates/edit.jinja @@ -121,7 +121,7 @@ function save() { req.responseType = "json"; req.onreadystatechange = function () { if (req.readyState == 4 && req.status == 200) { - window.location.reload(); + window.location.href = req.response.href; } }; req.send(JSON.stringify(doc));