Initial tansition to SQLAlchemy on the Game Table resource
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -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
|
||||
|
||||
25
src/game_table_model.py
Normal file
25
src/game_table_model.py
Normal file
@@ -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"<Game Table {self.game_table_name}>"
|
||||
|
||||
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,
|
||||
}
|
||||
@@ -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
|
||||
|
||||
12
src/main.py
12
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)
|
||||
|
||||
@@ -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/<table_id>")
|
||||
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/<int:table_id>", 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/<table_id>", 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")
|
||||
|
||||
@@ -59,7 +59,7 @@ class UserService:
|
||||
return []
|
||||
|
||||
def check_login(username: str, password: str):
|
||||
return;
|
||||
return
|
||||
|
||||
|
||||
users = Blueprint("users_api", __name__)
|
||||
|
||||
Reference in New Issue
Block a user