diff --git a/app.py b/app.py index 9c5eee1..f9d3eb7 100644 --- a/app.py +++ b/app.py @@ -136,9 +136,9 @@ def random_quote(): return render_template('random.html', quote=random_quote) -@app.route('/?') -def quote_homepathid(quote_id): - quote = Quote.query.get_or_404(quote_id) +@app.route('/') +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/') @@ -223,6 +275,7 @@ def reject(id): db.session.commit() return redirect(url_for('modapp')) + # Delete a quote (admin only) @app.route('/delete/') def delete(id): diff --git a/templates/modapp.html b/templates/modapp.html index 16f7eb7..8fbb3bb 100644 --- a/templates/modapp.html +++ b/templates/modapp.html @@ -7,6 +7,7 @@ +
@@ -33,30 +34,65 @@
+
-

Pending Quotes for Moderation

- {% for quote in pending_quotes %} -
-

#{{ quote.id }}: {{ quote.text }}

-

- - Submitted on {{ quote.date.strftime('%Y-%m-%d') }} | - Approve | - Reject | - Delete - -

-
-
- {% endfor %} +

All Quotes for Moderation

+ + + {% if all_quotes %} +
+ + + + + + + + + + {% for quote in all_quotes %} + + + + + + + + {% endfor %} +
SelectQuote IDQuoteStatusActions
#{{ quote.id }}{{ quote.text }} + {% if quote.status == 0 %} + Pending + {% elif quote.status == 1 %} + Approved + {% else %} + Rejected + {% endif %} + + Approve | + Reject | + Delete +
+ + + + +
+ {% else %} +

No quotes available for moderation at the moment.

+ {% endif %}
+