From ea7d82b1bb663c0b4c7952212442bc2cc18c6342 Mon Sep 17 00:00:00 2001 From: itdominator <1itdominator@gmail.com> Date: Sat, 23 Sep 2023 00:12:36 -0500 Subject: [PATCH] Converted to using SqlModel as wrapper around SqlAlchemy --- .gitignore | 2 ++ README.md | 7 ++++--- requirements.txt | 4 ++++ src/__builtins__.py | 10 +++------- src/utils/models.py | 30 ++++++++++++++++-------------- 5 files changed, 29 insertions(+), 24 deletions(-) create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore index b6e4761..d4b4e51 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +database.db + # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] diff --git a/README.md b/README.md index 4896ce9..de5bb36 100644 --- a/README.md +++ b/README.md @@ -2,9 +2,10 @@ A template project for Python with Gtk applications. ### Requirements -* PyGObject -* setproctitle -* pyxdg +* PyGObject (Gtk introspection library) +* pyxdg (Desktop ".desktop" file parser) +* setproctitle (Define process title to search and kill more easily) +* sqlmodel (SQL databases and is powered by Pydantic and SQLAlchemy) ### Note There are a "\" strings and files that need to be set according to your app's name located at: diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..4f65d82 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +PyGObject +pyxdg +setproctitle +sqlmodel diff --git a/src/__builtins__.py b/src/__builtins__.py index c4c3d59..60e9b5d 100644 --- a/src/__builtins__.py +++ b/src/__builtins__.py @@ -5,6 +5,7 @@ import threading # Lib imports # Application imports +from utils.models import engine from utils.event_system import EventSystem from utils.endpoint_registry import EndpointRegistry from utils.keybindings import Keybindings @@ -33,6 +34,8 @@ def daemon_threaded_wrapper(fn): # NOTE: Just reminding myself we can add to builtins two different ways... # __builtins__.update({"event_system": Builtins()}) builtins.app_name = "" +builtins.db = engine + builtins.keybindings = Keybindings() builtins.event_system = EventSystem() builtins.endpoint_registry = EndpointRegistry() @@ -48,10 +51,3 @@ builtins.logger = Logger(settings_manager.get_home_config_path(), \ builtins.threaded = threaded_wrapper builtins.daemon_threaded = daemon_threaded_wrapper builtins.event_sleep_time = 0.05 - - -try: - from utils.models import _db - builtins.db = _db -except ModuleNotFoundError as e: - logger.debug("Warning: Likely Flask SQLAlchemy not installed...") diff --git a/src/utils/models.py b/src/utils/models.py index 730fcbe..2f6100b 100644 --- a/src/utils/models.py +++ b/src/utils/models.py @@ -1,23 +1,25 @@ # Python imports +from typing import Optional # Lib imports -from flask_sqlalchemy import SQLAlchemy +from sqlmodel import Field, Session, SQLModel, create_engine -# Apoplication imports +# Application imports -_db = SQLAlchemy() + +class User(SQLModel, table = True): + id: Optional[int] = Field(default = None, primary_key = True) + name: str + password: str + email: Optional[str] = None -class User(_db.Model): - email = _db.Column(_db.Text()) - username = _db.Column(_db.Text()) - password = _db.Column(_db.Text()) - id = _db.Column(_db.Integer, primary_key=True, - unique=True, autoincrement=True) +# NOTE: for sake of example we create an admin user with no password set. +user = User(name = "Admin", password = "", email = "admin@domain.com") +engine = create_engine("sqlite:///database.db") +SQLModel.metadata.create_all(engine) - def __repr__(self): - return f"'{self.email}', '{self.username}', '{self.password}', '{self.id}'" - - -_db.create_all() +with Session(engine) as session: + session.add(user) + session.commit()