def get(self, count): response = {} standings = get_standings(count=count) team_ids = [team.account_id for team in standings] solves = Solves.query.filter(Solves.account_id.in_(team_ids)) awards = Awards.query.filter(Awards.account_id.in_(team_ids)) freeze = get_config('freeze') if freeze: solves = solves.filter(Solves.date < unix_time_to_utc(freeze)) awards = awards.filter(Awards.date < unix_time_to_utc(freeze)) solves = solves.all() awards = awards.all() for i, team in enumerate(team_ids): response[i + 1] = { 'id': standings[i].account_id, 'name': standings[i].name, 'solves': [] } for solve in solves: if solve.account_id == team: response[i + 1]['solves'].append({ 'challenge_id': solve.challenge_id, 'account_id': solve.account_id, 'team_id': solve.team_id, 'user_id': solve.user_id, 'value': solve.challenge.value, 'date': isoformat(solve.date) }) for award in awards: if award.account_id == team: response[i + 1]['solves'].append({ 'challenge_id': None, 'account_id': award.account_id, 'team_id': award.team_id, 'user_id': award.user_id, 'value': award.value, 'date': isoformat(award.date) }) response[i + 1]['solves'] = sorted(response[i + 1]['solves'], key=lambda k: k['date']) 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()) endpoint = None if get_config('user_mode') == TEAMS_MODE: endpoint = 'teams.public' arg = 'team_id' elif get_config('user_mode') == USERS_MODE: endpoint = 'users.public' arg = 'user_id' for solve in solves: response.append({ 'account_id': solve.account_id, 'name': solve.account.name, 'date': isoformat(solve.date), 'account_url': url_for(endpoint, **{arg: 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()) for solve in solves: response.append({ 'account_id': solve.account_id, 'name': solve.account.name, 'date': isoformat(solve.date) }) return { 'success': True, 'data': response }
def format_variables(content): ctf_name = get_config("ctf_name") ctf_description = get_config("ctf_description") ctf_start = get_config("start") if ctf_start: ctf_start = isoformat(unix_time_to_utc(int(ctf_start))) ctf_end = get_config("end") if ctf_end: ctf_end = isoformat(unix_time_to_utc(int(ctf_end))) ctf_freeze = get_config("freeze") if ctf_freeze: ctf_freeze = isoformat(unix_time_to_utc(int(ctf_freeze))) content = safe_format( content, ctf_name=ctf_name, ctf_description=ctf_description, ctf_start=ctf_start, ctf_end=ctf_end, ctf_freeze=ctf_freeze, ) return content
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) endpoint = None if get_config("user_mode") == TEAMS_MODE: endpoint = "teams.public" arg = "team_id" elif get_config("user_mode") == USERS_MODE: endpoint = "users.public" arg = "user_id" for solve in solves: response.append({ "account_id": solve.account_id, "name": solve.account.name, "date": isoformat(solve.date), "account_url": url_for(endpoint, **{arg: 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() # 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 = get_user_challenge_by_id(challenge_id) # 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, count): response = {} standings = get_standings(count=count) team_ids = [team.account_id for team in standings] solves = Solves.query.filter(Solves.account_id.in_(team_ids)) awards = Awards.query.filter(Awards.account_id.in_(team_ids)) freeze = get_config("freeze") if freeze: solves = solves.filter(Solves.date < unix_time_to_utc(freeze)) awards = awards.filter(Awards.date < unix_time_to_utc(freeze)) solves = solves.all() awards = awards.all() for i, team in enumerate(team_ids): response[i + 1] = { "id": standings[i].account_id, "name": standings[i].name, "solves": [], } for solve in solves: if solve.account_id == team: response[i + 1]["solves"].append({ "challenge_id": solve.challenge_id, "account_id": solve.account_id, "team_id": solve.team_id, "user_id": solve.user_id, "value": solve.challenge.value, "date": isoformat(solve.date), }) for award in awards: if award.account_id == team: response[i + 1]["solves"].append({ "challenge_id": None, "account_id": award.account_id, "team_id": award.team_id, "user_id": award.user_id, "value": award.value, "date": isoformat(award.date), }) response[i + 1]["solves"] = sorted(response[i + 1]["solves"], key=lambda k: k["date"]) return {"success": True, "data": response}
def get(self, count): response = {} standings = get_standings(count=count) team_ids = [team.account_id for team in standings] solves = Solves.query.filter(Solves.account_id.in_(team_ids)) awards = Awards.query.filter(Awards.account_id.in_(team_ids)) freeze = get_config("freeze") if freeze: solves = solves.filter(Solves.date < unix_time_to_utc(freeze)) awards = awards.filter(Awards.date < unix_time_to_utc(freeze)) solves = solves.all() awards = awards.all() # Build a mapping of accounts to their solves and awards solves_mapper = defaultdict(list) for solve in solves: solves_mapper[solve.account_id].append({ "challenge_id": solve.challenge_id, "account_id": solve.account_id, "team_id": solve.team_id, "user_id": solve.user_id, "value": solve.challenge.value, "date": isoformat(solve.date), }) for award in awards: solves_mapper[award.account_id].append({ "challenge_id": None, "account_id": award.account_id, "team_id": award.team_id, "user_id": award.user_id, "value": award.value, "date": isoformat(award.date), }) # Sort all solves by date for team_id in solves_mapper: solves_mapper[team_id] = sorted(solves_mapper[team_id], key=lambda k: k["date"]) for i, team in enumerate(team_ids): response[i + 1] = { "id": standings[i].account_id, "name": standings[i].name, "solves": solves_mapper.get(standings[i].account_id, []), } return {"success": True, "data": response}
def get(self, count): mode = get_config("user_mode") team_ids = session.get('teams_watching') if team_ids == None: team_ids = [] response = { "places_matched": {}, "places_unmatched": {}, "places_custom": {} } if is_admin(): matched_standings = get_matched_standings(admin=True, count=count) unmatched_standings = get_unmatched_standings(admin=True, count=count) custom_standings = get_custom_standings(team_ids=team_ids, admin=True, count=count) else: matched_standings = get_matched_standings(count=count) unmatched_standings = get_unmatched_standings(count=count) custom_standings = get_custom_standings(team_ids=team_ids, count=count) matched_team_ids = [team.account_id for team in matched_standings] unmatched_team_ids = [team.account_id for team in unmatched_standings] custom_team_ids = [team.account_id for team in custom_standings] matched_solves = Solves.query.filter( Solves.account_id.in_(matched_team_ids)) matched_awards = Awards.query.filter( Awards.account_id.in_(matched_team_ids)) unmatched_solves = Solves.query.filter( Solves.account_id.in_(unmatched_team_ids)) unmatched_awards = Awards.query.filter( Awards.account_id.in_(unmatched_team_ids)) custom_solves = Solves.query.filter( Solves.account_id.in_(custom_team_ids)) custom_awards = Awards.query.filter( Awards.account_id.in_(custom_team_ids)) freeze = get_config('freeze') if freeze: matched_solves = matched_solves.filter( Solves.date < unix_time_to_utc(freeze)) matched_awards = matched_awards.filter( Awards.date < unix_time_to_utc(freeze)) unmatched_solves = unmatched_solves.filter( Solves.date < unix_time_to_utc(freeze)) unmatched_awards = unmatched_awards.filter( Awards.date < unix_time_to_utc(freeze)) custom_solves = custom_solves.filter( Solves.date < unix_time_to_utc(freeze)) custom_awards = custom_awards.filter( Awards.date < unix_time_to_utc(freeze)) matched_solves = matched_solves.all() matched_awards = matched_awards.all() unmatched_solves = unmatched_solves.all() unmatched_awards = unmatched_awards.all() custom_solves = custom_solves.all() custom_awards = custom_awards.all() for i, team in enumerate(matched_team_ids): response['places_matched'][i + 1] = { 'id': matched_standings[i].account_id, 'name': matched_standings[i].name, 'solves': [] } for solve in matched_solves: if solve.account_id == team: response['places_matched'][i + 1]['solves'].append({ 'challenge_id': solve.challenge_id, 'account_id': solve.account_id, 'team_id': solve.team_id, 'user_id': solve.user_id, 'value': solve.challenge.value, 'date': isoformat(solve.date) }) for award in matched_awards: if award.account_id == team: response['places_matched'][i + 1]['solves'].append({ 'challenge_id': None, 'account_id': award.account_id, 'team_id': award.team_id, 'user_id': award.user_id, 'value': award.value, 'date': isoformat(award.date) }) response['places_matched'][i + 1]['solves'] = sorted( response['places_matched'][i + 1]['solves'], key=lambda k: k['date']) for i, team in enumerate(unmatched_team_ids): response['places_unmatched'][i + 1] = { 'id': unmatched_standings[i].account_id, 'name': unmatched_standings[i].name, 'solves': [] } for solve in unmatched_solves: if solve.account_id == team: response['places_unmatched'][i + 1]['solves'].append({ 'challenge_id': solve.challenge_id, 'account_id': solve.account_id, 'team_id': solve.team_id, 'user_id': solve.user_id, 'value': solve.challenge.value, 'date': isoformat(solve.date) }) for award in unmatched_awards: if award.account_id == team: response['places_unmatched'][i + 1]['solves'].append({ 'challenge_id': None, 'account_id': award.account_id, 'team_id': award.team_id, 'user_id': award.user_id, 'value': award.value, 'date': isoformat(award.date) }) response['places_unmatched'][i + 1]['solves'] = sorted( response['places_unmatched'][i + 1]['solves'], key=lambda k: k['date']) for i, team in enumerate(custom_team_ids): response['places_custom'][i + 1] = { 'id': custom_standings[i].account_id, 'name': custom_standings[i].name, 'solves': [] } for solve in custom_solves: if solve.account_id == team: response['places_custom'][i + 1]['solves'].append({ 'challenge_id': solve.challenge_id, 'account_id': solve.account_id, 'team_id': solve.team_id, 'user_id': solve.user_id, 'value': solve.challenge.value, 'date': isoformat(solve.date) }) for award in custom_awards: if award.account_id == team: response['places_custom'][i + 1]['solves'].append({ 'challenge_id': None, 'account_id': award.account_id, 'team_id': award.team_id, 'user_id': award.user_id, 'value': award.value, 'date': isoformat(award.date) }) response['places_custom'][i + 1]['solves'] = sorted( response['places_custom'][i + 1]['solves'], key=lambda k: k['date']) return {'success': True, 'data': response}
def get(self, count): response = {} standings = get_standings(count=count) team_ids = [team.account_id for team in standings] solves = Solves.query.filter(Solves.account_id.in_(team_ids)) awards = Awards.query.filter(Awards.account_id.in_(team_ids)) hints_name_list = db.session.query( db.func.concat("Hint ", Hints.id).label("hints_name")).count() if hints_name_list > 0: hints_name = db.func.concat("Hint ", Hints.id).label("hints_name") awards = db.session.query( Awards.account_id.label('account_id'), Awards.team_id.label('team_id'), Awards.user_id.label('user_id'), Awards.value.label('value'), Awards.date.label('date'), ) \ .join(Hints, Awards.name == hints_name) \ .join(Solves, (Awards.account_id == Solves.account_id) & (Hints.challenge_id == Solves.challenge_id)) \ .filter(Awards.value != 0) \ .filter(Awards.account_id.in_(team_ids)) awards_by_admin = db.session.query( Awards.account_id.label('account_id'), Awards.team_id.label('team_id'), Awards.user_id.label('user_id'), Awards.value.label('value'), Awards.date.label('date'), ) \ .filter(Awards.account_id.in_(team_ids)) \ .filter(Awards.value > 0) awards = awards.union(awards_by_admin) freeze = get_config('freeze') if freeze: solves = solves.filter(Solves.date < unix_time_to_utc(freeze)) awards = awards.filter(Awards.date < unix_time_to_utc(freeze)) solves = solves.all() awards = awards.all() for i, team in enumerate(team_ids): response[i + 1] = { 'id': standings[i].account_id, 'name': standings[i].name, 'solves': [] } for solve in solves: if solve.account_id == team: response[i + 1]['solves'].append({ 'challenge_id': solve.challenge_id, 'account_id': solve.account_id, 'team_id': solve.team_id, 'user_id': solve.user_id, 'value': solve.challenge.value, 'date': isoformat(solve.date) }) for award in awards: if award.account_id == team: response[i + 1]['solves'].append({ 'challenge_id': None, 'account_id': award.account_id, 'team_id': award.team_id, 'user_id': award.user_id, 'value': award.value, 'date': isoformat(award.date) }) response[i + 1]['solves'] = sorted(response[i + 1]['solves'], key=lambda k: k['date']) return {'success': True, 'data': response}