From a312cbcf2232223506655cc11ecdd56e667f5a1b Mon Sep 17 00:00:00 2001 From: ComputerTech312 Date: Fri, 11 Oct 2024 15:42:14 +0100 Subject: [PATCH] Random page now only displays approved quotes --- .gitignore | 2 ++ app.py | 13 ++++--------- requirements.txt | 2 ++ 3 files changed, 8 insertions(+), 9 deletions(-) create mode 100644 .gitignore create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c3d83e3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +venv +instance diff --git a/app.py b/app.py index bd13ab9..28a67dc 100644 --- a/app.py +++ b/app.py @@ -126,18 +126,13 @@ def vote(id, action): # Route for displaying a random quote @app.route('/random') def random_quote(): - count = Quote.query.count() + count = Quote.query.filter_by(status=1).count() # Only count approved quotes if count == 0: - flash("No quotes available yet.", 'error') + flash("No approved quotes available yet.", 'error') return redirect(url_for('index')) - random_id = random.randint(1, count) - random_quote = Quote.query.get(random_id) - - # If the random quote is not found (i.e., deleted), pick another random quote - while random_quote is None: - random_id = random.randint(1, count) - random_quote = Quote.query.get(random_id) + random_offset = random.randint(0, count - 1) # Generate a random offset + random_quote = Quote.query.filter_by(status=1).offset(random_offset).first() # Fetch a random approved quote return render_template('random.html', quote=random_quote) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..3c3b9fc --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +flask_sqlalchemy +argon2-cffi