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

Commit 465bdec

Browse files
committed
Use Built in Query Function
1 parent 220a638 commit 465bdec

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

quotefault/__init__.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from flask_migrate import Migrate
1313
from flask_pyoidc.flask_pyoidc import OIDCAuthentication
1414
from flask_sqlalchemy import SQLAlchemy
15-
from sqlalchemy import func
15+
from sqlalchemy.sql.expression import func
1616

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

188188

189-
def get_quote_query(speaker: str = "", submitter: str = "", include_hidden: bool = False):
189+
def get_quote_query(speaker: str = "", submitter: str = "", include_hidden: bool = False, order = Quote.quote_time.desc()):
190190
"""Return a query based on the args, with vote count attached to the quotes"""
191191
# Get all the quotes with their votes
192192
quote_query = db.session.query(Quote,
193193
func.sum(Vote.direction).label('votes')).outerjoin(Vote).group_by(Quote)
194194
# Put the most recent first
195-
quote_query = quote_query.order_by(Quote.quote_time.desc())
195+
quote_query = quote_query.order_by(order)
196196
# Filter hidden quotes
197197
if not include_hidden:
198198
quote_query = quote_query.filter(Quote.hidden == False)
@@ -377,8 +377,10 @@ def hidden():
377377
@app.route('/random', methods=['GET'])
378378
@auth.oidc_auth
379379
def random_quote():
380-
quote = db.session.execute("SELECT * FROM quotefault.quote WHERE hidden=0 ORDER BY RAND() LIMIT 1;").all()[0]
381-
out = f"{quote[2]} -{quote[3]} (Submitted by {quote[1]})"
380+
#quote = db.session.execute("SELECT * FROM quotefault.quote WHERE hidden=0 ORDER BY RAND() LIMIT 1;").all()[0]
381+
quote = get_quote_query(speaker = request.args.get('speaker'), \
382+
submitter = request.args.get('submitter'), order = func.random()).limit(1).all()[0][0]
383+
out = f"{quote.quote} -{quote.speaker} (Submitted by {quote.submitter})"
382384
return out, 200
383385

384386
@app.errorhandler(403)

0 commit comments

Comments
 (0)