from os import getenv from dotenv import load_dotenv from flask import Flask, abort, render_template from model import CourtList, PlayerList, GameList load_dotenv() PLAYER_LIST = PlayerList(location="test_data_large.csv") COURT_LIST = CourtList(12) GAME_GENERATOR = GameList() GAME_GENERATOR.clear(COURT_LIST) app = Flask("Match Up! Backend") env_config = getenv("PROD_APP_SETTINGS", "config.DevelopmentConfig") app.config.from_object(env_config) @app.route("/", methods=["GET"]) def home(): return render_template("index.j2") @app.route("/random-games", methods=["POST"]) def get_random_games(): print("Creating new games...") GAME_GENERATOR.create_random_games(COURT_LIST, PLAYER_LIST) return render_template( "games.j2", games=GAME_GENERATOR.games, ) @app.route("/skilled-games", methods=["POST"]) def get_skilled_games(): print("Creating new games...") GAME_GENERATOR.create_skilled_games(COURT_LIST, PLAYER_LIST) return render_template( "games.j2", games=GAME_GENERATOR.games, ) @app.route("/clear-games", methods=["POST"]) def clear_games(): print("Clearing games...") GAME_GENERATOR.clear(COURT_LIST) return render_template( "games.j2", games=GAME_GENERATOR.games, ) @app.route("/game-list", methods=["GET"]) def get_list_of_games(): return render_template( "games.j2", games=GAME_GENERATOR.games, ) @app.route("/player-toggle/", methods=["POST"]) def toggle_player(player_request): selection = PLAYER_LIST.get_player(player_request) if len(selection) != 1: abort(404) PLAYER_LIST.toggle_player(player_request) selection = PLAYER_LIST.get_player(player_request) return render_template( "player.j2", player=selection[0], ) @app.route("/player-list", methods=["GET"]) def get_list_of_players(): return render_template( "players.j2", players=PLAYER_LIST.players, ) @app.route("/court-toggle/", methods=["POST"]) def toggle_court(court_number): court_number = int(court_number) selection = COURT_LIST.get_court(court_number) if len(selection) != 1: abort(404, f"Multiple courts requested: {len(selection)}") COURT_LIST.toggle_court(court_number) selection = COURT_LIST.get_court(court_number) return render_template( "court.j2", court=selection[0], ) @app.route("/court-list", methods=["GET"]) def get_list_of_courts(): return render_template( "courts.j2", courts=COURT_LIST.courts, )