def add(category_name, title, content): category = CategoryOperations.get_category(category_name) published_datetime = datetime.utcnow() blog = Blog(category=category, title=title, content=content, published_dateTime=published_datetime) blog.save()
def delete_tag(blog_id, name): blogs = Blog.objects(id=blog_id) if len(blogs) == 1: blog = blogs.first() if blog.tags is not None: blog.tags.remove(name) blog.save()
def add_tag(blog_id, name): blogs = Blog.objects(id=blog_id) if len(blogs) == 1: blog = blogs.first() if blog.tags is None: blog.tags = [] blog.tags.append(name) blog.save()
def user_viewed(blog_id): blogs = Blog.objects(id=blog_id) if len(blogs) == 1: blog = blogs.first() if blog.viewed_numbers is None: blog.viewed_numbers = 0 blog.viewed_numbers += 1 blog.save()
def dislike(blog_id): blogs = Blog.objects(id=blog_id) if len(blogs) == 1: blog = blogs.first() if blog.dislikes is None: blog.dislikes = 0 blog.dislikes += 1 blog.save()
def modify(blog_id, category_name, title, content): blogs = Blog.objects(id=blog_id) if len(blogs) == 1: blog = blogs.first() blog.category.name = category_name blog.title = title blog.content = content blog.last_modified_dateTime = datetime.utcnow() blog.save()
def add_comment(blog_id, username, content): blogs = Blog.objects(id=blog_id) if len(blogs) == 1: blog = blogs.first() comment = Comment(username=username, content=content, published_datetime=datetime.utcnow()) if blog.comments is None: blog.comments = [] blog.comments.insert(0, comment) blog.save()
def delete_comment(blog_id, comment_id): blogs = Blog.objects(id=blog_id) if len(blogs) == 1: blog = blogs.first() if blog.comments is not None: idx = -1 for index, item in enumerate(blog.comments): if item.oid == comment_id: idx = index break if idx != -1: blog.comments.pop(idx) blog.save()
def dislike_comment(blog_id, comment_id): blogs = Blog.objects(id=blog_id) if len(blogs) == 1: blog = blogs.first() if blog.comments is not None: idx = -1 for index, item in enumerate(blog.comments): if item.oid == comment_id: idx = index break if idx != -1: if blog.comments[idx].dislikes is None: blog.comments[idx].dislikes = 0 blog.comments[idx].dislikes += 1 blog.save()
def add_reply(blog_id, comment_id, username, content): blogs = Blog.objects(id=blog_id) if len(blogs) == 1: blog = blogs.first() if blog.comments is not None: idx = -1 for index, item in enumerate(blog.comments): if item.oid == comment_id: idx = index break if idx != -1: reply = Reply(username=username, content=content, published_datetime=datetime.utcnow()) if blog.comments[idx].replies is None: blog.comments[idx].replies = [] blog.comments[idx].replies.append(reply) blog.save()
def delete_reply(blog_id, comment_id, reply_id): blogs = Blog.objects(id=blog_id) if len(blogs) == 1: blog = blogs.first() if blog.comments is not None: idx = -1 for index, item in enumerate(blog.comments): if item.oid == comment_id: idx = index break if idx != -1: idx2 = -1 for index, item in enumerate(blog.comments[idx].replies): if item.oid == reply_id: idx2 = index break if idx2 != -1: blog.comments[idx].replies.pop(idx2) blog.save()
def delete(blog_id): blogs = Blog.objects(id=blog_id) if len(blogs) == 1: blog = blogs.first() blog.delete()