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

37 lines
837 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,
crypt_context: CryptContext
):
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-17 17:06:00 +00:00
)
db.add(db_user)
db.commit()
db.refresh(db_user)
return db_user
def add_user_capability(db: Session, user_name: str, capability: str):
db_user_capability = models.UserCapability(
user_name=user_name,
capability=capability,
)
db.add(db_user_capability)
db.commit()
db.refresh(db_user_capability)
return db_user_capability