Skip to content

Commit

Permalink
Merge pull request #3 from bescka/comply_with_black
Browse files Browse the repository at this point in the history
Fromatting and removing unused Imports
  • Loading branch information
bescka authored Jul 26, 2024
2 parents 47281d3 + 04dc258 commit 9bc9148
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 58 deletions.
28 changes: 12 additions & 16 deletions backend-app/app/api/auth.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
# New imports security.py
from app.models.token import Token, TokenData
from app.models.user import User, UserCreate
from app.sql_db.crud import get_db, get_user_by_email
from sqlalchemy.orm import Session
from app.core.security import verify_password

from typing import Annotated
from fastapi import APIRouter, UploadFile, HTTPException, Depends, status
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
from fastapi.responses import JSONResponse
from jose import JWTError, jwt
import os
from datetime import datetime, timedelta, timezone
import pandas as pd
from starlette.responses import JSONResponse
from typing import Annotated

from app.core import settings
from dotenv import load_dotenv
import os
from fastapi import APIRouter, Depends, HTTPException, status
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
from jose import JWTError, jwt
from sqlalchemy.orm import Session

from app.core.security import verify_password
from app.models.token import Token, TokenData
from app.models.user import User
from app.sql_db.crud import get_db, get_user_by_email

load_dotenv()
SECRET_KEY = os.getenv("FASTAPI_SECRET_KEY")
Expand Down Expand Up @@ -92,7 +87,8 @@ async def api_health_check():

@router.post("/token")
async def login_for_access_token(
form_data: Annotated[OAuth2PasswordRequestForm, Depends()], db: Session = Depends(get_db)
form_data: Annotated[OAuth2PasswordRequestForm, Depends()],
db: Session = Depends(get_db),
) -> Token:
user = authenticate_user(form_data.username, form_data.password, db=db)
if not user:
Expand Down
7 changes: 4 additions & 3 deletions backend-app/app/api/fileupload.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
from typing import Annotated

import pandas as pd
from app.api.auth import get_current_active_user
from app.models.user import User
from app.sql_db.file_crud import create_update_table, get_db, insert_data
from fastapi import APIRouter, Depends, HTTPException, UploadFile
from fastapi.responses import JSONResponse
from sqlalchemy.orm import Session

from app.api.auth import get_current_active_user
from app.models.user import User
from app.sql_db.file_crud import create_update_table, get_db, insert_data

logger = logging.getLogger(__name__)

router = APIRouter(tags=["fileupload"])
Expand Down
21 changes: 12 additions & 9 deletions backend-app/app/api/users.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
from typing import Annotated
from fastapi import Depends, APIRouter, HTTPException

from fastapi import APIRouter, Depends, HTTPException
from fastapi.responses import JSONResponse
from sqlalchemy.orm import Session

from app.sql_db import crud
import app.models.database as models # INFO: Naming?
import app.models.user as api_m
from app.api.auth import get_current_active_admin, get_current_active_user
from app.sql_db import crud
from app.sql_db.crud import get_db
from app.sql_db.database import engine
from app.api.auth import get_current_active_user, get_current_active_admin

# WARNING: only temporary not needed if connected to already existing database

# models.Base.metadata.create_all(bind=engine)
def init_db():
print('creating db...')
print("creating db...")
models.Base.metadata.create_all(bind=engine)
print('db created')
print("db created")


