Exemple #1
0
def import_from_archive(file_name: str) -> list:
    successful_imports = []

    file_path = BACKUP_DIR.joinpath(file_name)

    with zipfile.ZipFile(file_path, "r") as zip_ref:
        zip_ref.extractall(TEMP_DIR)

    recipe_dir = TEMP_DIR.joinpath("recipes")
    for recipe in recipe_dir.glob("*.json"):
        with open(recipe, "r") as f:
            recipe_dict = json.loads(f.read())
            del recipe_dict["_id"]
            del recipe_dict["dateAdded"]

            recipeDoc = RecipeDocument(**recipe_dict)
            try:
                recipeDoc.save()
                successful_imports.append(recipe.stem)

            except:
                print("Failed Import:", recipe.stem)

    image_dir = TEMP_DIR.joinpath("images")
    for image in image_dir.iterdir():
        if image.stem in successful_imports:
            shutil.copy(image, IMG_DIR)

    shutil.rmtree(TEMP_DIR)
    return successful_imports
Exemple #2
0
def import_from_archive(file_name: str) -> list:
    successful_imports = []
    failed_imports = []

    file_path = BACKUP_DIR.joinpath(file_name)

    with zipfile.ZipFile(file_path, "r") as zip_ref:
        zip_ref.extractall(TEMP_DIR)

    recipe_dir = TEMP_DIR.joinpath("recipes")
    for recipe in recipe_dir.glob("*.json"):
        with open(recipe, "r") as f:
            recipe_dict = json.loads(f.read())

            try:
                recipe_dict = import_migration(recipe_dict)
                recipeDoc = RecipeDocument(**recipe_dict)
                recipeDoc.save()
                successful_imports.append(recipe.stem)
            except:
                logger.info(f"Failed Import: {recipe.stem}")
                failed_imports.append(recipe.stem)

    image_dir = TEMP_DIR.joinpath("images")
    for image in image_dir.iterdir():
        if image.stem in successful_imports:
            shutil.copy(image, IMG_DIR)

    shutil.rmtree(TEMP_DIR)

    return {"successful": successful_imports, "failed": failed_imports}
Exemple #3
0
    def save_to_db(self) -> str:
        recipe_dict = self.dict()

        extension = Path(recipe_dict["image"]).suffix
        recipe_dict["image"] = recipe_dict.get("slug") + extension

        try:
            total_time = recipe_dict.get("totalTime")
            recipe_dict["totalTime"] = str(total_time)
        except:
            pass

        recipeDoc = RecipeDocument(**recipe_dict)
        recipeDoc.save()

        return recipeDoc.slug