From f45bd6295321a5c317ebc7f76ae7665af4522f37 Mon Sep 17 00:00:00 2001 From: duyanhehe <duyanhex@gmail.com> Date: Sat, 26 Apr 2025 21:08:37 +0700 Subject: [PATCH] refactor --- app/backend/dummy_data.py | 13 +++++++++ app/backend/scripts/__init__.py | 2 -- app/backend/scripts/admin_init.py | 47 ------------------------------ requirements.txt | Bin 2342 -> 1152 bytes run_app.py | 6 ---- 5 files changed, 13 insertions(+), 55 deletions(-) delete mode 100644 app/backend/scripts/__init__.py delete mode 100644 app/backend/scripts/admin_init.py diff --git a/app/backend/dummy_data.py b/app/backend/dummy_data.py index fe37556..07fa31d 100644 --- a/app/backend/dummy_data.py +++ b/app/backend/dummy_data.py @@ -31,6 +31,19 @@ def insert_dummy_data(session: Session): print("Dummy data already exists, skipping insertion") return + # Create admin user first + if not session.exec(select(User).where(User.role == "admin")).first(): + admin_user = User( + username="admin", + email="admin@example.com", + password=hash_password("admin"), + phone_number="1234567890", + role="admin", + ) + session.add(admin_user) + session.commit() + print("Default admin user created successfully!") + if not session.exec(select(User)).first(): users = [ User( diff --git a/app/backend/scripts/__init__.py b/app/backend/scripts/__init__.py deleted file mode 100644 index d089d7f..0000000 --- a/app/backend/scripts/__init__.py +++ /dev/null @@ -1,2 +0,0 @@ -# Scripts for backend initialization and maintenance -from .admin_init import init_admin \ No newline at end of file diff --git a/app/backend/scripts/admin_init.py b/app/backend/scripts/admin_init.py deleted file mode 100644 index 5a52ec5..0000000 --- a/app/backend/scripts/admin_init.py +++ /dev/null @@ -1,47 +0,0 @@ -import sys -import os - -# Add the parent directory to the path to allow importing from the backend -sys.path.append( - os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) -) - -from backend.database import get_session, engine -from backend.models.models import SQLModel, User -from backend.utils.hashing import hash_password -from sqlmodel import select, Session - - -def init_admin(): - """Initialize a default admin user if no admin user exists""" - print("Initializing admin user...") - - # Create all tables if they don't exist - SQLModel.metadata.create_all(engine) - - with Session(engine) as session: - # Check if admin user already exists - admin = session.exec(select(User).where(User.role == "admin")).first() - - if admin: - print("Admin user already exists.") - return - - print("Creating default admin user...") - # Create default admin user with username and password both "admin" - hashed_password = hash_password("admin") - admin_user = User( - username="admin", - email="admin@example.com", - password=hashed_password, - phone_number="1234567890", - role="admin", - ) - - session.add(admin_user) - session.commit() - print("Default admin user created successfully!") - - -if __name__ == "__main__": - init_admin() diff --git a/requirements.txt b/requirements.txt index 8bce77cb9555277ac11381eadb381a2ddc28e46e..a9d3dfb1d98fe88cebd319979cae7b7c670868cf 100644 GIT binary patch literal 1152 zcmYey%gZlGEJ;n#EvYO>Ew;5a&@<OF;7ZJ^%*?m7HPN%sGvG=}E~+djv9&eUGtx8S zN=_{*$xO?%wKXy@GSxHGGdAQ(P6P1`^$gAR47rjs5{rscOLX({i*gflGOJRHKsrr8 zGC7&a*|xS8dWL!yT*>)4`9+Dji69$I^vt-xcI2lQB^G2<+S;0!=o#o4aXE)%`=%Bb zC#I(+<yY9+8tIvHC6^YL<mZ-TXXcfFOg06(DJ8KeJ0-OwHMs<29LUWndBp{lB^mj7 zU|o6!T&cN<nK|)gi8+}mi6!|(AYmgt1Fp2h;*!LIOprE1LsLCNLzsweat>J3K+l{j zJr&}x%;cQRB#=%6kWfJ-hzasodQoa>UQQ}Fhz#`%xiSn5!3LV>8E|EkloTZA7p2<T z8tQ?}0`W`o^K-yKZKh`eR!{*_Ze*cn$d#Frmk0_kLj$hNyv*eMytK@8kTr&S23(mX z#VLt->8VBerNtmIko&zd^Rg0+z&i9yxpG0_kYApsn^~d@a-OlCft~@EZ(>n)X+dye zS}I5#C|q+>N{ezpb{gs#aphJP7v|_D=jY|6CYR(F>B7RzQV(Q7eo<C2C=`wL3=Q-Q zO}PpZld}`kGxI>MGSM^QDo89Y28W8Fp1Gb0S3zb@PJX$qt)ZbF$jJpcrRnLFAitaH z8E_R8mzHGa*xH(domEhoTmVWckhn17DyU3J%qz)E26@fUK+g;=pqpAzQj`cwphh5v zAXMuXr<Rmt=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 --- a/run_app.py +++ b/run_app.py @@ -2,7 +2,6 @@ import threading import uvicorn from app.backend.main import app # Your FastAPI app import os -from app.backend.scripts.admin_init import init_admin def run_fastapi(): @@ -14,11 +13,6 @@ def start_tkinter_app(): if __name__ == "__main__": - # Initialize admin user - print("Initializing admin user...") - init_admin() - print("Admin initialization complete!") - # Start FastAPI in a separate thread fastapi_thread = threading.Thread(target=run_fastapi, daemon=True) fastapi_thread.start() -- GitLab