Fix needing a url change when id is changed

This commit is contained in:
Tim Van Baak 2021-02-18 23:20:54 -08:00
parent 2605aa05a1
commit e5ea4f871c
2 changed files with 12 additions and 8 deletions

View File

@ -85,19 +85,23 @@ def edit(document_id):
doc_path = safe_join(current_app.config['root'], f'{document_id}.json') 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 # Check for content updates
if request.method == 'POST': if request.method == 'POST':
sent_json = request.json sent_json = request.json
new_doc: Document = parse_document_from_json(sent_json) new_doc: Document = parse_document_from_json(sent_json)
with open(doc_path, 'w') as f: with open(doc_path, 'w') as f:
dump(new_doc, f) dump(new_doc, f)
return {} new_id = new_doc.get_tag_value('id')
if document_id != new_id:
# Load the document to edit new_path = safe_join(current_app.config['root'], f'{new_id}.json')
if not os.path.isfile(doc_path): os.rename(doc_path, new_path)
return abort(404) return { 'href': url_for('edit', document_id=new_id) }
with open(doc_path) as f:
doc: Document = load(f)
# Check for structural updates # Check for structural updates
if add := request.args.get('add'): if add := request.args.get('add'):

View File

@ -121,7 +121,7 @@ function save() {
req.responseType = "json"; req.responseType = "json";
req.onreadystatechange = function () { req.onreadystatechange = function () {
if (req.readyState == 4 && req.status == 200) { if (req.readyState == 4 && req.status == 200) {
window.location.reload(); window.location.href = req.response.href;
} }
}; };
req.send(JSON.stringify(doc)); req.send(JSON.stringify(doc));