def search_tags(tag, start_id): try: if tag[0] == '#': tag = tag[1:] if start_id is None: list_of_founded_post_dict = mongo_client_obj.fetch_data(Post, {'tags': {"$regex": "^" + tag + "*"}}, per_page_limit=per_page_limit) else: list_of_founded_post_dict = mongo_client_obj.fetch_data(Post, {"$and": [ {'_id': {"$lt": ObjectId(start_id)}}, {'tags': {"$regex": "^" + tag + "*"}}]}, per_page_limit=per_page_limit) list_of_post = [] for post_dict in list_of_founded_post_dict: post_obj = Post(**post_dict) founded_post_dict = make_dict_embedded_post(post_obj) list_of_post.append({'post': founded_post_dict}) start_id, has_continue = pagination(list_of_founded_post_dict, per_page_limit) output_founded_post = OutputPagePost(start_id=start_id, has_continue=has_continue, posts=list_of_post) return output_founded_post except Exception as e: logger.error("search_tags/base : " + str(e)) raise Exception(str(e))
def search_account(username, start_id): try: if start_id is None: list_of_founded_profile = mongo_client_obj.fetch_data(Profile, {'username': {"$regex": "^" + username + "*"}}, per_page_limit=per_page_limit) else: list_of_founded_profile = mongo_client_obj.fetch_data(Profile, {"$and": [ {'_id': {"$lt": ObjectId(start_id)}}, {'username': {"$regex": "^" + username + "*"}} ]}, per_page_limit=per_page_limit) profiles = [] for profile_dict in list_of_founded_profile: profile_obj = Profile(**profile_dict) founded_profile_dict = make_dict_embedded_profile(profile_obj) profiles.append(founded_profile_dict) start_id, has_continue = pagination(list_of_founded_profile, per_page_limit) output_founded_profile_obj = OutputFollowers(start_id=start_id, has_continue=has_continue, followers=profiles) return output_founded_profile_obj except Exception as e: logger.error("search_account/base : " + str(e)) raise Exception(str(e))
def show_followings(user_id, start_id): try: if start_id is None: list_of_followings = mongo_client_obj.fetch_data(FollowingRelation, {'follower': ObjectId(user_id)}, per_page_limit=per_page_limit) else: list_of_followings = mongo_client_obj.fetch_data(FollowingRelation, {"$and": [ {'_id': {"$lt": ObjectId(start_id)}}, {'follower': ObjectId(user_id)}]}, per_page_limit=per_page_limit) list_of_followings_id = [] for following in list_of_followings: list_of_followings_id.append(following.get("following")) list_of_followings_profile = mongo_client_obj.fetch_data(Profile, {'_id': {"$in": list_of_followings_id}}) followings = [] for following_profile in list_of_followings_profile: profile_obj = Profile(**following_profile) profile_dict = make_dict_embedded_profile(profile_obj) followings.append(profile_dict) start_id, has_continue = pagination(list_of_followings, per_page_limit) output_followings_obj = OutputFollowings(start_id=start_id, has_continue=has_continue, followings=followings) return output_followings_obj except Exception as e: logger.error("show_followings/base : " + str(e)) raise Exception(str(e))
def get_applicant_users(auth, start_id): try: if start_id is None: list_of_follow_request = mongo_client_obj.fetch_data(FollowRequest, {'requested_user': ObjectId(auth)}, per_page_limit=per_page_limit) else: list_of_follow_request = mongo_client_obj.fetch_data(FollowRequest, {"$and": [ {'_id': {"$lt": ObjectId(start_id)}}, {'requested_user': ObjectId(auth)} ]}, per_page_limit=per_page_limit) list_of_applicant_user_id = [] for applicant_dict in list_of_follow_request: list_of_applicant_user_id.append(applicant_dict['applicant_user']) list_of_applicant_profile = mongo_client_obj.fetch_data(Profile, {'_id': {"$in": list_of_applicant_user_id}}) applicant_users = [] for applicant_user in list_of_applicant_profile: applicant_user_obj = Profile(**applicant_user) profile_dict = make_dict_embedded_profile(applicant_user_obj) applicant_users.append(profile_dict) start_id, has_continue = pagination(list_of_follow_request, per_page_limit) output_applicant_users_obj = OutputFollowers(start_id=start_id, has_continue=has_continue, followers=applicant_users) return output_applicant_users_obj except Exception as e: logger.error("get_applicant_users/base : " + str(e)) raise Exception(str(e))
def get_likes(post_id, start_id): try: if start_id is None: list_of_likes = mongo_client_obj.fetch_data(Like, {'post_id': ObjectId(post_id)}, per_page_limit=per_page_limit) else: list_of_likes = mongo_client_obj.fetch_data(Like, {"$and": [ {'_id': {"$lt": ObjectId(start_id)}}, {'post_id': ObjectId(post_id)}]}, per_page_limit=per_page_limit) list_of_likes_dict = [] for like in list_of_likes: author_username = like["author"]["username"] author_picture = like["author"]["picture"] list_of_likes_dict.append({'author': { 'username': author_username, 'picture': author_picture}}) start_id, has_continue = pagination(list_of_likes, per_page_limit) output_like_obj = OutputLike(start_id=start_id, has_continue=has_continue, likes=list_of_likes_dict) return output_like_obj except Exception as e: logger.error("get_likes/base" + str(e)) raise Exception(str(e))
def get_comments(post_id, start_id): try: if start_id is None: list_of_comments = mongo_client_obj.fetch_data(Comment, {'post_id': ObjectId(post_id)}, per_page_limit=per_page_limit) else: list_of_comments = mongo_client_obj.fetch_data(Comment, {"$and": [ {'_id': {"$lt": ObjectId(start_id)}}, {'post_id': ObjectId(post_id)}]}, per_page_limit=per_page_limit) list_of_comments_dict = [] for comment in list_of_comments: author_username = comment["author"]["username"] author_picture = comment["author"]["picture"] comment_dict = make_dict_embedded_comment(author_username, author_picture, comment.get("comment_text"), comment.get("date")) list_of_comments_dict.append(comment_dict) start_id, has_continue = pagination(list_of_comments, per_page_limit) output_comment_obj = OutputComment(start_id=start_id, has_continue=has_continue, comments=list_of_comments_dict) return output_comment_obj except Exception as e: logger.error("get_comments/base" + str(e)) raise Exception(str(e))
def get_page_post(profile_id, start_id): try: if start_id is None: list_of_page_post_dict = mongo_client_obj.fetch_data(Post, {'publisher': ObjectId(profile_id)}, per_page_limit=per_page_limit) else: list_of_page_post_dict = mongo_client_obj.fetch_data(Post, {"$and": [ {'_id': {"$lt": ObjectId(start_id)}}, {'publisher': ObjectId(profile_id)}]}, per_page_limit=per_page_limit) list_of_post_in_page = [] for post_dict in list_of_page_post_dict: post_obj = Post(**post_dict) changed_post_dict = make_dict_embedded_post(post_obj) list_of_post_in_page.append({'post': changed_post_dict}) start_id, has_continue = pagination(list_of_page_post_dict, per_page_limit) output_page_post = OutputPost(start_id=start_id, has_continue=has_continue, posts=list_of_post_in_page) return output_page_post except Exception as e: logger.error("get_page_post/base : " + str(e)) raise Exception(str(e))
def publish_on_home_page(publisher_id, post_id): try: list_of_followings = mongo_client_obj.fetch_data(FollowingRelation, {'follower': publisher_id}) for follow_relation in list_of_followings: following = follow_relation["following"] mongo_client_obj.update_data(HomePage, {'owner': following}, {"$push": {"inclusive_posts": post_id}}, upsert=True) return True except Exception as e: logger.error("publish_on_home_page/base : " + str(e)) raise Exception(str(e))
def get_home_page(owner_id, start_id): try: first_page_obj = mongo_client_obj.fetch_one_data(HomePage, {'owner': ObjectId(owner_id)}) list_of_inclusive_posts_id = first_page_obj.inclusive_posts if start_id is None: list_of_inclusive_posts_dict = mongo_client_obj.fetch_data(Post, {'_id': {"$in": list_of_inclusive_posts_id}}, per_page_limit=per_page_limit) else: list_of_inclusive_posts_dict = mongo_client_obj.fetch_data(Post, {"$and": [ {'_id': {"$in": list_of_inclusive_posts_id}}, {'_id': {"$lt": ObjectId(start_id)}} ]}, per_page_limit=per_page_limit) publisher_id_list = list() for post in list_of_inclusive_posts_dict: publisher_id_list.append(post.get("publisher")) list_of_inclusive_publishers_dict = mongo_client_obj.fetch_data(Profile, {'_id': {"$in": publisher_id_list}}) list_of_post_in_page = [] for post_dict in list_of_inclusive_posts_dict: for profile_dict in list_of_inclusive_publishers_dict: if post_dict.get("publisher") == profile_dict.get("_id"): publisher_obj = Profile(**profile_dict) publisher_dict = make_dict_embedded_profile(publisher_obj) post_obj = Post(**post_dict) changed_post_dict = make_dict_embedded_post(post_obj) list_of_post_in_page.append({'publisher': publisher_dict, 'post': changed_post_dict}) break start_id, has_continue = pagination(list_of_inclusive_posts_dict, per_page_limit) output_first_page = OutputHomePage(start_id=start_id, has_continue=has_continue, posts=list_of_post_in_page) return output_first_page except Exception as e: logger.error("get_home_page/base : " + str(e)) raise Exception(str(e))
def get_blocked_following(auth, start_id): try: if start_id is None: list_of_blocked_following = mongo_client_obj.fetch_data(FollowingRelation, {"$and": [ {'follower': ObjectId(auth)}, {'block': True}]}, per_page_limit=per_page_limit) else: list_of_blocked_following = mongo_client_obj.fetch_data(FollowingRelation, {"$and": [ {'_id': {"$lt": ObjectId(start_id)}}, {'follower': ObjectId(auth)}, {'block': True}]}, per_page_limit=per_page_limit) list_of_blocked_followings_id = [] for blocked_following in list_of_blocked_following: list_of_blocked_followings_id.append(blocked_following.get("following")) list_of_blocked_followings_profile = mongo_client_obj.fetch_data(Profile, {'_id': {"$in": list_of_blocked_followings_id}}) blocked_followings = [] for blocked_following_profile in list_of_blocked_followings_profile: profile_obj = Profile(**blocked_following_profile) profile_dict = make_dict_embedded_profile(profile_obj) blocked_followings.append(profile_dict) start_id, has_continue = pagination(list_of_blocked_following, per_page_limit) output_blocked_followings_obj = OutputFollowings(start_id=start_id, has_continue=has_continue, followings=blocked_followings) return output_blocked_followings_obj except Exception as e: logger.error("get_blocked_following/base : " + str(e)) raise Exception(str(e))
def block_or_unblock_following(action, auth, following_id): if action == "block": try: # blocking = mongo_client_obj.update_data(FollowingRelation, # {"$and": [ # {'following': ObjectId(following_id)}, # {'follower': ObjectId(auth)}]}, # {"$set": {'block': True}}, # upsert=False) # find profile of blocked user blocked_profile = mongo_client_obj.fetch_one_data(Profile, {'_id': ObjectId(following_id)}) blocked_username = blocked_profile.username post_list_of_blocker = mongo_client_obj.fetch_data(Post, {'publisher': ObjectId(auth)}) post_ids = [] for post in post_list_of_blocker: post_ids.append(post["_id"]) print("post:ids : ", post_ids) # TODO : make the error correct remove_comments_of_blocked_user.delay(blocked_username=blocked_username, post_ids=2) # remove_likes_of_blocked_user.delay(blocked_username, post_ids) return "blocked" except Exception as e: logger.error("block/base : " + str(e)) raise Exception(str(e)) elif action == "unblock": try: blocking = mongo_client_obj.update_data(FollowingRelation, {"$and": [ {'following': ObjectId(following_id)}, {'follower': ObjectId(auth)}]}, {"$set": {'block': False}}, upsert=False) return "unblocked" except Exception as e: logger.error("unblock/base : " + str(e)) raise Exception(str(e))