From e45e335d507286ae19062471faf39d79d04be462 Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Tue, 7 Jun 2022 14:41:48 +0300 Subject: [PATCH] sql_app: added init db Signed-off-by: HeshamTB --- sql_app/init_db.py | 38 ++++++++++++++++++++++++++++++++++++++ sql_app/main.py | 12 +++++------- sql_app/utils.py | 9 +++++++++ 3 files changed, 52 insertions(+), 7 deletions(-) create mode 100644 sql_app/init_db.py create mode 100644 sql_app/utils.py diff --git a/sql_app/init_db.py b/sql_app/init_db.py new file mode 100644 index 0000000..175042f --- /dev/null +++ b/sql_app/init_db.py @@ -0,0 +1,38 @@ + +from . import crud, main, schemas, auth_helper +from decouple import config +from .database import SessionLocal +from datetime import timedelta + +db = SessionLocal() + +def init_user(): + user = schemas.UserCreate(email="hisham@banafa.com.sa", + username="Hesham", + password=config('first_user_pass')) + user_exists = crud.get_user_by_email(db, user.email) + if user_exists: return + crud.create_user(db, user) + token = auth_helper.create_access_token(data={"sub": user.username}, expires_delta=timedelta(minutes=15)) + res = crud.set_user_last_token(db, user.username, token) + if not res: print("Failed to add initial token") + +def init_door(): + iot_door = schemas.IotEntityCreate(bluetooth_mac="ff:ff:ff:ff", + description="Iot Lab Door") + door_exists = crud.get_iot_entity_by_bluetooth_mac(db, iot_door.bluetooth_mac) + if door_exists: return + crud.create_iot_entity(db, iot_door) + +def init_monitor(): + iot_monitor = schemas.IotEntityCreate(bluetooth_mac="ff:ff:00:ff", + description="Iot Lab Monitor") + monitor_exists = crud.get_iot_entity_by_bluetooth_mac(db, iot_monitor.bluetooth_mac) + if monitor_exists: return + crud.create_iot_entity(db, iot_monitor) + +def init(): + init_user() + init_door() + init_monitor() + \ No newline at end of file diff --git a/sql_app/main.py b/sql_app/main.py index 6db3665..0269b54 100644 --- a/sql_app/main.py +++ b/sql_app/main.py @@ -4,8 +4,9 @@ from fastapi.security.api_key import APIKey from fastapi.responses import PlainTextResponse from sqlalchemy.orm import Session -from . import crud, models, schemas, auth_helper +from . import crud, models, schemas, auth_helper, init_db from .database import SessionLocal, engine +from .utils import get_db from typing import List from datetime import timedelta, datetime @@ -16,13 +17,10 @@ oauth = OAuth2PasswordBearer(tokenUrl="tkn") app = FastAPI(title="IoT Building System") +# Split into endpoints modules +#app.include_router(users.router,prefix="/users", tags=["User"]) +init_db.init() -def get_db(): - db = SessionLocal() - try: - yield db - finally: - db.close() def get_current_user(token: str = Depends(oauth), db: Session = Depends(get_db)): credentials_exception = HTTPException( diff --git a/sql_app/utils.py b/sql_app/utils.py new file mode 100644 index 0000000..c8ccf88 --- /dev/null +++ b/sql_app/utils.py @@ -0,0 +1,9 @@ + +from .database import SessionLocal + +def get_db(): + db = SessionLocal() + try: + yield db + finally: + db.close() \ No newline at end of file