This repository has been archived on 2020-08-14. You can view files and clone it, but cannot push or open issues or pull requests.
statuspage/app/crud.py

47 lines
1.3 KiB
Python
Raw Normal View History

import bcrypt, time
from sqlalchemy.orm import Session
from . import models, schemas
def get_user(db: Session, user_id: int):
return db.query(models.User).filter(models.User.id == user_id).first()
def del_user(db: Session, user_id: int):
db.query(models.User).filter(models.User.id == user_id).delete()
db.commit()
return
def get_user_by_username(db: Session, name: str):
return db.query(models.User).filter(models.User.name == name).first()
def get_users(db: Session, skip: int = 0, limit: int = 100):
return db.query(models.User).offset(skip).limit(limit).all()
def create_user(db: Session, user: schemas.UserCreate):
h_et_s = bcrypt.hashpw(user.password.encode(), bcrypt.gensalt())
db_user = models.User(**user.dict())
db_user.name=user.name
db_user.password=h_et_s
db_user.created_unix = time.time()
db.add(db_user)
db.commit()
db.refresh(db_user)
return db_user
def get_services(db: Session, skip: int = 0, limit: int = 100):
return db.query(models.Service).offset(skip).limit(limit).all()
def create_service(db: Session, service: schemas.ServiceCreate):
db_item = models.Service(**service.dict())
""" inject updated+created times here """
db_item.updated_unix = time.time()
db_item.created_unix = time.time()
db.add(db_item)
db.commit()
db.refresh(db_item)
return db_item