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:
69
app.py
69
app.py
@@ -136,9 +136,9 @@ def random_quote():
|
||||
|
||||
return render_template('random.html', quote=random_quote)
|
||||
|
||||
@app.route('/?<int:id>')
|
||||
def quote_homepathid(quote_id):
|
||||
quote = Quote.query.get_or_404(quote_id)
|
||||
@app.route('/<int:id>')
|
||||
def quote_homepathid(id):
|
||||
quote = Quote.query.get_or_404(id)
|
||||
return render_template('quote.html', quote=quote)
|
||||
|
||||
@app.route('/quote')
|
||||
@@ -175,12 +175,61 @@ def login():
|
||||
# Admin panel route (accessible only to logged-in admins)
|
||||
@app.route('/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')
|
||||
return redirect(url_for('login'))
|
||||
|
||||
pending_quotes = Quote.query.filter_by(status=0).all()
|
||||
return render_template('modapp.html', pending_quotes=pending_quotes)
|
||||
# Fetch all quotes (pending, approved, and rejected)
|
||||
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'])
|
||||
def search():
|
||||
@@ -208,9 +257,12 @@ def approve(id):
|
||||
return redirect(url_for('login'))
|
||||
|
||||
quote = Quote.query.get_or_404(id)
|
||||
quote.status = 1 # 1 = approved
|
||||
quote.status = 1
|
||||
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)
|
||||
@app.route('/reject/<int:id>')
|
||||
@@ -223,6 +275,7 @@ def reject(id):
|
||||
db.session.commit()
|
||||
return redirect(url_for('modapp'))
|
||||
|
||||
|
||||
# Delete a quote (admin only)
|
||||
@app.route('/delete/<int:id>')
|
||||
def delete(id):
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
<link rel="stylesheet" href="/static/css/styles.css">
|
||||
</head>
|
||||
<body bgcolor="#ffffff" text="#000000" link="#c08000" vlink="#c08000" alink="#c08000">
|
||||
<!-- Navigation -->
|
||||
<center>
|
||||
<table cellpadding="2" cellspacing="0" width="80%" border="0">
|
||||
<tr>
|
||||
@@ -33,30 +34,65 @@
|
||||
</table>
|
||||
</center>
|
||||
|
||||
<!-- Main Content: All Quotes for Moderation -->
|
||||
<center>
|
||||
<table cellpadding="2" cellspacing="0" width="80%">
|
||||
<tr>
|
||||
<td class="bodytext">
|
||||
<h2>Pending Quotes for Moderation</h2>
|
||||
{% for quote in pending_quotes %}
|
||||
<div class="quote">
|
||||
<p>#{{ quote.id }}: {{ quote.text }}</p>
|
||||
<p>
|
||||
<small>
|
||||
Submitted on {{ quote.date.strftime('%Y-%m-%d') }} |
|
||||
<a href="/approve/{{ quote.id }}" class="qa">Approve</a> |
|
||||
<a href="/reject/{{ quote.id }}" class="qa">Reject</a> |
|
||||
<a href="/delete/{{ quote.id }}" class="qa">Delete</a>
|
||||
</small>
|
||||
</p>
|
||||
<hr>
|
||||
</div>
|
||||
{% endfor %}
|
||||
<h2>All Quotes for Moderation</h2>
|
||||
|
||||
<!-- Check if there are any quotes -->
|
||||
{% if all_quotes %}
|
||||
<form method="POST" action="/modapp/bulk_action">
|
||||
<table>
|
||||
<tr>
|
||||
<th>Select</th>
|
||||
<th>Quote ID</th>
|
||||
<th>Quote</th>
|
||||
<th>Status</th>
|
||||
<th>Actions</th>
|
||||
</tr>
|
||||
<!-- Loop through each quote -->
|
||||
{% 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>
|
||||
</tr>
|
||||
</table>
|
||||
</center>
|
||||
|
||||
<!-- Footer -->
|
||||
<center>
|
||||
<table border="0" cellpadding="2" cellspacing="0" width="80%" bgcolor="#c08000">
|
||||
<tr>
|
||||
|
||||
Reference in New Issue
Block a user