def suspend_user(user_id, community_name, end_suspension, start_suspension=None): """ suspends a user from a commmunity :param user_id: :param community_name: :param end_suspension: :param start_suspension: :return: """ if not user_exists(get_email_by_id(user_id)): return "User doesn't exist" if not community_exists(community_name): return "Community does not exist" if not start_suspension: start_suspension = datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S") suspension_list = exec_commit('SELECT user_id FROM suspensions WHERE user_id = %s AND community_name = %s', (user_id, community_name)) if not suspension_list: exec_commit('INSERT INTO suspensions (user_id, suspended_since, suspended_till, community_name) VALUES(%s, ' '%s, %s, %s)', (user_id, start_suspension, end_suspension, community_name)) else: exec_commit('UPDATE suspensions SET suspended_since = %s AND suspended_till = %s WHERE user_id = %s AND ' 'community_name = %s', (start_suspension, end_suspension, user_id, community_name)) return user_id + " is suspended from " + start_suspension + " until " + end_suspension + " on " + community_name
def create_direct_message(message_id: int, sender_id: str, receiver_id: str, time_sent: None, message: str) -> str: """ adds a message to db :param message_id: message id :param sender_id: sender's id :param receiver_id: receiver's id :param time_sent: :param message: text i.e. message content :return: """ sender_email = get_email_by_id(sender_id) receiver_email = get_email_by_id(receiver_id) if not user_exists(sender_email): return "User" + sender_email + "doesn't exist" if not user_exists(receiver_email): return "User" + receiver_email + "doesn't exist" init_time = datetime.now() init_time = str(init_time) if time_sent: init_time = datetime.strptime(time_sent, '%Y-%m-%d %H:%M:%S') suspension_dates = exec_get_all('SELECT suspended_since, suspended_till FROM users WHERE email = %s', (sender_email,)) suspended_since = suspension_dates[0][0] suspended_till = suspension_dates[0][1] if suspended_since and suspended_till: if suspended_since < init_time < suspended_till: return sender_email + " is currently suspended until " + suspended_till.strftime("%Y/%m/%d %H:%M:%S") # If no time was given it will default to the current time exec_commit(f'INSERT INTO direct_messages(message_id, sender_id, receiver_id, time_sent, message)' f' VALUES (%s,%s,%s,%s,%s)', (message_id, sender_id, receiver_id, init_time, message)) return "Message sent successfully"
def change_username(user_id, new_name, change_time=None): """ changes username (user_id in db) if it has not been changed in the last six months :param user_id: :param new_name: :param change_time: :return: """ email = get_email_by_id(user_id) if not user_exists(email): return "User doesn't exist" if len(new_name) < 8 or len(new_name) > 30: return "Username needs to be between 8 to 30 characters" new_time = datetime.now() if change_time: new_time = datetime.strptime(change_time, '%Y-%m-%d %H:%M:%S') change_date_list = exec_get_all('SELECT userid_reset FROM users WHERE email = %s', (email,)) change_date = change_date_list[0][0] six_months_later = relativedelta(months=6) if not change_date or (change_date + six_months_later <= new_time): exec_commit('UPDATE users SET userid_reset = %s WHERE email = %s', (new_time, email)) exec_commit('UPDATE users SET user_id = %s WHERE email = %s', (new_name, email)) return "User successfully changed username to " + new_name return "User changed their username in the last 6 months"
def create_user(user_id: str, name: str, phone_number: int, email: str, userid_set: datetime, userid_reset: None, suspended_since: None, suspended_till: None) -> str: """ Creates a new user :param userid_set: :param userid_reset: :param user_id: unique id of user :param name: name of user :param email: user's email address :param phone_number: user's 10-digit phone number :param suspended_till: :param suspended_since: :return: None """ if user_exists(email): return "User already exists" if len(user_id) < 6 or len(user_id) > 30: return "Username needs to be between 8 to 30 characters" if not userid_set: userid_set = datetime.now().strftime("%Y/%m/%d %H:%M:%S") string = f'INSERT INTO users(user_id, name, phone_number, email, userid_set, ' \ f'userid_reset, suspended_since, suspended_till)' \ f'VALUES (\'{user_id}\',\'{name}\', {phone_number},' \ f'\'{email}\', \'{userid_set}\', NULL, NULL, NULL)' exec_commit(string) return "User added successfully"
def add_channel(channel, community_name): if channel_exists(channel, community_name): return channel + " exists" if not community_exists(community_name): return "The community doesn't exist" exec_commit('INSERT INTO channels (name,community_name) VALUES (%s,%s)', (channel, community_name)) return channel + " was added to " + community_name
def add_user_to_community(user_id, community): """ This will add a user to a community """ if not community_exists(community): return "The community doesn't exist" if not user_exists(get_email_by_id(user_id)): return "The user doesn't exist" exec_commit('INSERT INTO memberships (user_id,community_name) VALUES (%s,%s)', (user_id, community)) return user_id + " is now a member of " + community
def read_message(message_id, receiver_id): """ marks message as read :param message_id: :param receiver_id: :return: text content """ texts = exec_get_all('SELECT message FROM direct_messages WHERE message_id = %s AND receiver_id = %s', (message_id, receiver_id)) if len(texts) == 1: exec_commit('UPDATE direct_messages SET is_read = TRUE WHERE message_id = %s', (message_id,)) return texts[0][0]
def add_community(community_name, channels=[]): """ This function will add a new community and a list of channels to it if specified """ if community_exists(community_name): return "The community already exists" exec_commit('INSERT INTO communities (name) VALUES (%s)', (community_name,)) print("Creating community " + community_name) for channel in channels: print(add_channel(channel, community_name) + " ") return community_name + " was added "
def resume_user(user_id, community_name): """ discontinues suspension for a user :param user_id: :return: """ if not user_exists(get_email_by_id(user_id)): return "User doesn't exist" if not community_exists(community_name): return "Community does not exist" exec_commit( 'UPDATE suspensions SET suspended_since = NULL AND suspended_till = NULL' ' WHERE user_id = %s AND community_name = %s', (user_id, community_name)) return user_id + " is no longer suspended on " + community_name
def get_last_message_id(): """ this will get us the last sent direct message on the app :return: """ msg_id = exec_commit('SELECT message_id FROM direct_messages ORDER BY message_id DESC LIMIT 1') return msg_id