Add a raw_query parameter to the standings functions

This commit is contained in:
Kevin Chung
2020-07-21 17:19:51 -04:00
parent 04280eda32
commit 732193734f

View File

@@ -8,7 +8,7 @@ from CTFd.utils.modes import get_model
@cache.memoize(timeout=60) @cache.memoize(timeout=60)
def get_standings(count=None, admin=False, fields=[]): def get_standings(count=None, admin=False, fields=[], raw_query=False):
""" """
Get standings as a list of tuples containing account_id, name, and score e.g. [(account_id, team_name, score)]. Get standings as a list of tuples containing account_id, name, and score e.g. [(account_id, team_name, score)].
@@ -108,16 +108,20 @@ def get_standings(count=None, admin=False, fields=[]):
""" """
Only select a certain amount of users if asked. Only select a certain amount of users if asked.
""" """
if count is None: if count:
standings = standings_query.all() standings = standings_query.limit(count)
else:
standings = standings_query.limit(count).all()
return standings """
Return the raw query if requested
"""
if raw_query:
return standings
return standings.all()
@cache.memoize(timeout=60) @cache.memoize(timeout=60)
def get_team_standings(count=None, admin=False, fields=[]): def get_team_standings(count=None, admin=False, fields=[], raw_query=False):
scores = ( scores = (
db.session.query( db.session.query(
Solves.team_id.label("team_id"), Solves.team_id.label("team_id"),
@@ -188,16 +192,17 @@ def get_team_standings(count=None, admin=False, fields=[]):
.order_by(sumscores.columns.score.desc(), sumscores.columns.id) .order_by(sumscores.columns.score.desc(), sumscores.columns.id)
) )
if count is None: if count:
standings = standings_query.all() standings = standings_query.limit(count)
else:
standings = standings_query.limit(count).all()
return standings if raw_query:
return standings
return standings.all()
@cache.memoize(timeout=60) @cache.memoize(timeout=60)
def get_user_standings(count=None, admin=False, fields=[]): def get_user_standings(count=None, admin=False, fields=[], raw_query=False):
scores = ( scores = (
db.session.query( db.session.query(
Solves.user_id.label("user_id"), Solves.user_id.label("user_id"),
@@ -267,9 +272,10 @@ def get_user_standings(count=None, admin=False, fields=[]):
.order_by(sumscores.columns.score.desc(), sumscores.columns.id) .order_by(sumscores.columns.score.desc(), sumscores.columns.id)
) )
if count is None: if count:
standings = standings_query.all() standings = standings_query.limit(count)
else:
standings = standings_query.limit(count).all()
return standings if raw_query:
return standings
return standings.all()