示例#1
0
def update_profile(profile_id, username, first_name, last_name, picture, picture_id, private):
    try:
        data = {
            'username': username,
            'first_name': first_name,
            'last_name': last_name,
            'picture': picture,
            'picture_id': ObjectId(picture_id),
            'private': private
        }
        old_profile_obj = mongo_client_obj.fetch_one_data(Profile,
                                                          {'_id': ObjectId(profile_id)})
        # delete previous picture
        if old_profile_obj.picture_id != picture_id:
            delete_picture(old_profile_obj.picture_id)
        # update whole object
        profile_obj = mongo_client_obj.update_data(Profile,
                                                   {'_id': ObjectId(profile_id)},
                                                   {"$set": data},
                                                   upsert=False)
        profile_dict = make_dict_embedded_profile(profile_obj)
        output_profile_obj = OutputProfile(**profile_dict)
        return output_profile_obj
    except Exception as e:
        logger.error("update_profile/base : " + str(e))
        raise Exception(str(e))
示例#2
0
def get_profile(profile_id):
    try:
        profile_obj = mongo_client_obj.fetch_one_data(Profile, {'_id': ObjectId(profile_id)})
        profile_dict = make_dict_embedded_profile(profile_obj)
        output_profile_obj = OutputProfile(**profile_dict)
        return output_profile_obj
    except Exception as e:
        logger.error("get_profile/base : " + str(e))
        raise Exception(str(e))
示例#3
0
def get_followings(auth, user_id, start_id):
    try:
        profile_of_user = mongo_client_obj.fetch_one_data(Profile,
                                                          {'_id': ObjectId(user_id)})
        if profile_of_user.private:
            existence_of_follow_relation = mongo_client_obj.fetch_one_data(FollowingRelation,
                                                                           {"$and": [
                                                                               {'following': ObjectId(auth)},
                                                                               {'follower': ObjectId(user_id)}
                                                                           ]})
            if existence_of_follow_relation == "Does Not Exist":
                return "not allowed"
            else:
                return show_followings(user_id, start_id)
        else:
            return show_followings(user_id, start_id)
    except Exception as e:
        logger.error("get_followings/base : " + str(e))
        raise Exception(str(e))
示例#4
0
def delete_picture(image_id):
    try:
        image_obj = mongo_client_obj.fetch_one_data(UploadedImage,
                                                    {'_id': ObjectId(image_id)})
        os.remove(image_obj.image_address)
        mongo_client_obj.delete_one_data(UploadedImage,
                                         {'_id': ObjectId(image_id)})
        return "deleted"
    except Exception as e:
        logger.error("delete_picture/base : " + str(e))
        raise Exception(str(e))
示例#5
0
def unlike_post(author_id, post_id):
    try:
        author_obj = mongo_client_obj.fetch_one_data(Profile, {'_id': ObjectId(author_id)})
        unlike_obj = mongo_client_obj.delete_one_data(Like, {"$and": [{'author': {'username': author_obj.username,
                                                                                  'picture': author_obj.picture}},
                                                                      {'post_id': ObjectId(post_id)}]})
        mongo_client_obj.update_data(Post,
                                     {'_id': ObjectId(post_id)},
                                     {"$inc": {'likes': -1}},
                                     upsert=False)
        return "unlike"
    except Exception as e:
        logger.error("unlike_post/base" + str(e))
        raise Exception(str(e))
示例#6
0
def start_to_follow(following_id, follower_id):
    try:
        following_id = ObjectId(following_id)
        follower_id = ObjectId(follower_id)

        follower_profile = mongo_client_obj.fetch_one_data(Profile,
                                                           {'_id': follower_id})
        if follower_profile.private:
            return request_to_follow(applicant_user=following_id, requested_user=follower_id)
        else:
            return follow(following_id, follower_id)

    except Exception as e:
        logger.error("start_to_follow/base : " + str(e))
        raise Exception(str(e))
示例#7
0
def like_post(author_id, post_id):
    try:
        author_obj = mongo_client_obj.fetch_one_data(Profile, {'_id': ObjectId(author_id)})
        like_obj = mongo_client_obj.insert_one_data(Like, {'author': {
                                                                'username': author_obj.username,
                                                                'picture': author_obj.picture},
                                                           'post_id': ObjectId(post_id)})
        # increase the number of post's like
        mongo_client_obj.update_data(Post,
                                     {'_id': ObjectId(post_id)},
                                     {"$inc": {'likes': 1}},
                                     upsert=False)
        return "like"
    except Exception as e:
        logger.error("like_post/base" + str(e))
        raise Exception(str(e))
示例#8
0
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))
示例#9
0
def post_comment(post_id, comment_text, tags, author_id, date):
    try:
        author_obj = mongo_client_obj.fetch_one_data(Profile, {'_id': ObjectId(author_id)})
        data = {
            'post_id': post_id,
            'comment_text': comment_text,
            'author': {
                'username': author_obj.username,
                'picture': author_obj.picture
            },
            'tags': tags,
            'date': date
        }
        comment_obj = mongo_client_obj.insert_one_data(Comment, data)
        comment_dict = make_dict_embedded_comment(author_obj.username, author_obj.picture,
                                                  comment_obj.comment_text, comment_obj.date)
        output_comment = EmbeddedComment(**comment_dict)
        return output_comment
    except Exception as e:
        logger.error("post_comment/base : " + str(e))
        raise Exception(str(e))
示例#10
0
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))