def add_post(author='aush', creation_time=datetime.datetime.now, content='This is just the beginning', likes=0, **kwargs): """ Args: author: username of author of the post, creation_time: date and time of creation of the post Returns: post_id / Error message """ if (check_user(author)): post = Post() post.author = author post.creation_time = creation_time post.content = content post.likes = likes post.save() post_id = post.id user = User.objects(username=author).first() user.posts.append(post_id) user.save() return post_id return None
def readPosts(**kwargs): """ Returns: Returns All Posts Objects in a list """ posts = Post.objects() return posts
def get_post_by_id(post_pk, **kwargs): """ Args: post_pk Returns: Post Object """ post = Post.objects(id=post_pk).first() return post
def deletePost(post_id, **kwargs): """ Args: post_id: id of the post, Returns: Boolean - True/False """ if (check_post(post_id)): post = Post.objects(id=post_id).first() post.delete() return True return False
def change_post_content(post_id, content, **kwargs): """ Args: post_id: id of the post, content: content of the post Returns: Boolean - True/False """ if (check_post(post_id)): post = Post.objects(id=post_id).first() # print(post.content) post.content = content # print(post.content) post.save() return True return False
def reduce_post_likes(post_id, username, **kwargs): """ Args: post_id: id of the post, username: username of the user unliking the post Returns: Boolean - True/False """ if (check_user(username) and check_post(post_id)): post = Post.objects(id=post_id).first() user = User.objects(username=username).first() if (post_id in user.liked_posts): post.likes = post.likes - 1 user.liked_posts.remove(post_id) post.save() user.save() return True return False
def check_post(post_id): post = Post.objects(id=post_id).first() if post is None: return False return True
def get_number_of_likes_by_postid(post_id, **kwargs): post = Post.objects(id=post_id).first() likes = post.likes return likes
all_posts_added = [] all_posts_removed = [] # print('Num Objects in post_update : ', len(posts_updates)) for post_update_object in posts_updates: update_string = post_update_object.update_value updates_TwoPSet = TwoPSet().loadFromDict( json_util.loads(update_string)) added_posts_jsons = updates_TwoPSet.addedValues() for added_post_json in added_posts_jsons: added_post = Post.from_json(added_post_json) all_posts_added.append(added_post) removed_posts_jsons = updates_TwoPSet.removedValues() for removed_post_json in removed_posts_jsons: removed_post = Post.from_json(removed_post_json) all_posts_removed.append(removed_post) # print('-----------------------------Added Posts-------------------------------') # print("Num of all_posts_added : ", len(all_posts_added)) # print_posts(all_posts_added) change_post_content(last_post_id, "The Red Roses") # posts = readPosts(dbName=dbName) # print_posts(posts)