Skip to content
This repository was archived by the owner on Feb 3, 2024. It is now read-only.

Add Random Quote Route #66

Merged
merged 5 commits into from
Jun 15, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions quotefault/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from flask_migrate import Migrate
from flask_pyoidc.flask_pyoidc import OIDCAuthentication
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import func
from sqlalchemy.sql.expression import func

app = Flask(__name__)
# look for a config file to associate with a db/port/ip/servername
Expand Down Expand Up @@ -186,13 +186,13 @@ def submit():
), 200


def get_quote_query(speaker: str = "", submitter: str = "", include_hidden: bool = False):
def get_quote_query(speaker: str = "", submitter: str = "", include_hidden: bool = False, order = Quote.quote_time.desc()):
"""Return a query based on the args, with vote count attached to the quotes"""
# Get all the quotes with their votes
quote_query = db.session.query(Quote,
func.sum(Vote.direction).label('votes')).outerjoin(Vote).group_by(Quote)
# Put the most recent first
quote_query = quote_query.order_by(Quote.quote_time.desc())
quote_query = quote_query.order_by(order)
# Filter hidden quotes
if not include_hidden:
quote_query = quote_query.filter(Quote.hidden == False)
Expand Down Expand Up @@ -374,6 +374,14 @@ def hidden():
metadata=metadata
)

@app.route('/random', methods=['GET'])
@auth.oidc_auth
def random_quote():
quote = get_quote_query(speaker = request.args.get('speaker'), \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you shouldn't need the slash, not within parentheses

submitter = request.args.get('submitter'), order = func.random()).limit(1).all()[0][0]
out = f"{quote.quote} -{quote.speaker} (Submitted by {quote.submitter})"
return out, 200

@app.errorhandler(403)
def forbidden(e):
return render_template('bootstrap/403.html', metadata=get_metadata()), 403
Expand Down