Skip to content

Commit

Permalink
working on app.py
Browse files Browse the repository at this point in the history
  • Loading branch information
BaseMax committed Nov 30, 2024
1 parent 5225d71 commit b73bf94
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
25 changes: 25 additions & 0 deletions config/admin/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ def add_file():
session['error'] = 'Filename is required.'
return redirect(url_for('index'))

new_file = new_file.strip()

if new_file == "":
session['error'] = 'Filename is required.'
return redirect(url_for('index'))

new_file = new_file.lstrip('/')

if not new_file.endswith('.yaml'):
Expand Down Expand Up @@ -79,6 +85,25 @@ def redirect_with_error(error_message):
"""
return redirect(url_for('index', error=error_message))


@app.route('/delete-file/<path:filepath>', methods=['POST'])
def delete_file(filepath):
# Construct the full file path
full_path = os.path.join(YAML_DIR, filepath)

# Check if the file exists and is within the allowed directory
if os.path.exists(full_path) and os.path.abspath(full_path).startswith(os.path.abspath(YAML_DIR)):
try:
os.remove(full_path)
session['error'] = f"File '{filepath}' has been deleted successfully."
except Exception as e:
session['error'] = f"Failed to delete file: {e}"
else:
session['error'] = f"File '{filepath}' not found or invalid path."

return redirect(url_for('index'))


if __name__ == '__main__':
os.makedirs(YAML_DIR, exist_ok=True)
app.run(debug=True, port=5000)
16 changes: 11 additions & 5 deletions config/admin/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,29 @@
<body class="container mt-5">
<h1>Salam Admin Panel</h1>

{% if request.args.get('error') %}
<!-- Display Error Message -->
{% if error %}
<div class="alert alert-danger" role="alert">
{{ request.args.get('error') }}
{{ error }}
</div>
{% endif %}

<!-- Form to Add New YAML File -->
<form action="{{ url_for('add_file') }}" method="post" class="mb-4">
<div class="input-group">
<input type="text" name="filename" class="form-control" placeholder="Enter new filename">
<input type="text" name="filename" class="form-control" placeholder="Enter new filename (e.g., myfile.yaml)">
<button type="submit" class="btn btn-primary">Add File</button>
</div>
</form>

<!-- List of YAML Files -->
<ul class="list-group">
{% for file in files %}
<li class="list-group-item">
<a href="{{ url_for('edit_file', filepath=file) }}">{{ file }}</a>
<li class="list-group-item d-flex justify-content-between align-items-center">
<a href="{{ url_for('edit_file', filepath=file) }}" class="text-decoration-none">{{ file }}</a>
<form action="{{ url_for('delete_file', filepath=file) }}" method="post" class="m-0">
<button type="submit" class="btn btn-danger btn-sm">Delete</button>
</form>
</li>
{% endfor %}
</ul>
Expand Down

0 comments on commit b73bf94

Please sign in to comment.