mirror of
https://github.com/hay-kot/mealie.git
synced 2025-08-22 06:23:34 -07:00
docs strings
This commit is contained in:
parent
77f66c43f3
commit
da2a3d2d32
3 changed files with 21 additions and 62 deletions
|
@ -1,62 +0,0 @@
|
||||||
import json
|
|
||||||
import shutil
|
|
||||||
import zipfile
|
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
from db.mongo.recipe_models import RecipeDocument
|
|
||||||
from utils.logger import logger
|
|
||||||
|
|
||||||
from services.recipe_services import IMG_DIR
|
|
||||||
|
|
||||||
CWD = Path(__file__).parent
|
|
||||||
BACKUP_DIR = CWD.parent.joinpath("data", "backups")
|
|
||||||
TEMPLATE_DIR = CWD.parent.joinpath("data", "templates")
|
|
||||||
TEMP_DIR = CWD.parent.joinpath("data", "temp")
|
|
||||||
|
|
||||||
|
|
||||||
def import_migration(recipe_dict: dict) -> dict:
|
|
||||||
del recipe_dict["_id"]
|
|
||||||
del recipe_dict["dateAdded"]
|
|
||||||
|
|
||||||
# Migration from list to Object Type Data
|
|
||||||
if type(recipe_dict["extras"]) == list:
|
|
||||||
recipe_dict["extras"] = {}
|
|
||||||
|
|
||||||
return recipe_dict
|
|
||||||
|
|
||||||
|
|
||||||
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}
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
pass
|
|
|
@ -12,6 +12,14 @@ from utils.logger import logger
|
||||||
|
|
||||||
class ExportDatabase:
|
class ExportDatabase:
|
||||||
def __init__(self, tag=None, templates=None) -> None:
|
def __init__(self, tag=None, templates=None) -> None:
|
||||||
|
"""Export a Mealie database. Export interacts directly with class objects and can be used
|
||||||
|
with any supported backend database platform. By default tags are timestands, and no Jinja2 templates are rendered
|
||||||
|
|
||||||
|
|
||||||
|
Args:
|
||||||
|
tag ([str], optional): A str to be used as a file tag. Defaults to None.
|
||||||
|
templates (list, optional): A list of template file names. Defaults to None.
|
||||||
|
"""
|
||||||
if tag:
|
if tag:
|
||||||
export_tag = tag + "_" + datetime.now().strftime("%Y-%b-%d")
|
export_tag = tag + "_" + datetime.now().strftime("%Y-%b-%d")
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -19,6 +19,19 @@ class ImportDatabase:
|
||||||
force_import: bool = False,
|
force_import: bool = False,
|
||||||
rebase: bool = False,
|
rebase: bool = False,
|
||||||
) -> None:
|
) -> None:
|
||||||
|
"""Import a database.zip file exported from mealie.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
zip_archive (str): The filename contained in the backups directory
|
||||||
|
import_recipes (bool, optional): Import Recipes?. Defaults to True.
|
||||||
|
import_settings (bool, optional): Determines if settings are imported. Defaults to True.
|
||||||
|
import_themes (bool, optional): Determines if themes are imported. Defaults to True.
|
||||||
|
force_import (bool, optional): Force import will update all existing recipes. If False existing recipes are skipped. Defaults to False.
|
||||||
|
rebase (bool, optional): Rebase will first clear the database and then import Recipes. Defaults to False.
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
Exception: If the zip file does not exists an exception raise.
|
||||||
|
"""
|
||||||
|
|
||||||
self.archive = BACKUP_DIR.joinpath(zip_archive)
|
self.archive = BACKUP_DIR.joinpath(zip_archive)
|
||||||
self.imp_recipes = import_recipes
|
self.imp_recipes = import_recipes
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue