Skip to content

Commit

Permalink
Update app.py
Browse files Browse the repository at this point in the history
  • Loading branch information
BIBOLV committed Dec 15, 2024
1 parent b0e922f commit 79bd52d
Showing 1 changed file with 20 additions and 21 deletions.
41 changes: 20 additions & 21 deletions wmbusmeters-ha-addon-edge/rootfs/flask/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,53 +109,52 @@ 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')
def drivers():
files = os.listdir(DRIVER_DIRECTORY)
return render_template('drivers.html', files=files)

@app.route('/edit_driver/<filename>', 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/<filename>', 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/<filename>', 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:
Expand Down

0 comments on commit 79bd52d

Please sign in to comment.