def find_blogs(blog_config: BlogConfig, query: dict) -> List['Blog']: db = Database(uri=blog_config.uri, db_name=blog_config.db_name) results = db.find(collection_name=blog_config.collection_name_blogs, query=query) results = [Blog.wrap_result(result) for result in results] if results else results return results
def find_posts(uri: str, db_name: str, collection_name: str, query: Dict) -> List['BlogPost']: db = Database(uri=uri, db_name=db_name) results = db.find(collection_name=collection_name, query=query) results = [BlogPost.wrap_result(result) for result in results] if results else results return results
def create_blog(self, blog_config: BlogConfig) -> None: query = {"_id": self._id} results = Blog.find_blogs(blog_config=blog_config, query=query) if len(results) == 0: logger.debug("Creating new blog titled '{}'".format(self.title)) db = Database(uri=blog_config.uri, db_name=blog_config.db_name) db.insert(collection_name=blog_config.collection_name_blogs, data=self.__dict__) else: logger.debug('This blog exists already. ID={}'.format(self._id))
def register(cls, blog_config: BlogConfig, email: str, password: str) -> bool: result = cls.get_by_email(blog_config=blog_config, email=email) if result: # User already exists return False # User does not exist db = Database(uri=blog_config.uri, db_name=blog_config.db_name) user = User(email=email, password=password) db.insert(collection_name=blog_config.collection_name_users, data=user.__dict__) session['email'] = email return True
def find_post(uri: str, db_name: str, collection_name: str, query: Dict) -> 'BlogPost': db = Database(uri=uri, db_name=db_name) result = db.find_one(collection_name=collection_name, query=query) result = BlogPost.wrap_result(result) if result else result return result
def post_to_db(self, uri: str, db_name: str, collection_name: str) -> None: db = Database(uri=uri, db_name=db_name) db.insert(collection_name=collection_name, data=self.__dict__)
def get_by_id(blog_config: BlogConfig, _id: str) -> 'User': db = Database(uri=blog_config.uri, db_name=blog_config.db_name) result = db.find_one(collection_name=blog_config.collection_name_users, query={'_id': _id}) result = User.wrap_result(result) if result else result return result
def find_blog(blog_config: BlogConfig, query: dict) -> 'Blog': db = Database(uri=blog_config.uri, db_name=blog_config.db_name) result = db.find_one(collection_name=blog_config.collection_name_blogs, query=query) result = Blog.wrap_result(result) if result else result return result