[UPD] Added api to create Library
This commit is contained in:
1
.python-version
Normal file
1
.python-version
Normal file
@@ -0,0 +1 @@
|
|||||||
|
3.12.3
|
||||||
@@ -1,12 +1,17 @@
|
|||||||
from sqlalchemy import create_engine
|
from sqlalchemy import create_engine
|
||||||
from sqlalchemy.orm import Session
|
|
||||||
|
|
||||||
from ...schema.library.base import Base
|
from ...schema.library.base import Base
|
||||||
from ...db.config.config import get_engine_configuration
|
from ...db.config.config import get_engine_configuration
|
||||||
|
|
||||||
|
import logging
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
def install():
|
def install():
|
||||||
|
logger.info("Installing")
|
||||||
engine_string, echo = get_engine_configuration()
|
engine_string, echo = get_engine_configuration()
|
||||||
|
logger.debug(f"engine_string: {engine_string}")
|
||||||
engine = create_engine(engine_string, echo=echo=="true", future=True) # TODO
|
engine = create_engine(engine_string, echo=echo=="true", future=True) # TODO
|
||||||
metadata = Base.metadata
|
metadata = Base.metadata
|
||||||
metadata.create_all(engine)
|
metadata.create_all(engine)
|
||||||
|
logger.info("Installed")
|
||||||
return engine
|
return engine
|
||||||
@@ -1,3 +1,8 @@
|
|||||||
|
__version__ = "0.2.0.dev"
|
||||||
|
|
||||||
from flask import Flask
|
from flask import Flask
|
||||||
|
|
||||||
|
from .routes.api import api
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
app.register_blueprint(api)
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
import configparser
|
import configparser
|
||||||
|
|
||||||
from .defaults import default_db_query
|
from .defaults import default_db_query
|
||||||
|
from .defaults import default_app_port, default_app_debug
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
@@ -8,9 +9,28 @@ logger = logging.getLogger(__name__)
|
|||||||
config = configparser.ConfigParser()
|
config = configparser.ConfigParser()
|
||||||
config.read("config.ini")
|
config.read("config.ini")
|
||||||
logger.debug(f"config: {config.sections()}")
|
logger.debug(f"config: {config.sections()}")
|
||||||
if not "DataBase" in config:
|
|
||||||
config["DataBase"] = default_db_query
|
|
||||||
|
|
||||||
def save_config():
|
def save_config():
|
||||||
with open("config.ini", "w") as f:
|
with open("config.ini", "w") as f:
|
||||||
f.write(config)
|
config.write(f)
|
||||||
|
|
||||||
|
def check_config():
|
||||||
|
save = False
|
||||||
|
if not "DataBase" in config:
|
||||||
|
logger.debug("DataBase not found in Config")
|
||||||
|
config["DataBase"] = {
|
||||||
|
"query": default_db_query
|
||||||
|
}
|
||||||
|
save = True
|
||||||
|
if not "App" in config:
|
||||||
|
logger.debug("App not found in Config")
|
||||||
|
|
||||||
|
config["App"] = {
|
||||||
|
"port": default_app_port,
|
||||||
|
"debug": default_app_debug
|
||||||
|
}
|
||||||
|
save = True
|
||||||
|
if save: save_config()
|
||||||
|
|
||||||
|
check_config()
|
||||||
@@ -1 +1,3 @@
|
|||||||
default_db_query = "sqlite:///library.db"
|
default_db_query = "sqlite:///library.db"
|
||||||
|
default_app_port = 15012
|
||||||
|
default_app_debug = False
|
||||||
@@ -26,6 +26,13 @@ class LibraryController:
|
|||||||
def __del__(self):
|
def __del__(self):
|
||||||
self.session.close()
|
self.session.close()
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __exit__(self, *exc):
|
||||||
|
del(self)
|
||||||
|
return False
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def data(self):
|
def data(self):
|
||||||
return self._library
|
return self._library
|
||||||
|
|||||||
1
app/routes/__init__.py
Normal file
1
app/routes/__init__.py
Normal file
@@ -0,0 +1 @@
|
|||||||
|
from .api import api
|
||||||
6
app/routes/api/__init__.py
Normal file
6
app/routes/api/__init__.py
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
from .blueprint import api
|
||||||
|
from .install import post_install
|
||||||
|
|
||||||
|
from .library import api_library
|
||||||
|
|
||||||
|
api.register_blueprint(api_library)
|
||||||
3
app/routes/api/blueprint.py
Normal file
3
app/routes/api/blueprint.py
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
from flask import Blueprint
|
||||||
|
|
||||||
|
api = Blueprint("api", __name__, url_prefix="/api")
|
||||||
24
app/routes/api/install.py
Normal file
24
app/routes/api/install.py
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
from flask import request
|
||||||
|
|
||||||
|
from ...config import config, save_config
|
||||||
|
from ...api.actions.install import install
|
||||||
|
|
||||||
|
import logging
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
from .blueprint import api
|
||||||
|
|
||||||
|
@api.route("/install", methods=["POST"])
|
||||||
|
def post_install():
|
||||||
|
try:
|
||||||
|
body = request.json
|
||||||
|
except:
|
||||||
|
logger.debug("Installing with config.ini params")
|
||||||
|
else:
|
||||||
|
if body.get("query_string"):
|
||||||
|
config["DataBase"]["query"] = body.get("query_string")
|
||||||
|
save_config()
|
||||||
|
finally:
|
||||||
|
install()
|
||||||
|
return { "status": "ok" }, 200
|
||||||
|
|
||||||
2
app/routes/api/library/__init__.py
Normal file
2
app/routes/api/library/__init__.py
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
from .blueprint import api_library
|
||||||
|
from .create import create_library
|
||||||
3
app/routes/api/library/blueprint.py
Normal file
3
app/routes/api/library/blueprint.py
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
from flask import Blueprint
|
||||||
|
|
||||||
|
api_library = Blueprint("api_library", __name__, url_prefix="/library")
|
||||||
28
app/routes/api/library/create.py
Normal file
28
app/routes/api/library/create.py
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
from flask import request
|
||||||
|
from sqlalchemy.exc import IntegrityError
|
||||||
|
|
||||||
|
from .blueprint import api_library
|
||||||
|
|
||||||
|
from ....controller import LibraryController
|
||||||
|
from ....schema.library.library import Library
|
||||||
|
|
||||||
|
import logging
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@api_library.route("/", methods=["POST"])
|
||||||
|
def create_library():
|
||||||
|
try:
|
||||||
|
data = request.json
|
||||||
|
except Exception as e:
|
||||||
|
logger.debug(f"{e}")
|
||||||
|
return { "status": "error", "error": "JSON Required" }, 415
|
||||||
|
|
||||||
|
with LibraryController() as controller:
|
||||||
|
try:
|
||||||
|
lib = Library(**data)
|
||||||
|
library = controller.create(lib)
|
||||||
|
except IntegrityError as e:
|
||||||
|
logger.debug(f"DB Error Creating {e}")
|
||||||
|
return { "status": "error", "error": f"{e.orig}" }, 400
|
||||||
|
else:
|
||||||
|
return { "status": "ok", "result": library.data.to_dict() }, 200
|
||||||
@@ -10,8 +10,8 @@ from .base import Base
|
|||||||
class Library(Base):
|
class Library(Base):
|
||||||
__tablename__ = "library"
|
__tablename__ = "library"
|
||||||
id: Mapped[int] = mapped_column(primary_key=True)
|
id: Mapped[int] = mapped_column(primary_key=True)
|
||||||
name: Mapped[str] = mapped_column(String(255))
|
name: Mapped[str] = mapped_column(String(255), unique=True)
|
||||||
notes: Mapped[str] = mapped_column(String(65656))
|
notes: Mapped[Optional[str]] = mapped_column(String(65656))
|
||||||
|
|
||||||
envs: Mapped[List["Env"]] = relationship(
|
envs: Mapped[List["Env"]] = relationship(
|
||||||
back_populates="library", cascade="all, delete-orphan"
|
back_populates="library", cascade="all, delete-orphan"
|
||||||
@@ -29,5 +29,12 @@ class Library(Base):
|
|||||||
back_populates="library", cascade="all, delete-orphan"
|
back_populates="library", cascade="all, delete-orphan"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def to_dict(self) -> dict:
|
||||||
|
return {
|
||||||
|
"id": self.id,
|
||||||
|
"name": self.name,
|
||||||
|
"notes": self.notes
|
||||||
|
}
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
return f"Library(id={self.id!r}, name={self.name!r}, notes={self.notes!r})"
|
return f"Library(id={self.id!r}, name={self.name!r}, notes={self.notes!r})"
|
||||||
@@ -1,2 +1,7 @@
|
|||||||
[DataBase]
|
[DataBase]
|
||||||
query="sqlite:///library.db"
|
query = sqlite:///test.db
|
||||||
|
|
||||||
|
[App]
|
||||||
|
port = 15012
|
||||||
|
debug = False
|
||||||
|
|
||||||
|
|||||||
14
main.py
14
main.py
@@ -0,0 +1,14 @@
|
|||||||
|
__version__ = "0.2.0.dev"
|
||||||
|
|
||||||
|
from app import app
|
||||||
|
from app.config import config
|
||||||
|
|
||||||
|
import logging
|
||||||
|
level = config["App"].get("debug", False) and logging.DEBUG or logging.INFO
|
||||||
|
logging.basicConfig(level=level)
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
logger.info(f"Logging level set to {level}")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
app.run(port=config["App"]["port"], debug=config["App"]["debug"])
|
||||||
Reference in New Issue
Block a user