router = APIRouter(
prefix="/users",
tags=["user"],
Expand Down Expand Up @@ -53,7 +54,8 @@ def update_user_state(
# TODO: improve exception handeling
if current_user.is_active is new_state.new_state:
raise HTTPException(
status_code=404, detail=f"User is_active is set to {new_state.new_state} already!"
status_code=404,
detail=f"User is_active is set to {new_state.new_state} already!",
)
_ = crud.update_is_active(db, current_user, new_state.new_state)
return JSONResponse(content={"Message": "User status updated"})
Expand Down Expand Up @@ -127,7 +129,8 @@ def update_any_user_state(
# TODO: improve exception handeling
if db_user.is_active is new_state.new_state:
raise HTTPException(
status_code=404, detail=f"User is_active is set to {new_state.new_state} already!"
status_code=404,
detail=f"User is_active is set to {new_state.new_state} already!",
)
updated_user = crud.update_is_active(db, db_user, new_state.new_state)
return JSONResponse(content={"Message": "User status updated"})
14 changes: 8 additions & 6 deletions backend-app/app/main.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import logging
import sys

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

from .api import auth, fileupload, users
from .api.users import init_db
from .api import auth, users, fileupload
import logging
import sys

logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
handlers=[
logging.StreamHandler(sys.stdout) # Output to console
]
handlers=[logging.StreamHandler(sys.stdout)],
)

app = FastAPI()
Expand All @@ -27,10 +27,12 @@
allow_headers=["*"],
)


@app.on_event("startup")
def on_startup():
init_db()


# @app.get('/')
# async def root():
# return {'message': 'live'}
2 changes: 1 addition & 1 deletion backend-app/app/models/db_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from sqlalchemy import Integer, String, Float
import pandas as pd
from sqlalchemy import Float, Integer, String


def map_dtype(dtype):
Expand Down
26 changes: 15 additions & 11 deletions backend-app/app/models/file_db.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
import logging

from sqlalchemy import Column, Integer, text
from app.sql_db.file_database import Base_file_db

from app.models.db_utils import map_dtype
import logging
from app.sql_db.file_database import Base_file_db

logger = logging.getLogger(__name__)


def create_file_table_class(df, table_name, existing_columns=None, only_new_columns=False):
columns = {
'__tablename__': table_name,
'__table_args__': {"extend_existing": True},
'id': Column(Integer, primary_key=True, autoincrement=True)
"__tablename__": table_name,
"__table_args__": {"extend_existing": True},
"id": Column(Integer, primary_key=True, autoincrement=True),
}

for column_name, dtype in df.dtypes.items():
if only_new_columns:
if column_name not in existing_columns:
columns[column_name] = Column(map_dtype(dtype))
else:
columns[column_name] = Column(map_dtype(dtype))

if existing_columns and not only_new_columns:
for column_name, column_type in existing_columns.items():
if column_name != "id": # Skip id as it's already defined
columns[column_name] = Column(column_type)

logger.info(f"Columns for {table_name}: {columns}")
FileTable = type('FileTable', (Base_file_db,), columns)
FileTable = type("FileTable", (Base_file_db,), columns)
return FileTable


def update_schema(df, engine, metadata, table_name):
metadata.reflect(bind=engine)
table = metadata.tables.get(table_name)
Expand All @@ -37,7 +41,7 @@ def update_schema(df, engine, metadata, table_name):
for column_name, dtype in df.dtypes.items():
if column_name not in existing_columns:
columns_to_add.append(Column(column_name, map_dtype(dtype)))

if columns_to_add:
with engine.connect() as conn:
for column in columns_to_add:
Expand All @@ -52,6 +56,6 @@ def update_schema(df, engine, metadata, table_name):
metadata.clear()
metadata.reflect(bind=engine)
existing_columns = {col.name: col.type for col in table.columns}

FileTable = create_file_table_class(df, table_name, existing_columns=existing_columns)
return FileTable
2 changes: 2 additions & 0 deletions backend-app/app/models/token.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from pydantic import BaseModel


class Token(BaseModel):
access_token: str
token_type: str
message: str


class TokenData(BaseModel):
email: str
3 changes: 2 additions & 1 deletion backend-app/app/models/user.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from pydantic import BaseModel
from typing import Optional

from pydantic import BaseModel


class UserBase(BaseModel):
email: str
Expand Down
4 changes: 1 addition & 3 deletions backend-app/app/sql_db/crud.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
from fastapi import HTTPException
from sqlalchemy import update
from sqlalchemy.orm import Session

from app.core.security import get_password_hashed
from app.models import database as sql_m
from app.models import user as api_m
from app.core.security import get_password_hashed
from app.sql_db.database import SessionLocal


Expand Down
2 changes: 1 addition & 1 deletion backend-app/app/sql_db/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from sqlalchemy import create_engine
from sqlalchemy.orm import declarative_base, sessionmaker

# load_dotenv()
load_dotenv()

SQLALCHEMY_DATABASE_URL = os.getenv("USER_DB_URL")

Expand Down
18 changes: 11 additions & 7 deletions backend-app/app/sql_db/file_crud.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from app.sql_db.file_database import SessionLocalFileDb, Base_file_db
from sqlalchemy.orm import Session
from sqlalchemy import MetaData
import logging

import pandas as pd
from sqlalchemy import MetaData
from sqlalchemy.orm import Session

from app.models.file_db import create_file_table_class, update_schema
import logging
from app.sql_db.file_database import Base_file_db, SessionLocalFileDb

logger = logging.getLogger(__name__)

Expand All @@ -15,6 +17,7 @@ def get_db():
finally:
db.close()


def create_update_table(df, engine, table_name):
metadata = MetaData()
metadata.reflect(engine)
Expand All @@ -30,14 +33,15 @@ def create_update_table(df, engine, table_name):
metadata.reflect(bind=engine)
return FileTable, f"Table with name {table_name} updated"


def insert_data(db: Session, df: pd.DataFrame, FileTable, update_column_name="id"):
# Ensure all numeric columns are correctly cast to numeric types
numeric_cols = df.select_dtypes(include=['number']).columns
df[numeric_cols] = df[numeric_cols].apply(pd.to_numeric, errors='coerce')
numeric_cols = df.select_dtypes(include=["number"]).columns
df[numeric_cols] = df[numeric_cols].apply(pd.to_numeric, errors="coerce")
df[numeric_cols] = df[numeric_cols].fillna(0)

# Ensure all string columns are correctly cast to string types
string_cols = df.select_dtypes(include=['object']).columns
string_cols = df.select_dtypes(include=["object"]).columns
df[string_cols] = df[string_cols].astype(str)

data = df.to_dict(orient="records")
Expand Down

0 comments on commit 9bc9148

Please sign in to comment.