def standings_to_string(standings): response = [] mode = get_config("user_mode") account_type = get_mode_as_word() if mode == TEAMS_MODE: team_ids = [] for team in standings: team_ids.append(team.account_id) teams = Teams.query.filter(Teams.id.in_(team_ids)).all() teams = [next(t for t in teams if t.id == id) for id in team_ids] for i, x in enumerate(standings): entry = { "pos": i + 1, "account_id": x.account_id, "account_url": generate_account_url(account_id=x.account_id), "account_type": account_type, "oauth_id": x.oauth_id, "name": x.name, "score": int(x.score), } if mode == TEAMS_MODE: members = [] for member in teams[i].members: members.append({ "id": member.id, "oauth_id": member.oauth_id, "name": member.name, "score": int(member.score), }) entry["members"] = members response.append(entry) return response
def get(self): standings = get_standings() response = [] mode = get_config("user_mode") account_type = get_mode_as_word() if mode == TEAMS_MODE: r = db.session.execute( select([ Users.id, Users.name, Users.oauth_id, Users.team_id, Users.hidden, Users.banned, ]).where(Users.team_id.isnot(None))) users = r.fetchall() membership = defaultdict(dict) for u in users: if u.hidden is False and u.banned is False: membership[u.team_id][u.id] = { "id": u.id, "oauth_id": u.oauth_id, "name": u.name, "score": 0, } # Get user_standings as a dict so that we can more quickly get member scores user_standings = get_user_standings() for u in user_standings: membership[u.team_id][u.user_id]["score"] = int(u.score) for i, x in enumerate(standings): entry = { "pos": i + 1, "account_id": x.account_id, "account_url": generate_account_url(account_id=x.account_id), "account_type": account_type, "oauth_id": x.oauth_id, "name": x.name, "score": int(x.score), } if mode == TEAMS_MODE: entry["members"] = list(membership[x.account_id].values()) response.append(entry) return {"success": True, "data": response}
def get(self, challenge_id): response = [] challenge = Challenges.query.filter_by(id=challenge_id).first_or_404() # TODO: Need a generic challenge visibility call. # However, it should be stated that a solve on a gated challenge is not considered private. if challenge.state == "hidden" and is_admin() is False: abort(404) Model = get_model() # Note that we specifically query for the Solves.account.name # attribute here because it is faster than having SQLAlchemy # query for the attribute directly and it's unknown what the # affects of changing the relationship lazy attribute would be solves = ( Solves.query.add_columns(Model.name.label("account_name")) .join(Model, Solves.account_id == Model.id) .filter( Solves.challenge_id == challenge_id, Model.banned == False, Model.hidden == False, ) .order_by(Solves.date.asc()) ) freeze = get_config("freeze") if freeze: preview = request.args.get("preview") if (is_admin() is False) or (is_admin() is True and preview): dt = datetime.datetime.utcfromtimestamp(freeze) solves = solves.filter(Solves.date < dt) for solve in solves: # Seperate out the account name and the Solve object from the SQLAlchemy tuple solve, account_name = solve response.append( { "account_id": solve.account_id, "name": account_name, "date": isoformat(solve.date), "account_url": generate_account_url(account_id=solve.account_id), } ) return {"success": True, "data": response}
def get(self, challenge_id): response = [] challenge = Challenges.query.filter_by(id=challenge_id).first_or_404() # TODO: Need a generic challenge visibility call. # However, it should be stated that a solve on a gated challenge is not considered private. if challenge.state == "hidden" and is_admin() is False: abort(404) Model = get_model() solves = ( Solves.query.join(Model, Solves.account_id == Model.id) .filter( Solves.challenge_id == challenge_id, Model.banned == False, Model.hidden == False, ) .order_by(Solves.date.asc()) ) freeze = get_config("freeze") if freeze: preview = request.args.get("preview") if (is_admin() is False) or (is_admin() is True and preview): dt = datetime.datetime.utcfromtimestamp(freeze) solves = solves.filter(Solves.date < dt) for solve in solves: response.append( { "account_id": solve.account_id, "name": solve.account.name, "date": isoformat(solve.date), "account_url": generate_account_url(account_id=solve.account_id), } ) return {"success": True, "data": response}
def get(self): standings = get_standings() response = [] mode = get_config("user_mode") account_type = get_mode_as_word() if mode == TEAMS_MODE: team_ids = [] for team in standings: team_ids.append(team.account_id) # Get team objects with members explicitly loaded in teams = (Teams.query.options(joinedload(Teams.members)).filter( Teams.id.in_(team_ids)).all()) # Sort according to team_ids order teams = [next(t for t in teams if t.id == id) for id in team_ids] # Get user_standings as a dict so that we can more quickly get member scores user_standings = get_user_standings() users = {} for u in user_standings: users[u.user_id] = u for i, x in enumerate(standings): entry = { "pos": i + 1, "account_id": x.account_id, "account_url": generate_account_url(account_id=x.account_id), "account_type": account_type, "oauth_id": x.oauth_id, "name": x.name, "score": int(x.score), } if mode == TEAMS_MODE: members = [] # This code looks like it would be slow # but it is faster than accessing each member's score individually for member in teams[i].members: user = users.get(member.id) if user: members.append({ "id": user.user_id, "oauth_id": user.oauth_id, "name": user.name, "score": int(user.score), }) else: if member.hidden is False and member.banned is False: members.append({ "id": member.id, "oauth_id": member.oauth_id, "name": member.name, "score": 0, }) entry["members"] = members response.append(entry) return {"success": True, "data": response}