Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
BaseMax committed Nov 30, 2024
1 parent 7f8c512 commit e9ec3c4
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 26 deletions.
42 changes: 28 additions & 14 deletions config/admin/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
app = Flask(__name__)
app.secret_key = 'your_secret_key'

PORT = 5000
YAML_DIR = '../'


def get_yaml_files() -> list[str]:
"""
Scans the YAML_DIR directory and returns a list of all YAML file paths.
Expand Down Expand Up @@ -61,9 +61,10 @@ def index() -> str:
Returns:
str: The rendered HTML template for the admin panel.
"""
error = session.pop('error', None)
message = session.pop('message', None)
message_type = session.pop('message_type', None)

return render_template('index.html', error=error, files=get_yaml_files())
return render_template('index.html', message=message, message_type=message_type, files=get_yaml_files())


@app.route('/edit/<path:filepath>', methods=['POST'])
Expand Down Expand Up @@ -107,9 +108,10 @@ def edit_file(filepath: str) -> str:

data = read_yaml(file_path)

error = session.pop('error', None)
message = session.pop('message', None)
message_type = session.pop('message_type', None)

return render_template('edit.html', error=error, filename=filepath, items=data['items'])
return render_template('edit.html', message=message, message_type=message_type, filename=filepath, items=data['items'])


@app.route('/add-file', methods=['POST'])
Expand All @@ -123,13 +125,15 @@ def add_file_action() -> str:
new_file = request.form.get('filename')

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

new_file = new_file.strip()

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

new_file = new_file.lstrip('/')
Expand All @@ -140,19 +144,25 @@ def add_file_action() -> str:
path = os.path.join(YAML_DIR, new_file)

if path.startswith(YAML_DIR + ".."):
session['error'] = 'Invalid filename or directory traversal attempt.'
session['message'] = 'Invalid filename or directory traversal attempt.'
session['message_type'] = 'error'
return redirect(url_for('index'))

if os.path.exists(path):
session['error'] = 'File already exists.'
session['message'] = 'File already exists.'
session['message_type'] = 'error'
return redirect(url_for('index'))

try:
write_yaml(path, {'items': []})

session['message'] = 'File created.'
session['message_type'] = 'ok'

return redirect(url_for('index'))
except Exception as e:
session['error'] = f'Failed to create file: {e}'
session['message'] = f'Failed to create file: {e}'
session['message_type'] = 'error'

return redirect(url_for('index'))

Expand All @@ -173,15 +183,19 @@ def delete_file_action(filepath: str) -> str:
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."
session['message'] = f"File '{filepath}' has been deleted successfully."
session['message_type'] = 'ok'
except Exception as e:
session['error'] = f"Failed to delete file: {e}"
session['message'] = f"Failed to delete file: {e}"
session['message_type'] = 'error'
else:
session['error'] = f"File '{filepath}' not found or invalid path."
session['message'] = f"File '{filepath}' not found or invalid path."
session['message_type'] = 'error'

return redirect(url_for('index'))


if __name__ == '__main__':
os.makedirs(YAML_DIR, exist_ok=True)
app.run(debug=True, port=5000)

app.run(debug=True, port=PORT)
9 changes: 8 additions & 1 deletion config/admin/templates/edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@
<body>
<div class="container mt-4">
<h1>Edit File: {{ filename }}</h1>
<table class="table table-bordered" id="data-table">

{% if message %}
<div class="alert {% if message_type == 'ok' %}alert-success{% else %}alert-danger{% endif %}" role="alert">
{{ message }}
</div>
{% endif %}

<table class="table table-bordered" id="data-table">
<thead>
<tr>
<th>ID</th>
Expand Down
10 changes: 5 additions & 5 deletions config/admin/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
<body class="container mt-5">
<h1>Salam Admin Panel</h1>

{% if error %}
<div class="alert alert-danger" role="alert">
{{ error }}
</div>
{% endif %}
{% if message %}
<div class="alert {% if message_type == 'ok' %}alert-success{% else %}alert-danger{% endif %}" role="alert">
{{ message }}
</div>
{% endif %}

<form action="{{ url_for('add_file_action') }}" method="post" class="mb-4">
<div class="input-group">
Expand Down
6 changes: 0 additions & 6 deletions config/block.yaml

This file was deleted.

0 comments on commit e9ec3c4

Please sign in to comment.