refactor: substitute longer names for shorter to live longer

This commit is contained in:
surtur 2020-08-07 02:25:33 +02:00
parent fc0051b243
commit 6316d635dd
Signed by: wanderer
GPG Key ID: 19CE1EC1D9E0486D

@ -3,26 +3,29 @@ from sqlalchemy.orm import Session
from . import models, schemas from . import models, schemas
User=models.User
Service=models.Service
def get_user(db: Session, user_id: int): def get_user(db: Session, user_id: int):
return db.query(models.User).filter(models.User.id == user_id).first() return db.query(User).filter(User.id == user_id).first()
def del_user(db: Session, user_id: int): def del_user(db: Session, user_id: int):
db.query(models.User).filter(models.User.id == user_id).delete() db.query(User).filter(User.id == user_id).delete()
return db.commit() return db.commit()
def get_user_by_username(db: Session, name: str): def get_user_by_username(db: Session, name: str):
return db.query(models.User).filter(models.User.name == name).first() return db.query(User).filter(User.name == name).first()
def get_users(db: Session, skip: int = 0, limit: int = 100): def get_users(db: Session, skip: int = 0, limit: int = 100):
return db.query(models.User).offset(skip).limit(limit).all() return db.query(User).offset(skip).limit(limit).all()
def create_user(db: Session, user: schemas.UserCreate): def create_user(db: Session, user: schemas.UserCreate):
user.password = bcrypt.hashpw(user.password.encode(), bcrypt.gensalt()) user.password = bcrypt.hashpw(user.password.encode(), bcrypt.gensalt())
db_user = models.User(**user.dict()) db_user = User(**user.dict())
db_user.created_unix = time.time() db_user.created_unix = time.time()
db.add(db_user) db.add(db_user)
db.commit() db.commit()
@ -31,33 +34,33 @@ def create_user(db: Session, user: schemas.UserCreate):
def update_user(db: Session, user_id: int, user: schemas.UserUpdate): def update_user(db: Session, user_id: int, user: schemas.UserUpdate):
updated_user = models.User(**user.dict()) updated_user = User(**user.dict())
if (updated_user.password is not None): if (updated_user.password is not None):
updated_user.password = bcrypt.hashpw(user.password.encode(), bcrypt.gensalt()) updated_user.password = bcrypt.hashpw(user.password.encode(), bcrypt.gensalt())
db.query(models.User).filter(models.User.id==user_id).update({models.User.password: updated_user.password}) db.query(User).filter(User.id==user_id).update({User.password: updated_user.password})
if (updated_user.name is not None): if (updated_user.name is not None):
db.query(models.User).filter(models.User.id==user_id).update({models.User.name: updated_user.name}) db.query(User).filter(User.id==user_id).update({User.name: updated_user.name})
if (updated_user.full_name is not None): if (updated_user.full_name is not None):
db.query(models.User).filter(models.User.id==user_id).update({models.User.full_name: updated_user.full_name}) db.query(User).filter(User.id==user_id).update({User.full_name: updated_user.full_name})
if (updated_user.is_active is not None): if (updated_user.is_active is not None):
db.query(models.User).filter(models.User.id==user_id).update({models.User.is_active: updated_user.is_active}) db.query(User).filter(User.id==user_id).update({User.is_active: updated_user.is_active})
if (updated_user is not user): if (updated_user is not user):
db.query(models.User).filter(models.User.id==user_id).update({models.User.updated_unix: time.time()}) db.query(User).filter(User.id==user_id).update({User.updated_unix: time.time()})
return db.commit() return db.commit()
def get_service(db: Session, service_id: int): def get_service(db: Session, service_id: int):
return db.query(models.Service).filter(models.Service.id == service_id).first() return db.query(Service).filter(Service.id == service_id).first()
def get_services(db: Session, skip: int = 0, limit: int = 100): def get_services(db: Session, skip: int = 0, limit: int = 100):
return db.query(models.Service).offset(skip).limit(limit).all() return db.query(Service).offset(skip).limit(limit).all()
def create_service(db: Session, service: schemas.ServiceCreate): def create_service(db: Session, service: schemas.ServiceCreate):
db_item = models.Service(**service.dict()) db_item = Service(**service.dict())
db_item.created_unix = time.time() db_item.created_unix = time.time()
db.add(db_item) db.add(db_item)
db.commit() db.commit()
@ -66,27 +69,27 @@ def create_service(db: Session, service: schemas.ServiceCreate):
def del_service(db: Session, service_id: int): def del_service(db: Session, service_id: int):
db.query(models.Service).filter(models.Service.id == service_id).delete() db.query(Service).filter(Service.id == service_id).delete()
return db.commit() return db.commit()
def update_service(db: Session, service_id: int, s: schemas.ServiceUpdate): def update_service(db: Session, service_id: int, s: schemas.ServiceUpdate):
updated_s = models.Service(**s.dict()) updated_s = Service(**s.dict())
if (updated_s.name is not None): if (updated_s.name is not None):
db.query(models.Service).filter(models.Service.id==service_id).update({models.Service.name: updated_s.name}) db.query(Service).filter(Service.id==service_id).update({Service.name: updated_s.name})
if (updated_s.owner_id is not None): if (updated_s.owner_id is not None):
db.query(models.Service).filter(models.Service.id==service_id).update({models.Service.owner_id: updated_s.owner_id}) db.query(Service).filter(Service.id==service_id).update({Service.owner_id: updated_s.owner_id})
if (updated_s.is_private is not None): if (updated_s.is_private is not None):
db.query(models.Service).filter(models.Service.id==service_id).update({models.Service.is_private: updated_s.is_private}) db.query(Service).filter(Service.id==service_id).update({Service.is_private: updated_s.is_private})
if (updated_s.description is not None): if (updated_s.description is not None):
db.query(models.Service).filter(models.Service.id==service_id).update({models.Service.description: updated_s.description}) db.query(Service).filter(Service.id==service_id).update({Service.description: updated_s.description})
if (updated_s.service_type is not None): if (updated_s.service_type is not None):
db.query(models.Service).filter(models.Service.id==service_id).update({models.Service.service_type: updated_s.service_type}) db.query(Service).filter(Service.id==service_id).update({Service.service_type: updated_s.service_type})
if (updated_s.url is not None): if (updated_s.url is not None):
db.query(models.Service).filter(models.Service.id==service_id).update({models.Service.url: updated_s.url}) db.query(Service).filter(Service.id==service_id).update({Service.url: updated_s.url})
if (updated_s.is_active is not None): if (updated_s.is_active is not None):
db.query(models.Service).filter(models.Service.id==service_id).update({models.Service.is_active: updated_s.is_active}) db.query(Service).filter(Service.id==service_id).update({Service.is_active: updated_s.is_active})
if (updated_s is not s): if (updated_s is not s):
db.query(models.Service).filter(models.Service.id==service_id).update({models.Service.updated_unix: time.time()}) db.query(Service).filter(Service.id==service_id).update({Service.updated_unix: time.time()})
return db.commit() return db.commit()