def search_user(authorized_username, search_key, username): """ The function returns a list of users searched by search_key. *Parameters*: - *authorized_username (string)*: The user that is logged in now. - *search_key (string)*: The keyword used to get best match users. *Returns*: - *User_list*: a list of objects of user. """ if search_key == "": return [] results = query_factory.search_user(search_key) try: results = actions.paginate(dictionaries_list=results, required_size=size, start_after_key='username', start_after_value=username) except TypeError as E: print(E) raise if results is None: return None user_list = [] for result in results: # print(result['username']) result["profile_image_url"] = create_url('picture', result["profile_image_url"]) friendship = actions.get_friendship(authorized_username, result['username']) result.update(friendship) user_list.append(User(result)) return user_list
def get_recent_conversationers(from_username, last_conversationers_retrieved_username=None): """ This function get list of conversationers between two users converting lists of dictionaries into lists of Conversation model, it calls function from query factory that returns lists of dictionaries fit Conversation model with the given pagination. *Parameter:* - *from_username*: user who is send the message. - *last_conversationers_retrieved_username*: id of last message retrieved *Returns:* - *models.User object* """ if actions.is_user(from_username) is False: raise Exception('Username who sent this message does not exist.') if last_conversationers_retrieved_username is not None: if actions.is_user(last_conversationers_retrieved_username) is False: raise Exception('Username does not exist.') conversationers = query_factory.get_recent_conversationers(from_username) try: conversationers = actions.paginate( dictionaries_list=conversationers, required_size=20, start_after_key='username', start_after_value=last_conversationers_retrieved_username) except TypeError: raise if conversationers is None: return [] conversationer_list = [] if len(conversationers) == 0: return conversationer_list for conversation in conversationers: to_username = conversation['username'] dictionary = {'username': to_username} temp = {'screen_name': conversation['screen_name']} dictionary.update(temp) temp = {'profile_image_url': conversation['profile_image_url']} dictionary.update(temp) flag = check_follow(from_username, to_username) temp = {'following': flag} dictionary.update(temp) flag = check_follow(to_username, from_username) temp = {'follows_you': flag} dictionary.update(temp) flag = check_block(from_username, to_username) temp = {'blocked': flag} dictionary.update(temp) flag = check_mute(from_username, to_username) temp = {'muted': flag} dictionary.update(temp) user = User(dictionary) conversationer_list.append(user) return conversationer_list
def get_profile_following(username, last_retrieved_username, authorized_username): """ The function returns a list of UserProfile objects of the followed users. *Parameters*: - *username (string)*: The user that is logged in now. - *last_retrieved_username(string)*: The last user in the previous list. *Returns*: - *followed_list*: a list of objects of UsersProfiles. """ followed = query_factory.get_following(username) try: followed = timelines_and_trends_actions.paginate( dictionaries_list=followed, required_size=size, start_after_key='username', start_after_value=last_retrieved_username) except TypeError as E: print(E) raise if followed is None: return None user_profile_list = [] for follower in followed: check = query_factory.if_blocked(follower['username'], authorized_username)['count'] if check == 0: follower[ "followers_count"] = users_profile_query_factory.get_user_followers( follower['username'])["count"] follower[ "following_count"] = users_profile_query_factory.get_user_following( follower['username'])["count"] follower[ "kweeks_count"] = users_profile_query_factory.get_number_of_kweeks( follower['username'])['count'] follower[ "likes_count"] = users_profile_query_factory.get_number_of_likes( follower['username'])['count'] friendship = timelines_and_trends_actions.get_friendship( authorized_username, follower['username']) follower.update(friendship) user_profile_list.append(UserProfile(follower)) return user_profile_list
def get_messages(from_username, to_username, last_message_retrieved_id=None): """ This function get list of messages from (from_user)to (to_username) converting lists of dictionaries into lists of DirectMessage model, it calls function from query factory that returns lists of dictionaries fit DirectMessage model with the given pagination. *Parameter:* - *from_username*: user who is send the message. - *to_username*: user who is received the message. - *last_message_retrieved_id*: id of last message retrieved *Returns:* - *models.Notification object* """ if last_message_retrieved_id is not None: try: last_message_retrieved_id = int(last_message_retrieved_id) except ValueError: raise if actions.is_user(from_username) is False: raise Exception('Username who sent this message does not exist.') if actions.is_user(to_username) is False: raise Exception( 'Username who want to receive this message does not exist.') messages = query_factory.get_messages(from_username, to_username) try: messages = actions.paginate( dictionaries_list=messages, required_size=20, start_after_key='id', start_after_value=last_message_retrieved_id) except TypeError: raise if messages is None: return None message_list = [] if len(messages) == 0: return message_list for message in messages: message['created_at'] = change_time(message['created_at']) message_list.append(DirectMessage(message)) return message_list
def get_notifications(notified_username, last_notification_retrieved_id=None): """ This function get list of notifications for a given username by converting lists of dictionaries into lists of notification model, it call fucntion from query factory that returns lists of dictionaries fit notification model with the given pagination. *Parameter:* - *username*: user who will be notified . - *last_notification_retrieved_id*: id of last notification retrieved *Returns*: - *models.Notification object* """ if last_notification_retrieved_id is not None: try: last_notification_retrieved_id = int( last_notification_retrieved_id) except ValueError: raise notifications = query_factory.get_notifications(notified_username) try: notifications = actions.paginate( dictionaries_list=notifications, required_size=20, start_after_key='id', start_after_value=last_notification_retrieved_id) except TypeError: raise if notifications is None: return None notification_list = [] if len(notifications) == 0: return notification_list for notification in notifications: notification['created_at'] = action.change_time( notification['created_at']) notification_list.append(Notification(notification)) return notification_list
def search_user(authorized_username, search_key, username, results_size=size): """ The function returns a list of users searched by search_key. *Parameters*: - *authorized_username (string)*: The user that is logged in now. - *search_key (string)*: The keyword used to get best match users. - *username (string)*: The last username retrieve. Results after this one are fetched. *Returns*: - *User_list*: a list of objects of user_profile. """ if search_key == "": return [] results = query_factory.search_user(search_key) try: results = actions.paginate(dictionaries_list=results, required_size=results_size, start_after_key='username', start_after_value=username) except TypeError as E: print(E) raise if results is None: return None user_list = [] for result in results: result["followers_count"] = query_factory.get_user_followers( result['username'])["count"] result["following_count"] = query_factory.get_user_following( result['username'])["count"] result["kweeks_count"] = query_factory.get_number_of_kweeks( result['username'])['count'] result["likes_count"] = query_factory.get_number_of_likes( result['username'])['count'] friendship = actions.get_friendship(authorized_username, result['username']) result.update(friendship) user_list.append(UserProfile(result)) return user_list
def get_conversations(auth_username, last_conversations_retrieved_id=None): """ This function get list of conversation between two users converting lists of dictionaries into lists of Conversation model, it calls function from query factory that returns lists of dictionaries fit Conversation model with the given pagination. *Parameter:* - *auth_username*: user who is logging in. - *last_conversations_retrieved_id*: id of last message retrieved *Returns:* - *models.Conversation object* """ if last_conversations_retrieved_id is not None: try: last_conversations_retrieved_id = int( last_conversations_retrieved_id) except ValueError: raise if actions.is_user(auth_username) is False: raise Exception('Username who sent this message does not exist.') conversations = query_factory.get_conversations(auth_username) try: conversations = actions.paginate( dictionaries_list=conversations, required_size=20, start_after_key='id', start_after_value=last_conversations_retrieved_id) except TypeError: raise if conversations is None: return None conversation_list = [] if len(conversations) == 0: return conversation_list for conversation in conversations: to_username = conversation['to_username'] from_username = conversation['from_username'] dictionary = {'id': conversation['id']} temp = {'from_username': from_username} dictionary.update(temp) temp = {'to_username': to_username} dictionary.update(temp) new_format = change_time(conversation['created_at']) temp = {'created_at': new_format} dictionary.update(temp) temp = {'text': conversation['text']} dictionary.update(temp) temp = {'media_url': conversation['media_url']} dictionary.update(temp) direct_message = DirectMessage(dictionary) dic = {'last_message': direct_message} if to_username == auth_username: username = from_username else: username = to_username dictionary = {'username': username} temp = {'screen_name': conversation['screen_name']} dictionary.update(temp) temp = {'profile_image_url': conversation['profile_image_url']} dictionary.update(temp) flag = check_follow(auth_username, username) temp = {'following': flag} dictionary.update(temp) flag = check_follow(username, auth_username) temp = {'follows_you': flag} dictionary.update(temp) flag = check_block(auth_username, username) temp = {'blocked': flag} dictionary.update(temp) flag = check_mute(auth_username, username) temp = {'muted': flag} dictionary.update(temp) user = User(dictionary) dic2 = {'user': user} dic2.update(dic) conversation_list.append(Conversation(dic2).to_json()) return conversation_list