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

Commit a29ba0a

Browse files
authored
Merge pull request #66 from jabbate19/random-quote
Add Random Quote Route
2 parents 5e718f4 + 0196cf4 commit a29ba0a

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

quotefault/__init__.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from flask_pyoidc.flask_pyoidc import OIDCAuthentication
1919
from flask_pyoidc.provider_configuration import ProviderConfiguration, ClientMetadata
2020
from flask_sqlalchemy import SQLAlchemy
21-
from sqlalchemy import func
21+
from sqlalchemy.sql.expression import func
2222

2323
app = Flask(__name__)
2424
# look for a config file to associate with a db/port/ip/servername
@@ -198,13 +198,13 @@ def submit():
198198
), 200
199199

200200

201-
def get_quote_query(speaker: str = "", submitter: str = "", include_hidden: bool = False):
201+
def get_quote_query(speaker: str = "", submitter: str = "", include_hidden: bool = False, order = Quote.quote_time.desc()):
202202
"""Return a query based on the args, with vote count attached to the quotes"""
203203
# Get all the quotes with their votes
204204
quote_query = db.session.query(Quote,
205205
func.sum(Vote.direction).label('votes')).outerjoin(Vote).group_by(Quote)
206206
# Put the most recent first
207-
quote_query = quote_query.order_by(Quote.quote_time.desc())
207+
quote_query = quote_query.order_by(order)
208208
# Filter hidden quotes
209209
if not include_hidden:
210210
quote_query = quote_query.filter(Quote.hidden == False)
@@ -426,6 +426,14 @@ def hidden():
426426
metadata=metadata
427427
)
428428

429+
@app.route('/random', methods=['GET'])
430+
@auth.oidc_auth
431+
def random_quote():
432+
quote = get_quote_query(speaker = request.args.get('speaker'), \
433+
submitter = request.args.get('submitter'), order = func.random()).limit(1).all()[0][0]
434+
out = f"{quote.quote} -{quote.speaker} (Submitted by {quote.submitter})"
435+
return out, 200
436+
429437
@app.errorhandler(403)
430438
def forbidden(e):
431439
return render_template('bootstrap/403.html', metadata=get_metadata()), 403

0 commit comments

Comments
 (0)