def single_result_json(id): race = Race() race_participants = RaceParticipants() race_results = RaceResult() race_query = race.get_race(id) if race_query: json = [] snails_results_list = [] race_participants_by_id = race_participants.get_race_participants_race_id( id) for row in race_participants_by_id: race_results_snail = race_results.get_race_result(row.id) snails_results_list.append({ "id_snail": row.id_snail, "position_snail": race_results_snail.position, "time_snail": race_results_snail.time_to_finish, "DNF": race_results_snail.did_not_finish }) json.append({"id_race": int(id), "snails": snails_results_list}) return json return { 'status': 'Failed', 'message': 'Results not found' }, status.HTTP_404_NOT_FOUND
def races_endpoint(): """GET end point to return races information""" auth = Auth(app) if not auth.authenticate_request(): return auth.unauthorized_response() race = Race() race_query = race.get_all_races() all_race_participants = RaceParticipants() if race_query: json = [] for each_race in race_query: race_participants = all_race_participants.get_race_participants_race_id( each_race.id) snails_id_list = [] for row in race_participants: snails_id_list.append(row.id_snail) json.append({ "id": each_race.id, "date": each_race.date, "status": each_race.status, "id_round": each_race.id_round, "id_snails": snails_id_list }) return json return { 'status': 'Failed', 'message': 'Races not found' }, status.HTTP_404_NOT_FOUND
def results_json(): race_participants = RaceParticipants() race_results = race_participants.get_race_results() races = {} if race_results: for (race_participant, race_result) in race_results: snail = ResultSnail(race_participant.id_snail, race_result.position, race_result.time_to_finish, race_result.did_not_finish) if race_participant.id_race not in races.keys(): races[race_participant.id_race] = ResultRace( race_participant.id_race) race = races[race_participant.id_race] if snail not in race.snails: race.snails.append(snail) return [race.get_json() for race in races.values()] return { 'status': 'Failed', 'message': 'Results not found' }, status.HTTP_404_NOT_FOUND
def single_race_endpoint(id): """GET end point to return a single race's information""" auth = Auth(app) if not auth.authenticate_request(): return auth.unauthorized_response() race = Race() single_race_query = race.get_race(id) single_race_participants = RaceParticipants( ).get_race_participants_race_id(id) if single_race_query: json = [] snails_id_list = [] for row in single_race_participants: snails_id_list.append(row.id_snail) json.append({ "id": single_race_query.id, "date": single_race_query.date, "status": single_race_query.status, "id_round": single_race_query.id_round, "id_snails": snails_id_list }) return json return { 'status': 'Failed', 'message': 'Race not found' }, status.HTTP_404_NOT_FOUND
def race(round_id, race_id): if not current_user.is_authenticated: return redirect(url_for('login.login')) race_results_form = RaceResultsForm() race = Race().get_race(race_id) participants = RaceParticipants().get_participants_snails_race_id(race_id) unresulted_participants = [ participant for participant, snail, result in participants if not result ] if race_results_form.validate_on_submit(): result_row = RaceResult().get_race_result( race_results_form.id_race_participants.data) if not result_row: times = [] for participant, snail, result in participants: time = RaceResult().get_time_to_finish(participant.id) if time: times.append(time[0]) time_to_finish = race_results_form.time_to_finish.data new_times = calc_new_positions(times, time_to_finish) new_result_position = update_existing_positions( participants, new_times, time_to_finish) db.session.add( RaceResult( position=new_result_position, time_to_finish=race_results_form.time_to_finish.data, did_not_finish=race_results_form.did_not_finish.data, id_race_participants=race_results_form. id_race_participants.data)) db.session.commit() flash("Race Result recorded for Race Participant ID {}.".format( race_results_form.id_race_participants.data)) else: flash("Race Result Failed. Race Result already exists for Race " "Participant ID {}.".format( race_results_form.id_race_participants.data)) return redirect( url_for('result.race', round_id=round_id, race_id=race_id)) elif race_results_form.errors.items(): flash("Form submission not valid. Please resubmit.") return redirect( url_for('result.race', round_id=round_id, race_id=race_id)) return render_template('results.html', race=race, participants=participants, unresulted_participants=unresulted_participants, race_results_form=race_results_form)
def validate_snail_in_same_race(race_id, snail_id): race_participants = RaceParticipants().get_race_participants_race_id( race_id) for snail in race_participants: if int(snail.id_snail) == int(snail_id): return True return False
def validate_snail_in_same_round(round_id, race_id, snail_id): races_in_round = Race().get_races_by_round(round_id) for race in races_in_round: race_participants = RaceParticipants().get_race_participants_race_id( race.id) for snail in race_participants: if int(snail.id_snail) == int(snail_id): return True return False
def commit_snail_to_race(id_race, id_snail): race_participant = RaceParticipants(id_race=id_race, id_snail=id_snail) db.session.add(race_participant) db.session.commit()