def create_pending_user(self, phone_number): with sa_session(self.scoped_session) as session: invited_user = TBHUser(phone_number=phone_number, status=PENDING_STATUS) session.add(invited_user) return invited_user
def get_user_by_id(self, user_id): with sa_session(self.scoped_session) as session: try: user = session.query(TBHUser).filter( TBHUser.id == user_id).one() except: raise ResourceError('User resource not found') return user
def get_user_by_phone_number(self, phone_number): with sa_session(self.scoped_session) as session: try: user = session.query(TBHUser) \ .filter(TBHUser.get_user_by_phone_number == phone_number).one() except: raise ResourceError('User resource not found') return user
def verify_user(self, phone_number): with sa_session(self.scoped_session) as session: try: user = session.query(TBHUser).filter( TBHUser.phone_number == phone_number).one() except: raise ResourceError('User resource not found') user.status = 1
def create_verified_user(self, user_id, phone_number, gender, push_id): with sa_session(self.scoped_session) as session: invited_user = TBHUser(phone_number=phone_number, id=user_id, status=VERIFIED_STATUS, gender=gender, push_id=push_id) session.add(invited_user) return invited_user
def get_sent_messages(self, user_id): ''' Get all messages sent by a user ''' with sa_session(self.scoped_session) as session: sent_messages = session.query(Message) \ .filter(and_(Message.sender_id == user_id, Message.status == PENDING_MESSAGE_STATUS)).all() return sent_messages
def get_received_messages(self, user_id): ''' Get all received messages of a user ''' with sa_session(self.scoped_session) as session: received_messages = session.query(Message) \ .filter(and_(Message.receiver_id == user_id, Message.status == PENDING_MESSAGE_STATUS)).all() return received_messages
def get_sender_and_receiver(self, sender_id, receiver_phone_number): ''' Get a sender by id, and receiver by phone number in a single query. ''' with sa_session(self.scoped_session) as session: users = session.query(TBHUser) \ .filter(or_(TBHUser.id == sender_id, TBHUser.phone_number == receiver_phone_number)).all() return users
def create_message(self, sending_user, receiving_user, text, status): ''' Create a message instance ''' with sa_session(self.scoped_session) as session: message = Message(sending_user=sending_user, receiving_user=receiving_user, text=text, status=status) session.add(message)
def get_mutual_messages(self, user_id): ''' Get all mutual messages for a user ''' with sa_session(self.scoped_session) as session: mutual_messages = session.query(Message) \ .filter(and_( ((Message.sender_id == user_id) | (Message.receiver_id == user_id)), ((Message.status == MUTUAL_MESSAGE_STATUS)) )).all() return mutual_messages
def get_pending_messages_between_users(self, sender_id, receiver_id, text): ''' Get all pending messages between two users ''' with sa_session(self.scoped_session) as session: pending_messages = session.query(Message) \ .filter( or_( and_(Message.sender_id == sender_id, Message.receiver_id == receiver_id), and_(Message.sender_id == receiver_id, Message.receiver_id == sender_id) ) ).filter(Message.text == text).one() return pending_messages
def delete_messages(self, messages): with sa_session(self.scoped_session) as session: session.delete(messages)