def check_if_user_exists(cls, user_id: int) -> bool: with db_session() as session: user = session.query( User ).filter_by( id=user_id ).first() return bool(user)
def create_public(cls, public_dto: PublicDTO) -> None: with db_session() as session: public = Public( id=public_dto.public_id, name=public_dto.public_name, slug_url=public_dto.public_slug_url, ) session.add(public)
def create_user(cls, user_dto: UserDTO) -> None: with db_session() as session: user = User( id=user_dto.user_id, login=user_dto.login, first_name=user_dto.first_name, last_name=user_dto.last_name, ) session.add(user)
def get_user_publics(cls, user_id: int) -> List[PublicDTO]: with db_session() as session: user = session.query(User).get(user_id) publics_dto_list = [ PublicDTO( public_id=user_public.public.id, public_name=user_public.public.name, public_slug_url=user_public.public.slug_url, ) for user_public in user.publics ] return publics_dto_list
def remove_user_public(cls, user_id: int, public_id: int) -> None: with db_session() as session: user_public = session.query( UserPublic ).filter_by( user_id=user_id, public_id=public_id, ).first() session.delete(user_public) public = session.query(Public).get(public_id) if len(public.users) == 0: session.delete(public)
def link_public_to_user(cls, user_id: int, public_id: int) -> None: with db_session() as session: user_public_relation = session.query( UserPublic ).filter_by( user_id=user_id, public_id=public_id ).first() if not user_public_relation: user = session.query(User).get(user_id) public = session.query(Public).get(public_id) user_public = UserPublic() user_public.public = public user_public.user = user public.users.append(user_public) user.publics.append(user_public)
def get_users_publics_to_refresh(cls) -> List[UserPublicRefreshDTO]: with db_session() as session: users = session.query(User).all() user_public_refresh_dto_list = [] for user in users: publics_dto_list = [ PublicDTO( public_id=user_public.public.id, public_name=user_public.public.name, public_slug_url=user_public.public.slug_url, ) for user_public in user.publics ] user_public_refresh_dto = UserPublicRefreshDTO( user_id=user.id, publics=publics_dto_list, ) user_public_refresh_dto_list.append(user_public_refresh_dto) return user_public_refresh_dto_list
def get_last_refresh(cls) -> int: with db_session() as session: last_refresh = cls._get_or_create_last_refresh(session) last_refresh_timestamp = last_refresh.timestamp return last_refresh_timestamp
def update_last_refresh(cls) -> None: with db_session() as session: last_refresh = cls._get_or_create_last_refresh(session) last_refresh.timestamp = timestamp_now()
def update_last_refresh(): with db_session() as session: last_refresh = LastRefreshDAO._get_or_create_last_refresh(session) datetime_to_set = datetime.utcnow() - DEBUG_LAST_REFRESH_UPDATE_DELTA last_refresh.timestamp = int(time.mktime(datetime_to_set.timetuple()))
def check_if_public_exists(cls, public_id: int) -> bool: with db_session() as session: public = session.query(Public).filter_by(id=public_id).first() return bool(public)