def update_db(added_stat: 'UserStat', update) -> None: try: with session_scope() as db: user_stat = db.query(UserStatDB).filter( UserStatDB.stats_monday == added_stat.stats_monday, UserStatDB.cid == added_stat.cid, UserStatDB.uid == added_stat.uid) if user_stat.update(update) > 0: return logger.error( f'[userstat.update] user {added_stat.uid}:{added_stat.cid} not found in DB' ) # update возвращает 0 если объекта нет в бд # тогда мы добавляем его в бд и еще раз пробуем обновить # статистику добавляем даже по тем, кого нет в таблицах User|ChatUser add_to_db(UserStatDB.copy(added_stat)) with session_scope() as db: user_stat = db.query(UserStatDB).filter( UserStatDB.stats_monday == added_stat.stats_monday, UserStatDB.cid == added_stat.cid, UserStatDB.uid == added_stat.uid) user_stat.update(update) except Exception as e: logger.error(e) raise Exception( f"Can't update userstat {added_stat.uid}:{added_stat.cid} to DB" )
def add(uid, cid, date, from_uid, leave_type): try: add_to_db( LeaveCollectorDB(uid=uid, cid=cid, date=date, from_uid=from_uid, leave_type=leave_type)) except Exception as e: logger.error(e) raise Exception(f"Can't add leave_collect {uid}:{cid} to DB")
def add(cls, value: 'ChatUser'): try: add_to_db(cls.copy(value)) except Exception as e: logger.error(e) raise Exception(f"Can't add chatuser {value.uid}:{value.cid} to DB")
def add(added_stat: 'UserStat') -> None: try: add_to_db(UserStatDB.copy(added_stat)) except Exception as e: logger.error(e) raise Exception(f"Can't add userstat {added_stat.uid} to DB")
def add(cls, new_user: '******'): try: add_to_db(cls.copy(new_user)) except Exception as e: logger.error(e) raise Exception(f"Can't add user {new_user.uid} to DB")