def delete_friendship(self, friend_a_id, friend_b_id): """ Delete Friend :param friend_a_id: user id :param friend_b_id: user id """ self.session.query(Friend) \ .filter(sa.or_( sa.and_( Friend.friend_a_id == friend_a_id, Friend.friend_b_id == friend_b_id ), sa.and_( Friend.friend_a_id == friend_b_id, Friend.friend_b_id == friend_a_id ) ) ) \ .delete() try: self.session.commit() except SQLAlchemyError as e: logger.log_error(f"Database Error: {str(e)}") self.session.rollback() raise exceptions.ServerError("Error deleting friendship") return None
def commit_changes(self): """Commit changes in session object to database""" try: self.session.commit() except SQLAlchemyError as e: logger.log_error(f"Database Error: {str(e)}") raise exceptions.ServerError("Error updating user info")
def delete_userinvite(self, userinvite): """ Delete UserInvite from database :param userinvite: UserInvite object """ self.session.delete(userinvite) try: self.session.commit() except SQLAlchemyError as e: logger.log_error(f"Database Error: {str(e)}") self.session.rollback() raise exceptions.ServerError("Error deleting invite") return None
def add_userinvites(self, userinvites): """ Add multiple UserInvite objects to a database :param userinvites: UserInvite objects """ try: self.session.bulk_save_objects(userinvites) self.session.commit() except SQLAlchemyError as e: logger.log_error(f"Database Error: {str(e)}") self.session.rollback() raise exceptions.ServerError("Error inviting users") return None
def add_userinvite(self, userinvite): """ Add UserInvite to database :param userinvite: UserInvite object """ self.session.add(userinvite) try: self.session.commit() except SQLAlchemyError as e: logger.log_error(f"Database Error: {str(e)}") self.session.rollback() raise exceptions.ServerError("Error inviting user.") return None
def delete_user(self, user): """ Delete user from database :param user: User object """ self.session.delete(user) try: self.session.commit() except SQLAlchemyError as e: self.session.rollback() logger.log_error(f"Database Error: {str(e)}") raise exceptions.ServerError("Error deleting user") return None
def delete_event(self, event): """ Delete event from database :param event: Event object """ self.session.delete(event) try: self.session.commit() except SQLAlchemyError as e: logger.log_error(f"Database Error: {str(e)}") self.session.rollback() raise exceptions.ServerError("Error deleting event.") return None
def add_event(self, event): """ Add event to database :param event: Event object """ self.session.add(event) try: self.session.commit() except SQLAlchemyError as e: logger.log_error(f"Database Error: {str(e)}") self.session.rollback() raise exceptions.ServerError("Error creating event.") return None
def add_friendinvites(self, friendinvites): """ Add multiple FriendInvite objects to a database :param userinvites: sequence of FriendInvite objects """ try: self.session.bulk_save_objects(friendinvites) self.session.commit() except SQLAlchemyError as e: logger.log_error(f"Database Error: {str(e)}") self.session.rollback() raise exceptions.ServerError("Error adding invites") return None
def add_user(self, user): """ Add user to database :param user: User object """ self.session.add(user) try: self.session.commit() except SQLAlchemyError as e: self.session.rollback() if isinstance(e, IntegrityError): raise exceptions.InputError("Email address in use.") else: logger.log_error(f"Database Error: {str(e)}") raise exceptions.ServerError("Error adding user") return None
def add_friendship(self, friend_a_id, friend_b_id): """ Add friendship to db. :param friend_a_id: user id :param friend_b_id: user id :return: both Friend objects (Friendship is reflexive) """ a_to_b = Friend(friend_a_id=friend_a_id, friend_b_id=friend_b_id, creation_date=datetime.datetime.utcnow()) b_to_a = Friend(friend_a_id=friend_b_id, friend_b_id=friend_a_id, creation_date=datetime.datetime.utcnow()) try: self.session.bulk_save_objects([a_to_b, b_to_a]) self.session.commit() except SQLAlchemyError as e: logger.log_error(f"Database Error: {str(e)}") self.session.rollback() raise exceptions.ServerError("Error adding friendship") return (a_to_b, b_to_a)
def add_friendship_from_invite(self, friendinvite): """ Add friendship to db from friendinvite passed in and remove friendinvite from db. :param friendinvite: FriendInvite object :return: both Friend objects """ a_to_b = Friend(friend_a_id=friendinvite.requesting_id, friend_b_id=friendinvite.requested_id, creation_date=datetime.datetime.utcnow()) b_to_a = Friend(friend_a_id=friendinvite.requested_id, friend_b_id=friendinvite.requesting_id, creation_date=datetime.datetime.utcnow()) try: self.session.bulk_save_objects([a_to_b, b_to_a]) self.session.delete(friendinvite) self.session.commit() except SQLAlchemyError as e: logger.log_error(f"Database Error: {str(e)}") self.session.rollback() raise exceptions.ServerError("Error adding friendship") return (a_to_b, b_to_a)