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
def edit_comment(id, data): return Database.edit(collection='comments', query={'_id': id}, data=data)
def remove_comment(id): return Database.remove(collection='comments', query={'_id': id})
def get_comment(cls, id): comment_data = Database.find_one(collection='comments', query={'_id': id}) comment = cls(**comment_data) comment.created_date = str(comment.created_date.ctime()) return comment
def save_to_mongo(self): Database.insert(collection='comments', data=self.json())
def get_post(cls, id): post_data = Database.find_one(collection='posts', query={'_id': id}) post = cls(**post_data) post.created_date = str(post.created_date.ctime()) return post
def edit_post(id, data): return Database.edit(collection='posts', query={'_id': id}, data=data)
def remove_post(id): return Database.remove(collection='posts', query={'_id': id})
def initialize_database(): Datebase.initialize()
def get_by_email(cls, email): data = Datebase.find_one(collection='admins', query={'email': email}) if data is not None: return cls(**data)