kiwi-vpn/api/kiwi_vpn_api/db/crud.py

32 lines
702 B
Python
Raw Normal View History

2022-03-17 17:06:00 +00:00
from sqlalchemy.orm import Session
2022-03-18 17:36:44 +00:00
from passlib.context import CryptContext
2022-03-17 17:06:00 +00:00
from . import models, schemas
def get_user(db: Session, name: str):
return (db
.query(models.User)
.filter(models.User.name == name).first())
2022-03-18 17:36:44 +00:00
def create_user(
db: Session,
user: schemas.UserCreate,
2022-03-18 19:15:45 +00:00
capabilities: list[str],
crypt_context: CryptContext,
2022-03-18 17:36:44 +00:00
):
2022-03-17 17:06:00 +00:00
db_user = models.User(
name=user.name,
2022-03-18 17:36:44 +00:00
password=crypt_context.hash(user.password),
2022-03-18 19:15:45 +00:00
capabilities=[
models.UserCapability(capability=capability)
for capability in capabilities
]
2022-03-17 17:06:00 +00:00
)
2022-03-18 19:15:45 +00:00
2022-03-17 17:06:00 +00:00
db.add(db_user)
db.commit()
db.refresh(db_user)
return db_user