From 704b8daa6d6770e0d84d782979c2c4171b560831 Mon Sep 17 00:00:00 2001 From: Leigh Morresi <275001+dgtlmoon@users.noreply.github.com> Date: Tue, 2 Feb 2021 15:37:20 +0100 Subject: [PATCH] Code cleanup edit submit handler can be the same function --- backend/backend.py | 62 ++++++++++++++++++------------------- backend/templates/base.html | 2 +- backend/templates/edit.html | 2 +- 3 files changed, 33 insertions(+), 33 deletions(-) diff --git a/backend/backend.py b/backend/backend.py index cee3fedd..8708da62 100644 --- a/backend/backend.py +++ b/backend/backend.py @@ -113,12 +113,40 @@ def main_page(): return output -@app.route("/edit", methods=['GET']) +@app.route("/edit", methods=['GET', 'POST']) def edit_page(): global messages + import validators + + if request.method == 'POST': + uuid = request.args.get('uuid') + + url = request.form.get('url').strip() + tag = request.form.get('tag').strip() + + form_headers = request.form.get('headers').strip().split("\n") + extra_headers = {} + if form_headers: + for header in form_headers: + if len(header): + parts = header.split(':', 1) + extra_headers.update({parts[0].strip(): parts[1].strip()}) + + validators.url(url) # @todo switch to prop/attr/observer + datastore.data['watching'][uuid].update({'url': url, + 'tag': tag, + 'headers': extra_headers}) + datastore.needs_write = True + + messages.append({'class': 'ok', 'message': 'Updated watch.'}) + + return redirect(url_for('main_page')) + + else: + + uuid = request.args.get('uuid') + output = render_template("edit.html", uuid=uuid, watch=datastore.data['watching'][uuid], messages=messages) - uuid = request.args.get('uuid') - output = render_template("edit.html", uuid=uuid, watch=datastore.data['watching'][uuid], messages=messages) return output @@ -264,36 +292,8 @@ def api_delete(): return redirect(url_for('main_page')) -@app.route("/api/update", methods=['POST']) -def api_update(): - global messages - import validators - - uuid = request.args.get('uuid') - - url = request.form.get('url').strip() - tag = request.form.get('tag').strip() - - form_headers = request.form.get('headers').strip().split("\n") - extra_headers = {} - if form_headers: - for header in form_headers: - if len(header): - parts = header.split(':', 1) - extra_headers.update({parts[0].strip(): parts[1].strip()}) - - validators.url(url) #@todo switch to prop/attr/observer - datastore.data['watching'][uuid].update({'url': url, - 'tag': tag, - 'headers':extra_headers}) - datastore.needs_write = True - - messages.append({'class': 'ok', 'message': 'Updated watch.'}) - - return redirect(url_for('main_page')) - @app.route("/api/checknow", methods=['GET']) def api_watch_checknow(): global messages diff --git a/backend/templates/base.html b/backend/templates/base.html index ea7293ab..d635e07f 100644 --- a/backend/templates/base.html +++ b/backend/templates/base.html @@ -19,7 +19,7 @@