diff options
| author | Karan Jayachandra <mail@karanjayachandra.com> | 2024-10-14 08:51:59 +0000 |
|---|---|---|
| committer | Karan Jayachandra <mail@karanjayachandra.com> | 2024-10-14 08:51:59 +0000 |
| commit | eac95cc2e259badf3a8c3309e6e7be97d5cbd868 (patch) | |
| tree | 492b1497cf648e08e84a35dea2d217a57643dab0 /app.py | |
| parent | 2b264abdf62822936e3510b0f9690ba1e359a9b8 (diff) | |
| parent | f80463a6437533deef1d85ffa31cf0cc5ed8e454 (diff) | |
Merge branch 'pure' into 'develop'
Dash to Flask HTML Conversion
See merge request KaranJayachandra/match_up!1
Diffstat (limited to 'app.py')
| -rw-r--r-- | app.py | 130 |
1 files changed, 89 insertions, 41 deletions
@@ -1,55 +1,103 @@ -from dash import Dash -from pathlib import PurePath -from importlib.resources import files -from match_up.model import PlaySession -from match_up.view import MainLayout -from match_up.tests import ( - TEST_DATABASE_LOCATION, -) -from dash_bootstrap_components.themes import ZEPHYR -from flask import Flask, render_template +from os import getenv +from dotenv import load_dotenv +from flask import Flask, abort, render_template +from model import CourtList, PlayerList, GameList -DATABASE_LOCATION = "storage.json" +load_dotenv() -# class Application: -# def __init__(self, debug=False): -# self._debug = debug -# if debug: -# database_location = TEST_DATABASE_LOCATION -# else: -# database_location = DATABASE_LOCATION -# self._session = PlaySession(PurePath(database_location)) -# self._dashboard = Dash("Match Up!", external_stylesheets=[ZEPHYR]) -# self._layout = MainLayout( -# self._session.data.get_players(), self._session.get_matches() -# ) -# self._dashboard.layout = self._layout.get() +PLAYER_LIST = PlayerList(location="test_data_large.csv") +COURT_LIST = CourtList(12) +GAME_GENERATOR = GameList() +GAME_GENERATOR.clear(COURT_LIST) -# def run(self): -# self._dashboard.run(debug=self._debug) +app = Flask("Match Up! Backend") +env_config = getenv("PROD_APP_SETTINGS", "config.DevelopmentConfig") +app.config.from_object(env_config) -# def main(): -# a = Application(debug=True) -# a.run() +@app.route("/", methods=["GET"]) +def home(): + return render_template("index.j2") -app = Flask("Match Up") -session = PlaySession(PurePath("test_storage.json")) -base_template_path = "match_up.templates" +@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("/") -def home(): - print(files(base_template_path).joinpath("index.html").name) + +@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( - "index.html", - player_list=session.data.get_players(), - game_list=session.get_matches(), + "games.j2", + games=GAME_GENERATOR.games, ) -if __name__ == "__main__": - print("Starting app") - app.run(host="localhost", debug=True, port=80) +@app.route("/game-list", methods=["GET"]) +def get_list_of_games(): + return render_template( + "games.j2", + games=GAME_GENERATOR.games, + ) + + +@app.route("/player-toggle/<player_request>", 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/<court_number>", 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, + ) |
