Converted to using SqlModel as wrapper around SqlAlchemy

This commit is contained in:
itdominator 2023-09-23 00:12:36 -05:00
parent 4b4eac8f7d
commit ea7d82b1bb
5 changed files with 29 additions and 24 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
database.db
# Byte-compiled / optimized / DLL files # Byte-compiled / optimized / DLL files
__pycache__/ __pycache__/
*.py[cod] *.py[cod]

View File

@ -2,9 +2,10 @@
A template project for Python with Gtk applications. A template project for Python with Gtk applications.
### Requirements ### Requirements
* PyGObject * PyGObject (Gtk introspection library)
* setproctitle * pyxdg (Desktop ".desktop" file parser)
* pyxdg * setproctitle (Define process title to search and kill more easily)
* sqlmodel (SQL databases and is powered by Pydantic and SQLAlchemy)
### Note ### Note
There are a "\<change_me\>" strings and files that need to be set according to your app's name located at: There are a "\<change_me\>" strings and files that need to be set according to your app's name located at:

4
requirements.txt Normal file
View File

@ -0,0 +1,4 @@
PyGObject
pyxdg
setproctitle
sqlmodel

View File

@ -5,6 +5,7 @@ import threading
# Lib imports # Lib imports
# Application imports # Application imports
from utils.models import engine
from utils.event_system import EventSystem from utils.event_system import EventSystem
from utils.endpoint_registry import EndpointRegistry from utils.endpoint_registry import EndpointRegistry
from utils.keybindings import Keybindings 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... # NOTE: Just reminding myself we can add to builtins two different ways...
# __builtins__.update({"event_system": Builtins()}) # __builtins__.update({"event_system": Builtins()})
builtins.app_name = "<change_me>" builtins.app_name = "<change_me>"
builtins.db = engine
builtins.keybindings = Keybindings() builtins.keybindings = Keybindings()
builtins.event_system = EventSystem() builtins.event_system = EventSystem()
builtins.endpoint_registry = EndpointRegistry() builtins.endpoint_registry = EndpointRegistry()
@ -48,10 +51,3 @@ builtins.logger = Logger(settings_manager.get_home_config_path(), \
builtins.threaded = threaded_wrapper builtins.threaded = threaded_wrapper
builtins.daemon_threaded = daemon_threaded_wrapper builtins.daemon_threaded = daemon_threaded_wrapper
builtins.event_sleep_time = 0.05 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...")

View File

@ -1,23 +1,25 @@
# Python imports # Python imports
from typing import Optional
# Lib imports # 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): # NOTE: for sake of example we create an admin user with no password set.
email = _db.Column(_db.Text()) user = User(name = "Admin", password = "", email = "admin@domain.com")
username = _db.Column(_db.Text()) engine = create_engine("sqlite:///database.db")
password = _db.Column(_db.Text()) SQLModel.metadata.create_all(engine)
id = _db.Column(_db.Integer, primary_key=True,
unique=True, autoincrement=True)
def __repr__(self): with Session(engine) as session:
return f"'{self.email}', '{self.username}', '{self.password}', '{self.id}'" session.add(user)
session.commit()
_db.create_all()