def young_stars(event='WMTBOC', place=None): model = Results(mysql) at_last_one_participation = model.get_participation_years(event) result = {} for competitor_id, participation_nr in at_last_one_participation: if place: place = place if place <= 3 else 3 res = model.get_first_medal(competitor_id, event.upper(), place) else: res = model.get_first_medal(competitor_id, event.upper(), 1) if res: try: born = int(COMPETITORS[competitor_id]['born'].split('-')[0]) except ValueError: born = 0 except AttributeError: born = 0 if born: age = res[0][1] - born result[competitor_id] = [age, res] result = sorted(result.items(), key=lambda kv: kv[1][0]) result = [res for res in result if res[1][0] <= 23] title = "Young stars on {}".format(tools.EVENT_NAMES[event.upper()]) if place: disclaimer = "Competitors who got a {} medal in age 24 or younger.".format( tools.EVENT_NAMES[event.upper()]) else: disclaimer = "Competitors who got their first {} medal before becoming 24.".format( tools.EVENT_NAMES[event.upper()]) return flask.render_template('youngstars.html', title=title, disclaimer=disclaimer, table_data=result, place=place, medal_names=MEDAL_NAMES, competitors=COMPETITORS, flags=tools.IOC_INDEX)
def competitor(competitor_id): model = Results(mysql) try: current = COMPETITORS[int(competitor_id)] except KeyError: flask.abort(404) data = model.get_competitor_results(competitor_id) data.sort(key=operator.itemgetter(2)) data = [tools.format_competitor_row(row, RACES) for row in data] distances = list({row['dist'] for row in data}) medal_table = tools.prepare_medal_table(model, competitor_id) relay_medal_table = tools.prepare_medal_table(model, competitor_id, "relay") my_wmtboc = model.get_event_competitor_participation( competitor_id, 'WMTBOC') my_emtboc = model.get_event_competitor_participation( competitor_id, 'EMTBOC') first_medals = { 'medal_wmtboc': model.get_first_medal(competitor_id, 'WMTBOC'), 'medal_emtboc': model.get_first_medal(competitor_id, 'EMTBOC'), 'title_wmtboc': model.get_first_medal(competitor_id, 'WMTBOC', 1), 'title_emtboc': model.get_first_medal(competitor_id, 'EMTBOC', 1), 'relay_medal_wmtboc': model.get_first_medal(competitor_id, 'WMTBOC', table="relay"), 'relay_medal_emtboc': model.get_first_medal(competitor_id, 'EMTBOC', table="relay"), 'relay_title_wmtboc': model.get_first_medal(competitor_id, 'WMTBOC', 1, table="relay"), 'relay_title_emtboc': model.get_first_medal(competitor_id, 'EMTBOC', 1, table="relay") } title = "{} {}".format(current['first'], current['last']) try: birth = current['born'].split('-')[0] except AttributeError: birth = None return flask.render_template('competitor.html', title=title, birth=birth, medal_table=medal_table, relay_medal_table=relay_medal_table, wmtboc_total=WMTBOC_NR, wmtboc_competitor=len(my_wmtboc), wmtbo_years=", ".join(my_wmtboc), emtboc_total=EMTBOC_NR, emtboc_competitor=len(my_emtboc), emtbo_years=", ".join(my_emtboc), first_medals=first_medals, medal_names=MEDAL_NAMES, races=RACES, competitor=current, data=data, distances=distances, flags=tools.IOC_INDEX)