diff --git a/wmbusmeters-ha-addon-edge/rootfs/flask/app.py b/wmbusmeters-ha-addon-edge/rootfs/flask/app.py index 9908ab3..3ff34a8 100644 --- a/wmbusmeters-ha-addon-edge/rootfs/flask/app.py +++ b/wmbusmeters-ha-addon-edge/rootfs/flask/app.py @@ -109,13 +109,12 @@ def decrypt(): else: return jsonify({'ERROR': 'Unable to extract details from file'}) -def get_ingress_path(): - referer = request.headers.get('Referer') - if referer: - parsed_url = urlparse(referer) - path_parts = parsed_url.path.split('/') - if len(path_parts) > 3 and path_parts[2] == 'api' and path_parts[3] == 'hassio_ingress': - return path_parts[4] # Extract ingress path +def extract_ingress_path_from_url(): + full_url = request.url + parsed_url = urlparse(full_url) + path_parts = parsed_url.path.split('/') + if len(path_parts) > 3 and path_parts[2] == 'api' and path_parts[3] == 'hassio_ingress': + return path_parts[4] return None @app.route('/drivers') @@ -123,39 +122,39 @@ def drivers(): files = os.listdir(DRIVER_DIRECTORY) return render_template('drivers.html', files=files) -@app.route('/edit_driver/', methods=['GET', 'POST']) -def edit_driver(filename): - filepath = os.path.join(DRIVER_DIRECTORY, filename) - ingress_path = get_ingress_path() +@app.route('/add_driver', methods=['GET', 'POST']) +def add_driver(): + ingress_path = extract_ingress_path_from_url() if request.method == 'POST': + filename = request.form['filename'] content = request.form['content'] + filepath = os.path.join(DRIVER_DIRECTORY, filename) with open(filepath, 'w') as f: f.write(content) if ingress_path: return redirect(f"/api/hassio_ingress/{ingress_path}/drivers") return redirect(url_for('drivers')) - with open(filepath, 'r') as f: - content = f.read() - return render_template('edit_driver.html', filename=filename, content=content) + return render_template('add_driver.html', filename='', content='') -@app.route('/add_driver', methods=['GET', 'POST']) -def add_driver(): - ingress_path = get_ingress_path() +@app.route('/edit_driver/', methods=['GET', 'POST']) +def edit_driver(filename): + filepath = os.path.join(DRIVER_DIRECTORY, filename) + ingress_path = extract_ingress_path_from_url() if request.method == 'POST': - filename = request.form['filename'] content = request.form['content'] - filepath = os.path.join(DRIVER_DIRECTORY, filename) with open(filepath, 'w') as f: f.write(content) if ingress_path: return redirect(f"/api/hassio_ingress/{ingress_path}/drivers") return redirect(url_for('drivers')) - return render_template('add_driver.html', filename='', content='') + with open(filepath, 'r') as f: + content = f.read() + return render_template('edit_driver.html', filename=filename, content=content) @app.route('/delete_driver/', methods=['POST']) def delete_driver(filename): filepath = os.path.join(DRIVER_DIRECTORY, filename) - ingress_path = get_ingress_path() + ingress_path = extract_ingress_path_from_url() if os.path.exists(filepath): os.remove(filepath) if ingress_path: