示例#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
示例#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}
示例#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
示例#4
0
def export_recipes(dest_dir: Path, template=None) -> Path:
    all_recipes = RecipeDocument.objects()
    for recipe in all_recipes:
        json_recipe = recipe.to_json(indent=4)

        if template:
            md_dest = dest_dir.parent.joinpath("templates")
            md_dest.mkdir(parents=True, exist_ok=True)
            template = TEMPLATE_DIR.joinpath(template)
            export_markdown(md_dest, json_recipe, template)

        filename = recipe.slug + ".json"
        file_path = dest_dir.joinpath(filename)

        with open(file_path, "w") as f:
            f.write(json_recipe)