def opt_out_of_quotes(is_exec, requester: Mention, target: Mention = None, session=db_session) -> str: # if no target, target ourselves if target is None: target = requester # check if requester has permission permission = True if requester.is_id_type(): # discord user check (with exec override) if requester.id != target.id and not is_exec: permission = False else: if requester.string != target.string and not is_exec: permission = False if not permission: raise QuoteException(QuoteError.NOT_PERMITTED) # check if user has opted out if target.is_id_type(): f = QuoteOptouts.user_id == target.id else: f = QuoteOptouts.user_string == target.string q = session.query(QuoteOptouts).filter(f).one_or_none() if q is not None: raise QuoteException(QuoteError.OPTED_OUT) # opt out user optout = QuoteOptouts(user_type=target.type, user_id=target.id, user_string=target.string) try: session.add(optout) session.commit() # purge old quotes deleted = purge_quotes(is_exec, requester, target, session) session.commit() return deleted except (ScalarListException, SQLAlchemyError) as e: session.rollback() logging.exception(e) raise QuoteException(QuoteError.DB_ERROR)
def user_opted_out(user: Mention, session=db_session) -> bool: """check if mentioned user has opted out""" if user.is_id_type(): f = QuoteOptouts.user_id == user.id else: f = QuoteOptouts.user_string == user.string q = session.query(QuoteOptouts).filter(f).one_or_none() return q is not None
def add_quote(author: Mention, quote, time, session=db_session) -> str: """Add a quote by the specified author""" if quote is None: raise QuoteException(QuoteError.BAD_FORMAT) if user_opted_out(author, session): raise QuoteException(QuoteError.OPTED_OUT) if author.is_id_type(): new_quote = Quote.id_quote(author.id, quote, time) else: new_quote = Quote.string_quote(author.string, quote, time) try: session.add(new_quote) session.commit() return str(new_quote.quote_id) except (ScalarListException, SQLAlchemyError) as e: session.rollback() logging.exception(e) raise QuoteException(QuoteError.DB_ERROR)
def opt_in_to_quotes(requester: Mention, session=db_session) -> str: # check to see if target is opted out already if requester.is_id_type(): q = session.query(QuoteOptouts).filter(QuoteOptouts.user_id == requester.id) else: q = session.query(QuoteOptouts).filter( QuoteOptouts.user_string == requester.string ) if q.one_or_none() is None: raise QuoteException(QuoteError.NO_OP) # opt in try: q.delete() session.commit() return "OK" except (ScalarListException, SQLAlchemyError) as e: session.rollback() logging.exception(e) raise QuoteException(QuoteError.DB_ERROR)
def purge_quotes( is_exec, requester: Mention, target: Mention, session=db_session ) -> str: if not is_exec and requester != target: raise QuoteException(QuoteError.NOT_PERMITTED) # get quotes if target.is_id_type(): f = session.query(Quote).filter(Quote.author_id == target.id) else: f = session.query(Quote).filter(Quote.author_string == target.string) to_delete = f.count() try: f.delete() session.commit() return str(to_delete) except (ScalarListException, SQLAlchemyError) as e: session.rollback() logging.exception(e) raise QuoteException(QuoteError.DB_ERROR)
def _(query: Mention, session=db_session): """query by Mention""" if query.is_id_type(): return session.query(Quote).filter(Quote.author_id == query.id) else: return session.query(Quote).filter(Quote.author_string == query.string)