def init_db(): db.create_all() Product.insert_default() User.insert_default() Method.insert_default() Store.insert_default() Status.insert_default() print('Default products had been created ...')
def remove_category(self, db: Session, db_obj: Product, category: Category): db_obj.categories = list( filter(lambda c: c.id != category.id, db_obj.categories)) db.add(db_obj) db.commit() db.refresh(db_obj)
def add_images(self, db: Session, db_obj: Product, images: List[Image]) -> List[Image]: db_obj.images = [*db_obj.images, *images] db.add(db_obj) db.commit() db.refresh(db_obj) return db_obj.images
def add_categories(self, db: Session, db_obj: Product, categories: List[Category]) -> List[Category]: temp = [*db_obj.categories, *categories] db_obj.categories = temp db.add(db_obj) db.commit() db.refresh(db_obj) return temp
def remove_image(self, db: Session, db_obj: Product, image: Image): db_obj.images = list( filter(lambda img: img.url != image.url, db_obj.images)) db.add(db_obj) db.commit() db.refresh(db_obj)