diff --git a/.gitignore b/.gitignore index d7a4d04..dbfc333 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ -# Created by venv; see https://docs.python.org/3/library/venv.html +i# Created by venv; see https://docs.python.org/3/library/venv.html venv +__pycache__ diff --git a/requirements.txt b/requirements.txt index 314769f..d091c13 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,8 +3,10 @@ blinker==1.9.0 certifi==2026.2.25 charset-normalizer==3.4.6 click==8.3.1 -docker @ git+https://github.com/docker/docker-py@main +docker @ git+https://github.com/docker/docker-py@df3f8e2abc5a03de482e37214dddef9e0cee1bb1 Flask==3.1.3 +Flask-SQLAlchemy==3.1.1 +greenlet==3.4.0 idna==3.11 itsdangerous==2.2.0 Jinja2==3.1.6 @@ -15,5 +17,7 @@ pathspec==1.0.4 platformdirs==4.9.4 pytokens==0.4.1 requests==2.33.0 +SQLAlchemy==2.0.49 +typing_extensions==4.15.0 urllib3==2.6.3 Werkzeug==3.1.6 diff --git a/src/container_managment.py b/src/container_managment.py index 67259dc..53732d5 100644 --- a/src/container_managment.py +++ b/src/container_managment.py @@ -40,7 +40,7 @@ def delete_container(docker_id) -> bool: container = client.containers.get(docker_id) container.remove() return True - except docker.errors.NotFound, docker.errors.APIError: + except (docker.errors.NotFound, docker.errors.APIError): return False @@ -50,7 +50,7 @@ def stop_container(docker_id) -> bool: container = client.containers.get(docker_id) container.kill() return True - except docker.errors.NotFound, docker.errors.APIError: + except (docker.errors.NotFound, docker.errors.APIError): return False @@ -66,7 +66,7 @@ def start_foundry_container(table: GameTable, key: KeyTable): if table.docker_id != None and table.docker_id != 0: try: container = client.containers.get(table.docker_id) - except docker.errors.NotFound, docker.errors.NullResource: + except (docker.errors.NotFound, docker.errors.NullResource): container = None if container: diff --git a/src/database.py b/src/database.py index f47057a..1f9b7f6 100644 --- a/src/database.py +++ b/src/database.py @@ -33,11 +33,11 @@ CREATE TABLE IF NOT EXISTS "game_keys" ( """ _user_table_create = """ -CREATE TABLE IF NOT EXISTS "" ( - "id": INTEGER NOT NULL, - "username": TEXT NOT NULL, - "hash": TEXT NOT NULL, - PRIMARY KEY ("ID) +CREATE TABLE IF NOT EXISTS "users" ( + "id" INTEGER NOT NULL, + "username" TEXT NOT NULL, + "hash" TEXT NOT NULL, + PRIMARY KEY ("id") ); """ @@ -55,7 +55,7 @@ class SmartCursor: curosr: Cursor autoClose: bool - def __init__(self, cursor: Cursor = None, connection: Connectoin = None): + def __init__(self, cursor: Cursor = None, connection: Connection = None): self.cursor = cursor self.connection = connection self.autoClose = False diff --git a/src/game_table_model.py b/src/game_table_model.py new file mode 100644 index 0000000..aad81eb --- /dev/null +++ b/src/game_table_model.py @@ -0,0 +1,25 @@ +from main import db + + +class GameTable(db.Model): + + __tablename__ = "game_tables" + + game_table_id = db.Column(db.Integer, primary_key=True) + game_table_name = db.Column(db.String(30)) + game_table_link = db.Column(db.String(30), unique=True) + active = db.Column(db.Boolean) + version = db.Column(db.Integer) + docker_id = db.Column(db.Integer) + + def __repr__(self): + return f"" + + def to_dict(self): + return { + "table_id": self.game_table_id, + "table_name": self.game_table_name, + "table_link": self.game_table_link, + "active": self.active, + "version": self.version, + } diff --git a/src/key_tables.py b/src/key_tables.py index 88bc48b..867692c 100644 --- a/src/key_tables.py +++ b/src/key_tables.py @@ -4,7 +4,7 @@ from database import SmartCursor class KeyTable: key: str - game_table_id: number + game_table_id: int key_file: str created: bool updated: bool diff --git a/src/main.py b/src/main.py index bd301f6..a1dd0f5 100644 --- a/src/main.py +++ b/src/main.py @@ -2,19 +2,27 @@ from flask import Flask, g, jsonify, request import sqlite3 import container_managment from database import SmartCursor, get_db, init_db -import tables import os +from flask_sqlalchemy import SQLAlchemy + +db = SQLAlchemy() def create_app(): app = Flask(__name__) + app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:////data/tables.db" + + db.init_app(app) + @app.teardown_appcontext def close_connection(exception): db = getattr(g, "_database", None) if db is not None: db.close() + import tables + app.register_blueprint(tables.tables, url_prefix="/api") with app.app_context(): @@ -24,4 +32,4 @@ def create_app(): if __name__ == "__main__": - create_app.run(debug=True) + create_app().run(debug=True) diff --git a/src/tables.py b/src/tables.py index 8a289ab..391779e 100644 --- a/src/tables.py +++ b/src/tables.py @@ -4,63 +4,49 @@ from key_tables import KeyService, KeyTable import container_managment import random from database import get_db, SmartCursor +from game_table_model import GameTable +import game_table_model +from main import db as potato tables = Blueprint("tables_api", __name__) @tables.route("/tables", methods=["POST"]) def create_table(): - db = get_db() - with SmartCursor(connection=db) as smartCursor: - table = GameTable( - game_table_id=0, - game_table_name=request.get_json()["table_name"], - game_table_link=request.get_json()["table_link"], - version=request.get_json()["version"], - active=False, - docker_id=None, - ) - table.commit(smartCursor) - db.commit() - return jsonify(table.toJson()), 200 + table = GameTable( + game_table_id=0, + game_table_name=request.get_json()["table_name"], + game_table_link=request.get_json()["table_link"], + version=request.get_json()["version"], + active=False, + docker_id=None, + ) + potato.session.add(table) + potato.session.commit() + return jsonify(table.to_dict()) @tables.route("/tables/") def get_table(table_id): - gameService = GameService(get_db()) - table = gameService.read_by_id(table_id) - return jsonify(table.toJson()) + table = GameTable.query.get_or_404(table_id) + current_app.logger.info(table) + return jsonify(table.to_dict()) @tables.route("/tables/", methods=["POST"]) def update_table(table_id): - db = get_db() - gameService = GameService(db) - table = gameService.read_by_id(table_id) + table = GameTable.query.get_or_404(table_id) table.game_table_name = request.get_json()["table_name"] table.game_table_link = request.get_json()["table_link"] table.version = request.get_json()["version"] - with SmartCursor(connection=db) as smartCursor: - table.commit(smartCursor) - db.commit() - return table.toJson(), 200 + potato.session.commit() @tables.route("tables/", methods=["DELETE"]) def delete_table(table_id): - db = get_db() - gameService = GameService(db) - table = gameService.read_by_id(table_id) - if table == None: - return jsonify({}), 404 - - container_managment.delete_container(table.docker_id) - - table.delete() - with SmartCursor(connection=db) as smartCursor: - table.commit(smartCursor) - db.commit() - return jsonify({}), 200 + table = GameTable.query.get_or_404(table_id) + potato.session.delete(table) + potato.session.commit() @tables.route("/tables/active") diff --git a/src/users.py b/src/users.py index 707e389..e7c15d1 100644 --- a/src/users.py +++ b/src/users.py @@ -59,7 +59,7 @@ class UserService: return [] def check_login(username: str, password: str): - return; + return users = Blueprint("users_api", __name__)