Add delete request button to admin dashboard with confirmation and file cleanup
This commit is contained in:
parent
7ef8779eb7
commit
b8f1fee102
3 changed files with 41 additions and 2 deletions
31
app.py
31
app.py
|
|
@ -8,7 +8,7 @@ from flask import Flask, request, render_template, redirect, url_for, flash, ses
|
|||
from werkzeug.utils import secure_filename
|
||||
|
||||
from config import Config
|
||||
from models import init_db, close_db, create_request, get_request_by_id, get_request_by_token, list_requests, update_request, now_utc
|
||||
from models import init_db, close_db, create_request, get_request_by_id, get_request_by_token, list_requests, update_request, now_utc, delete_request
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config.from_object(Config)
|
||||
|
|
@ -263,6 +263,35 @@ def admin_request(rid):
|
|||
|
||||
return render_template('admin/request.html', req=req, statuses=STATUS_LABELS, file_exists=file_exists, basename=basename)
|
||||
|
||||
@app.route('/admin/request/<int:rid>/delete', methods=['POST'])
|
||||
def admin_delete_request(rid):
|
||||
redir = require_admin()
|
||||
if redir:
|
||||
return redir
|
||||
req = get_request_by_id(rid)
|
||||
if not req:
|
||||
abort(404)
|
||||
|
||||
# Delete uploaded files if they exist
|
||||
for field in ('song_a_path', 'song_b_path'):
|
||||
path = req.get(field)
|
||||
if path and Path(path).exists():
|
||||
try:
|
||||
Path(path).unlink()
|
||||
except OSError:
|
||||
pass
|
||||
# Remove empty upload directory
|
||||
upload_dir = Path(current_app.config['UPLOAD_FOLDER']) / str(rid)
|
||||
if upload_dir.exists():
|
||||
try:
|
||||
upload_dir.rmdir()
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
delete_request(rid)
|
||||
flash(f'Request #{rid} deleted.', 'success')
|
||||
return redirect(url_for('admin_dashboard'))
|
||||
|
||||
# ---------------- init ----------------
|
||||
|
||||
@app.cli.command('init-db')
|
||||
|
|
|
|||
|
|
@ -93,3 +93,8 @@ def update_request(request_id, **fields):
|
|||
vals = list(fields.values()) + [request_id]
|
||||
db.execute(f'UPDATE requests SET {cols} WHERE id = ?', vals)
|
||||
db.commit()
|
||||
|
||||
def delete_request(request_id):
|
||||
db = get_db()
|
||||
db.execute('DELETE FROM requests WHERE id = ?', (request_id,))
|
||||
db.commit()
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ tr:hover{background:#2d3748}
|
|||
.pending,.prompt_ready{background:#60a5fa;color:#000}
|
||||
.songs_uploaded{background:#a78bfa;color:#000}
|
||||
.actions a{color:#93c5fd;text-decoration:none;margin-right:.8rem}
|
||||
.actions button.delete{background:transparent;color:#f87171;border:none;padding:0;cursor:pointer;font:inherit;text-decoration:none;margin-right:.8rem}
|
||||
.logout{float:right;color:#f87171;text-decoration:none}
|
||||
.flash{padding:.8rem;background:#064e3b;border-radius:.5rem;margin-bottom:1rem}
|
||||
.flash.error{background:#450a0a}
|
||||
|
|
@ -65,7 +66,11 @@ tr:hover{background:#2d3748}
|
|||
<td>{{ r.style_genre or '-' }}</td>
|
||||
<td><span class="status-badge {{ r.status }}">{{ statuses[r.status] }}</span></td>
|
||||
<td>{% if r.customer_approved != 'none' %}{{ r.customer_approved.upper() }}{% else %}-{% endif %}</td>
|
||||
<td class="actions"><a href="{{ url_for('admin_request', rid=r.id) }}">Open</a></td>
|
||||
<td class="actions"><a href="{{ url_for('admin_request', rid=r.id) }}">Open</a>
|
||||
<form method="POST" action="{{ url_for('admin_delete_request', rid=r.id) }}" style="display:inline" onsubmit="return confirm('ARE YOU SURE you want to delete request #{{ r.id }} for {{ r.name }}? This cannot be undone.')">
|
||||
<button type="submit" class="delete">Delete</button>
|
||||
</form>
|
||||
</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
{% if not requests %}
|
||||
|
|
|
|||
Reference in a new issue