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
__pycache__/
*.py[cod]

View File

@ -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 "\<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
# 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 = "<change_me>"
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...")

View File

@ -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()