From 70b7b2d950b62939034eaa34c605beb8159edd51 Mon Sep 17 00:00:00 2001 From: Karan Jayachandra Date: Fri, 25 Oct 2024 18:07:43 +0200 Subject: Added a basic pyproject --- pyproject.toml | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 pyproject.toml (limited to 'pyproject.toml') diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..cae5554 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,18 @@ +[project] +name = "match_up" +version = "0.1" +authors = [ + { name="Karan Jayachandra", email="mail@karanjayachandra.com" }, +] +description = "A small web application for matchmaking" +readme = "README.md" +requires-python = ">=3.10" +classifiers = [ + "Programming Language :: Python :: 3", + "License :: OSI Approved :: MIT License", + "Operating System :: OS Independent", +] + +[project.urls] +Homepage = "https://gitlab.com/KaranJayachandra/match_up" +Issues = "https://gitlab.com/KaranJayachandra/match_up/-/issues" \ No newline at end of file -- cgit v1.3.1 From 2a390e99e03fe538169aa6d7c23669eaa5ece467 Mon Sep 17 00:00:00 2001 From: Karan Jayachandra Date: Fri, 25 Oct 2024 18:24:13 +0200 Subject: Added the requirements on the static files --- pyproject.toml | 19 +++++++++- src/match_up/__init__.py | 93 ++++++++++++++++++++++++++++++++++++++++++++++ src/match_up/app.py | 90 -------------------------------------------- src/match_up/controller.py | 4 +- 4 files changed, 112 insertions(+), 94 deletions(-) create mode 100644 src/match_up/__init__.py delete mode 100644 src/match_up/app.py (limited to 'pyproject.toml') diff --git a/pyproject.toml b/pyproject.toml index cae5554..9ef21d8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,10 @@ +[build-system] +requires = ["setuptools>=64", "setuptools_scm>=8"] +build-backend = "setuptools.build_meta" + [project] name = "match_up" -version = "0.1" +dynamic = ["version"] authors = [ { name="Karan Jayachandra", email="mail@karanjayachandra.com" }, ] @@ -15,4 +19,15 @@ classifiers = [ [project.urls] Homepage = "https://gitlab.com/KaranJayachandra/match_up" -Issues = "https://gitlab.com/KaranJayachandra/match_up/-/issues" \ No newline at end of file +Issues = "https://gitlab.com/KaranJayachandra/match_up/-/issues" + + +[tool.setuptools.packages.find] +where = ["src"] + +[tool.setuptools.package-data] +"*" = ["*.txt"] +mypkg1 = ["data1.rst"] + +[project.scripts] +match_up = "match_up:main" \ No newline at end of file diff --git a/src/match_up/__init__.py b/src/match_up/__init__.py new file mode 100644 index 0000000..badf3f0 --- /dev/null +++ b/src/match_up/__init__.py @@ -0,0 +1,93 @@ +from dotenv import load_dotenv +from match_up.controller import RoundGenerator +from match_up.model import CourtList, PlayerList +from jinja2 import FileSystemLoader, Environment +from flask import Flask, render_template, request + + +load_dotenv() +app = Flask("Match Up! Backend") +rg = RoundGenerator(courts=CourtList(), players=PlayerList()) +re = Environment(loader=FileSystemLoader("templates")) + + +@app.route("/", methods=["GET"]) +def home(): + return render_template("index.j2") + + +@app.route("/time", methods=["GET"]) +def get_time(): + mins, secs = rg.timer.get() + t = re.from_string('{% from "macros.j2" import timer %}{{timer(mins, secs)}}') + return render_template(t, mins=mins, secs=secs) + + +@app.route("/control", methods=["GET"]) +def get_controls(): + return render_template("controls.j2", courts=rg.courts.get_courts()) + + +@app.route("/guests", methods=["GET"]) +def get_guests(): + t = re.from_string('{% from "macros.j2" import guests %}{{guests(count)}}') + return render_template(t, count=rg.players.guests) + + +@app.route("/decrement", methods=["POST"]) +def increment_guests(): + t = re.from_string('{% from "macros.j2" import guests %}{{guests(count)}}') + return render_template(t, count=rg.players.decrement_guests()) + + +@app.route("/increment", methods=["POST"]) +def decrement_guests(): + t = re.from_string('{% from "macros.j2" import guests %}{{guests(count)}}') + return render_template(t, count=rg.players.increment_guests()) + + +@app.route("/propose", methods=["POST"]) +def get_random_games(): + levels = int(request.form["type"].split()[0]) + return render_template("games.j2", games=rg.propose(levels), title="Proposal") + + +@app.route("/confirm", methods=["POST"]) +def confirm_games(): + return render_template("games.j2", games=rg.confirm(), title=f"Round: {rg.round}") + + +@app.route("/clear", methods=["POST"]) +def clear_games(): + return render_template("games.j2", games=rg.clear(), title=f"Round: {rg.round}") + + +@app.route("/player-toggle/", methods=["POST"]) +def toggle_player(player_request): + id = int(player_request) + status, name = rg.players.toggle_player_status(id) + t = re.from_string( + '{% from "macros.j2" import player %}{{player(id, status, name)}}' + ) + return render_template(t, id=id, status=status, name=name) + + +@app.route("/players", methods=["GET"]) +def get_list_of_players(): + return render_template("players.j2", players=rg.players.get_players()) + + +@app.route("/court-toggle/", methods=["POST"]) +def toggle_court(court_number): + id = int(court_number) + status = rg.courts.toggle_court_status(id) + t = re.from_string('{% from "macros.j2" import court %}{{ court(id, status) }}') + return render_template(t, id=id, status=status) + + +@app.route("/courts", methods=["GET"]) +def get_list_of_courts(): + return render_template("courts.j2", courts=rg.courts.get_courts()) + +def main(): + app.run() diff --git a/src/match_up/app.py b/src/match_up/app.py deleted file mode 100644 index cb790f3..0000000 --- a/src/match_up/app.py +++ /dev/null @@ -1,90 +0,0 @@ -from dotenv import load_dotenv -from controller import RoundGenerator -from model import CourtList, PlayerList -from jinja2 import FileSystemLoader, Environment -from flask import Flask, render_template, request - - -load_dotenv() -app = Flask("Match Up! Backend") -rg = RoundGenerator(courts=CourtList(), players=PlayerList()) -re = Environment(loader=FileSystemLoader("templates")) - - -@app.route("/", methods=["GET"]) -def home(): - return render_template("index.j2") - - -@app.route("/time", methods=["GET"]) -def get_time(): - mins, secs = rg.timer.get() - t = re.from_string('{% from "macros.j2" import timer %}{{timer(mins, secs)}}') - return render_template(t, mins=mins, secs=secs) - - -@app.route("/control", methods=["GET"]) -def get_controls(): - return render_template("controls.j2", courts=rg.courts.get_courts()) - - -@app.route("/guests", methods=["GET"]) -def get_guests(): - t = re.from_string('{% from "macros.j2" import guests %}{{guests(count)}}') - return render_template(t, count=rg.players.guests) - - -@app.route("/decrement", methods=["POST"]) -def increment_guests(): - t = re.from_string('{% from "macros.j2" import guests %}{{guests(count)}}') - return render_template(t, count=rg.players.decrement_guests()) - - -@app.route("/increment", methods=["POST"]) -def decrement_guests(): - t = re.from_string('{% from "macros.j2" import guests %}{{guests(count)}}') - return render_template(t, count=rg.players.increment_guests()) - - -@app.route("/propose", methods=["POST"]) -def get_random_games(): - levels = int(request.form["type"].split()[0]) - return render_template("games.j2", games=rg.propose(levels), title="Proposal") - - -@app.route("/confirm", methods=["POST"]) -def confirm_games(): - return render_template("games.j2", games=rg.confirm(), title=f"Round: {rg.round}") - - -@app.route("/clear", methods=["POST"]) -def clear_games(): - return render_template("games.j2", games=rg.clear(), title=f"Round: {rg.round}") - - -@app.route("/player-toggle/", methods=["POST"]) -def toggle_player(player_request): - id = int(player_request) - status, name = rg.players.toggle_player_status(id) - t = re.from_string( - '{% from "macros.j2" import player %}{{player(id, status, name)}}' - ) - return render_template(t, id=id, status=status, name=name) - - -@app.route("/players", methods=["GET"]) -def get_list_of_players(): - return render_template("players.j2", players=rg.players.get_players()) - - -@app.route("/court-toggle/", methods=["POST"]) -def toggle_court(court_number): - id = int(court_number) - status = rg.courts.toggle_court_status(id) - t = re.from_string('{% from "macros.j2" import court %}{{ court(id, status) }}') - return render_template(t, id=id, status=status) - - -@app.route("/courts", methods=["GET"]) -def get_list_of_courts(): - return render_template("courts.j2", courts=rg.courts.get_courts()) diff --git a/src/match_up/controller.py b/src/match_up/controller.py index e6dc827..2f8bfaf 100644 --- a/src/match_up/controller.py +++ b/src/match_up/controller.py @@ -1,8 +1,8 @@ from typing import Tuple from operator import attrgetter from dataclasses import dataclass -from utilities import sample_list, Timer -from model import Team, Game, PlayerList, CourtList, MAX_LEVEL, PLAYER_PER_COURT +from match_up.utilities import sample_list, Timer +from match_up.model import Team, Game, PlayerList, CourtList, MAX_LEVEL, PLAYER_PER_COURT def _create_placeholders(courts: dict, name: str) -> list[Game]: -- cgit v1.3.1 From 5b01707a04c362ced66428bef8e0234176d212d9 Mon Sep 17 00:00:00 2001 From: Karan Jayachandra Date: Fri, 8 Nov 2024 17:30:19 +0100 Subject: Added the changes to the templates to pass the url_for function --- pyproject.toml | 4 ++-- src/match_up/__init__.py | 20 +++++++++++++------- src/match_up/templates/index.j2 | 4 ++-- src/match_up/templates/macros.j2 | 4 ++-- src/match_up/templates/players.j2 | 4 ++-- 5 files changed, 21 insertions(+), 15 deletions(-) (limited to 'pyproject.toml') diff --git a/pyproject.toml b/pyproject.toml index 9ef21d8..9e8a793 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -26,8 +26,8 @@ Issues = "https://gitlab.com/KaranJayachandra/match_up/-/issues" where = ["src"] [tool.setuptools.package-data] -"*" = ["*.txt"] -mypkg1 = ["data1.rst"] +"templates" = ["*.j2"] +"static" = ["*.css", "*.js", "*.png", "*.ico"] [project.scripts] match_up = "match_up:main" \ No newline at end of file diff --git a/src/match_up/__init__.py b/src/match_up/__init__.py index badf3f0..3a78b8d 100644 --- a/src/match_up/__init__.py +++ b/src/match_up/__init__.py @@ -1,19 +1,24 @@ from dotenv import load_dotenv +from importlib.resources import path from match_up.controller import RoundGenerator from match_up.model import CourtList, PlayerList -from jinja2 import FileSystemLoader, Environment -from flask import Flask, render_template, request - +from jinja2 import FileSystemLoader, Environment, PackageLoader +from flask import Flask, render_template, request, url_for load_dotenv() -app = Flask("Match Up! Backend") +with path("match_up", "static") as p: + static_folder = p +with path("match_up", "templates") as p: + template_folder = p +app = Flask("Match Up! Backend", static_folder=static_folder, template_folder=template_folder) rg = RoundGenerator(courts=CourtList(), players=PlayerList()) -re = Environment(loader=FileSystemLoader("templates")) +re = Environment(loader=PackageLoader("match_up")) @app.route("/", methods=["GET"]) def home(): - return render_template("index.j2") + template = re.get_template("index.j2").render(url_for=url_for) + return template @app.route("/time", methods=["GET"]) @@ -74,7 +79,8 @@ def toggle_player(player_request): @app.route("/players", methods=["GET"]) def get_list_of_players(): - return render_template("players.j2", players=rg.players.get_players()) + template = re.get_template("players.j2").render(players=rg.players.get_players(), url_for=url_for) + return template @app.route("/court-toggle/", methods=["POST"]) diff --git a/src/match_up/templates/index.j2 b/src/match_up/templates/index.j2 index 832dbfb..ce0d531 100644 --- a/src/match_up/templates/index.j2 +++ b/src/match_up/templates/index.j2 @@ -1,10 +1,10 @@ {% from "macros.j2" import header, footer, navbar %} - {{ header() }} + {{ header(url_for) }}
- {{ navbar() }} + {{ navbar(url_for) }}
{{ footer() }} diff --git a/src/match_up/templates/macros.j2 b/src/match_up/templates/macros.j2 index 8d05bb0..dedbf48 100644 --- a/src/match_up/templates/macros.j2 +++ b/src/match_up/templates/macros.j2 @@ -22,7 +22,7 @@ {%- endmacro %} -{% macro header() -%} +{% macro header(url_for) -%} @@ -38,7 +38,7 @@
Made by Karan using Flask, HTMX and Bulma
{%- endmacro %} -{% macro navbar() -%} +{% macro navbar(url_for) -%}