Improved bulk action handling in modapp

- Added helper functions for approving, rejecting, and deleting quotes (approve_quote, reject_quote, delete_quote)
- Implemented validation for quote selection and action type
- Added user feedback (flash messages) for successful or invalid actions
- Ensured no action is performed if no quotes are selected in bulk operations
This commit is contained in:
2024-10-11 18:49:10 +01:00
parent 918667a86a
commit 79ed713a18
2 changed files with 112 additions and 23 deletions

69
app.py
View File

@@ -136,9 +136,9 @@ def random_quote():
return render_template('random.html', quote=random_quote) return render_template('random.html', quote=random_quote)
@app.route('/?<int:id>') @app.route('/<int:id>')
def quote_homepathid(quote_id): def quote_homepathid(id):
quote = Quote.query.get_or_404(quote_id) quote = Quote.query.get_or_404(id)
return render_template('quote.html', quote=quote) return render_template('quote.html', quote=quote)
@app.route('/quote') @app.route('/quote')
@@ -175,12 +175,61 @@ def login():
# Admin panel route (accessible only to logged-in admins) # Admin panel route (accessible only to logged-in admins)
@app.route('/modapp') @app.route('/modapp')
def modapp(): def modapp():
if not session.get('admin'): # If admin not logged in, redirect to login if not session.get('admin'):
flash('You need to log in first.', 'danger') flash('You need to log in first.', 'danger')
return redirect(url_for('login')) return redirect(url_for('login'))
pending_quotes = Quote.query.filter_by(status=0).all() # Fetch all quotes (pending, approved, and rejected)
return render_template('modapp.html', pending_quotes=pending_quotes) all_quotes = Quote.query.order_by(Quote.date.desc()).all()
return render_template('modapp.html', all_quotes=all_quotes)
@app.route('/modapp/bulk_action', methods=['POST'])
def bulk_action():
action = request.form.get('action')
quote_ids = request.form.getlist('quote_ids')
if not quote_ids:
flash("No quotes selected.", "warning")
return redirect(url_for('modapp'))
valid_actions = ['approve', 'reject', 'delete']
if action not in valid_actions:
flash("Invalid action selected.", "error")
return redirect(url_for('modapp'))
if action == 'approve':
for quote_id in quote_ids:
approve_quote(quote_id)
elif action == 'reject':
for quote_id in quote_ids:
reject_quote(quote_id)
elif action == 'delete':
for quote_id in quote_ids:
delete_quote(quote_id)
flash(f"Bulk action '{action}' applied to selected quotes.", "success")
return redirect(url_for('modapp'))
# Define helper functions for each action
def approve_quote(quote_id):
quote = Quote.query.get(quote_id)
if quote:
quote.status = 1 # Approved
db.session.commit()
def reject_quote(quote_id):
quote = Quote.query.get(quote_id)
if quote:
quote.status = 2 # Rejected
db.session.commit()
def delete_quote(quote_id):
quote = Quote.query.get(quote_id)
if quote:
db.session.delete(quote)
db.session.commit()
@app.route('/search', methods=['GET']) @app.route('/search', methods=['GET'])
def search(): def search():
@@ -208,9 +257,12 @@ def approve(id):
return redirect(url_for('login')) return redirect(url_for('login'))
quote = Quote.query.get_or_404(id) quote = Quote.query.get_or_404(id)
quote.status = 1 # 1 = approved quote.status = 1
db.session.commit() db.session.commit()
return redirect(url_for('modapp'))
# Redirect back to the same page
page = request.args.get('page', 1)
return redirect(url_for('modapp', page=page))
# Reject a quote (admin only) # Reject a quote (admin only)
@app.route('/reject/<int:id>') @app.route('/reject/<int:id>')
@@ -223,6 +275,7 @@ def reject(id):
db.session.commit() db.session.commit()
return redirect(url_for('modapp')) return redirect(url_for('modapp'))
# Delete a quote (admin only) # Delete a quote (admin only)
@app.route('/delete/<int:id>') @app.route('/delete/<int:id>')
def delete(id): def delete(id):

View File

@@ -7,6 +7,7 @@
<link rel="stylesheet" href="/static/css/styles.css"> <link rel="stylesheet" href="/static/css/styles.css">
</head> </head>
<body bgcolor="#ffffff" text="#000000" link="#c08000" vlink="#c08000" alink="#c08000"> <body bgcolor="#ffffff" text="#000000" link="#c08000" vlink="#c08000" alink="#c08000">
<!-- Navigation -->
<center> <center>
<table cellpadding="2" cellspacing="0" width="80%" border="0"> <table cellpadding="2" cellspacing="0" width="80%" border="0">
<tr> <tr>
@@ -33,30 +34,65 @@
</table> </table>
</center> </center>
<!-- Main Content: All Quotes for Moderation -->
<center> <center>
<table cellpadding="2" cellspacing="0" width="80%"> <table cellpadding="2" cellspacing="0" width="80%">
<tr> <tr>
<td class="bodytext"> <td class="bodytext">
<h2>Pending Quotes for Moderation</h2> <h2>All Quotes for Moderation</h2>
{% for quote in pending_quotes %}
<div class="quote"> <!-- Check if there are any quotes -->
<p>#{{ quote.id }}: {{ quote.text }}</p> {% if all_quotes %}
<p> <form method="POST" action="/modapp/bulk_action">
<small> <table>
Submitted on {{ quote.date.strftime('%Y-%m-%d') }} | <tr>
<a href="/approve/{{ quote.id }}" class="qa">Approve</a> | <th>Select</th>
<a href="/reject/{{ quote.id }}" class="qa">Reject</a> | <th>Quote ID</th>
<a href="/delete/{{ quote.id }}" class="qa">Delete</a> <th>Quote</th>
</small> <th>Status</th>
</p> <th>Actions</th>
<hr> </tr>
</div> <!-- Loop through each quote -->
{% endfor %} {% for quote in all_quotes %}
<tr style="background-color: '{% if quote.status == 1 %}#d4edda{% elif quote.status == 2 %}#f8d7da{% else %}#fff{% endif %}'">
<td><input type="checkbox" name="quote_ids" value="{{ quote.id }}"></td>
<td>#{{ quote.id }}</td>
<td>{{ quote.text }}</td>
<td>
{% if quote.status == 0 %}
Pending
{% elif quote.status == 1 %}
Approved
{% else %}
Rejected
{% endif %}
</td>
<td>
<a href="/approve/{{ quote.id }}">Approve</a> |
<a href="/reject/{{ quote.id }}">Reject</a> |
<a href="/delete/{{ quote.id }}">Delete</a>
</td>
</tr>
{% endfor %}
</table>
<!-- Bulk Action Dropdown and Submit -->
<select name="action">
<option value="approve">Approve Selected</option>
<option value="reject">Reject Selected</option>
<option value="delete">Delete Selected</option>
</select>
<input type="submit" value="Apply">
</form>
{% else %}
<p>No quotes available for moderation at the moment.</p>
{% endif %}
</td> </td>
</tr> </tr>
</table> </table>
</center> </center>
<!-- Footer -->
<center> <center>
<table border="0" cellpadding="2" cellspacing="0" width="80%" bgcolor="#c08000"> <table border="0" cellpadding="2" cellspacing="0" width="80%" bgcolor="#c08000">
<tr> <tr>