def get_comments(post_id): comments_data = [ comment for comment in Database.find(collection='comments', query={'post_id': post_id}) ] comments = [] for comment_data in comments_data: comments += [ Comment(post_id=comment_data['post_id'], content=comment_data['content'], created_date=comment_data['created_date'], author=comment_data['author'], _id=comment_data['_id']) ] result = [] for comment in comments: result += [{ 'post_id': comment.post_id, 'content': comment.content, 'created_date': str(comment.created_date.ctime()), '_id': comment._id, 'author': comment.author }] result.reverse() return result
def search(string): string = str(string).strip() posts_data = [post for post in Database.find(collection='posts', query={"title": {'$regex': ".*" + string.capitalize() + ".*"}})] posts_data += [post for post in Database.find(collection='posts', query={"title": {'$regex': ".*" + string + ".*"}})] posts_data = Post.remove_duplicates(posts_data) posts = [] for post_data in posts_data: s = str(post_data['content']) if len(s) < 750: posts += [Post(title=post_data['title'], content=s, author=post_data['author'], created_date=post_data['created_date'], _id=post_data['_id'])] else: limit = 750 for i in range(limit, 0, -1): if s[i] is " ": limit = i break s = s[:limit] posts += [Post(title=post_data['title'], content=s + " ...", author=post_data['author'], created_date=post_data['created_date'], _id=post_data['_id'])] result = [] for post in posts: result += [{ 'title': post.title, 'content': post.content, 'author': post.author, 'created_date': str(post.created_date.ctime()), '_id': post._id }] result.reverse() return result
def get_posts(): posts_data = [post for post in Database.find(collection='posts', query={})] posts = [] for post_data in posts_data: s = str(post_data['content']) if len(s) < 750: posts += [Post(title=post_data['title'], content=s, author=post_data['author'], created_date=post_data['created_date'], _id=post_data['_id'])] else: limit = 750 for i in range(limit, 0, -1): if s[i] is " ": limit = i break s = s[:limit] posts += [Post(title=post_data['title'], content=s + " ...", author=post_data['author'], created_date=post_data['created_date'], _id=post_data['_id'])] result = [] for post in posts: result += [{ 'title': str(post.title).strip(), 'content': str(post.content).strip(), 'author': str(post.author).strip(), 'created_date': str(post.created_date.ctime()), '_id': str(post._id).strip() }] result.reverse() return result