KintoneAppBuilder created
This commit is contained in:
0
backend/app/db/__init__.py
Normal file
0
backend/app/db/__init__.py
Normal file
69
backend/app/db/crud.py
Normal file
69
backend/app/db/crud.py
Normal file
@@ -0,0 +1,69 @@
|
||||
from fastapi import HTTPException, status
|
||||
from sqlalchemy.orm import Session
|
||||
import typing as t
|
||||
|
||||
from . import models, schemas
|
||||
from app.core.security import get_password_hash
|
||||
|
||||
|
||||
def get_user(db: Session, user_id: int):
|
||||
user = db.query(models.User).filter(models.User.id == user_id).first()
|
||||
if not user:
|
||||
raise HTTPException(status_code=404, detail="User not found")
|
||||
return user
|
||||
|
||||
|
||||
def get_user_by_email(db: Session, email: str) -> schemas.UserBase:
|
||||
return db.query(models.User).filter(models.User.email == email).first()
|
||||
|
||||
|
||||
def get_users(
|
||||
db: Session, skip: int = 0, limit: int = 100
|
||||
) -> t.List[schemas.UserOut]:
|
||||
return db.query(models.User).offset(skip).limit(limit).all()
|
||||
|
||||
|
||||
def create_user(db: Session, user: schemas.UserCreate):
|
||||
hashed_password = get_password_hash(user.password)
|
||||
db_user = models.User(
|
||||
first_name=user.first_name,
|
||||
last_name=user.last_name,
|
||||
email=user.email,
|
||||
is_active=user.is_active,
|
||||
is_superuser=user.is_superuser,
|
||||
hashed_password=hashed_password,
|
||||
)
|
||||
db.add(db_user)
|
||||
db.commit()
|
||||
db.refresh(db_user)
|
||||
return db_user
|
||||
|
||||
|
||||
def delete_user(db: Session, user_id: int):
|
||||
user = get_user(db, user_id)
|
||||
if not user:
|
||||
raise HTTPException(status.HTTP_404_NOT_FOUND, detail="User not found")
|
||||
db.delete(user)
|
||||
db.commit()
|
||||
return user
|
||||
|
||||
|
||||
def edit_user(
|
||||
db: Session, user_id: int, user: schemas.UserEdit
|
||||
) -> schemas.User:
|
||||
db_user = get_user(db, user_id)
|
||||
if not db_user:
|
||||
raise HTTPException(status.HTTP_404_NOT_FOUND, detail="User not found")
|
||||
update_data = user.dict(exclude_unset=True)
|
||||
|
||||
if "password" in update_data:
|
||||
update_data["hashed_password"] = get_password_hash(user.password)
|
||||
del update_data["password"]
|
||||
|
||||
for key, value in update_data.items():
|
||||
setattr(db_user, key, value)
|
||||
|
||||
db.add(db_user)
|
||||
db.commit()
|
||||
db.refresh(db_user)
|
||||
return db_user
|
||||
15
backend/app/db/models.py
Normal file
15
backend/app/db/models.py
Normal file
@@ -0,0 +1,15 @@
|
||||
from sqlalchemy import Boolean, Column, Integer, String
|
||||
|
||||
from .session import Base
|
||||
|
||||
|
||||
class User(Base):
|
||||
__tablename__ = "user"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
email = Column(String, unique=True, index=True, nullable=False)
|
||||
first_name = Column(String)
|
||||
last_name = Column(String)
|
||||
hashed_password = Column(String, nullable=False)
|
||||
is_active = Column(Boolean, default=True)
|
||||
is_superuser = Column(Boolean, default=False)
|
||||
45
backend/app/db/schemas.py
Normal file
45
backend/app/db/schemas.py
Normal file
@@ -0,0 +1,45 @@
|
||||
from pydantic import BaseModel
|
||||
import typing as t
|
||||
|
||||
|
||||
class UserBase(BaseModel):
|
||||
email: str
|
||||
is_active: bool = True
|
||||
is_superuser: bool = False
|
||||
first_name: str = None
|
||||
last_name: str = None
|
||||
|
||||
|
||||
class UserOut(UserBase):
|
||||
pass
|
||||
|
||||
|
||||
class UserCreate(UserBase):
|
||||
password: str
|
||||
|
||||
class Config:
|
||||
orm_mode = True
|
||||
|
||||
|
||||
class UserEdit(UserBase):
|
||||
password: t.Optional[str] = None
|
||||
|
||||
class Config:
|
||||
orm_mode = True
|
||||
|
||||
|
||||
class User(UserBase):
|
||||
id: int
|
||||
|
||||
class Config:
|
||||
orm_mode = True
|
||||
|
||||
|
||||
class Token(BaseModel):
|
||||
access_token: str
|
||||
token_type: str
|
||||
|
||||
|
||||
class TokenData(BaseModel):
|
||||
email: str = None
|
||||
permissions: str = "user"
|
||||
21
backend/app/db/session.py
Normal file
21
backend/app/db/session.py
Normal file
@@ -0,0 +1,21 @@
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
from app.core import config
|
||||
|
||||
engine = create_engine(
|
||||
config.SQLALCHEMY_DATABASE_URI,
|
||||
)
|
||||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||
|
||||
Base = declarative_base()
|
||||
|
||||
|
||||
# Dependency
|
||||
def get_db():
|
||||
db = SessionLocal()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
Reference in New Issue
Block a user