db to session

This commit is contained in:
hay-kot 2021-02-09 19:57:53 -09:00
commit 664b50c7f0
7 changed files with 37 additions and 48 deletions

View file

@ -32,10 +32,10 @@ def available_imports():
@router.post("/export/database", status_code=201) @router.post("/export/database", status_code=201)
def export_database(data: BackupJob, db: Session = Depends(generate_session)): def export_database(data: BackupJob, session: Session = Depends(generate_session)):
"""Generates a backup of the recipe database in json format.""" """Generates a backup of the recipe database in json format."""
export_path = backup_all( export_path = backup_all(
session=db, session=session,
tag=data.tag, tag=data.tag,
templates=data.templates, templates=data.templates,
export_recipes=data.options.recipes, export_recipes=data.options.recipes,
@ -80,12 +80,12 @@ async def upload_nextcloud_zipfile(file_name: str):
@router.post("/{file_name}/import", status_code=200) @router.post("/{file_name}/import", status_code=200)
def import_database( def import_database(
file_name: str, import_data: ImportJob, db: Session = Depends(generate_session) file_name: str, import_data: ImportJob, session: Session = Depends(generate_session)
): ):
""" Import a database backup file generated from Mealie. """ """ Import a database backup file generated from Mealie. """
import_db = ImportDatabase( import_db = ImportDatabase(
session=db, session=session,
zip_archive=import_data.name, zip_archive=import_data.name,
import_recipes=import_data.recipes, import_recipes=import_data.recipes,
force_import=import_data.force, force_import=import_data.force,

View file

@ -27,18 +27,7 @@ async def get_log(num: int):
""" Doc Str """ """ Doc Str """
with open(LOGGER_FILE, "rb") as f: with open(LOGGER_FILE, "rb") as f:
log_text = tail(f, num) log_text = tail(f, num)
HTML_RESPONSE = f""" HTML_RESPONSE = log_text
<html>
<head>
<title>Mealie Log</title>
</head>
<body style="white-space: pre-line">
<p>
{log_text}
</p>
</body>
</html>
"""
return HTML_RESPONSE return HTML_RESPONSE

View file

@ -10,53 +10,53 @@ router = APIRouter(prefix="/api/meal-plans", tags=["Meal Plan"])
@router.get("/all", response_model=List[MealPlan]) @router.get("/all", response_model=List[MealPlan])
def get_all_meals(db: Session = Depends(generate_session)): def get_all_meals(session: Session = Depends(generate_session)):
""" Returns a list of all available Meal Plan """ """ Returns a list of all available Meal Plan """
return MealPlan.get_all(db) return MealPlan.get_all(session)
@router.post("/create") @router.post("/create")
def set_meal_plan(data: MealPlan, db: Session = Depends(generate_session)): def set_meal_plan(data: MealPlan, session: Session = Depends(generate_session)):
""" Creates a meal plan database entry """ """ Creates a meal plan database entry """
data.process_meals(db) data.process_meals(session)
data.save_to_db(db) data.save_to_db(session)
return SnackResponse.success("Mealplan Created") return SnackResponse.success("Mealplan Created")
@router.get("/this-week", response_model=MealPlan) @router.get("/this-week", response_model=MealPlan)
def get_this_week(db: Session = Depends(generate_session)): def get_this_week(session: Session = Depends(generate_session)):
""" Returns the meal plan data for this week """ """ Returns the meal plan data for this week """
return MealPlan.this_week(db) return MealPlan.this_week(session)
@router.put("/{plan_id}") @router.put("/{plan_id}")
def update_meal_plan( def update_meal_plan(
plan_id: str, meal_plan: MealPlan, db: Session = Depends(generate_session) plan_id: str, meal_plan: MealPlan, session: Session = Depends(generate_session)
): ):
""" Updates a meal plan based off ID """ """ Updates a meal plan based off ID """
meal_plan.process_meals(db) meal_plan.process_meals(session)
meal_plan.update(db, plan_id) meal_plan.update(session, plan_id)
return SnackResponse.info("Mealplan Updated") return SnackResponse.info("Mealplan Updated")
@router.delete("/{plan_id}") @router.delete("/{plan_id}")
def delete_meal_plan(plan_id, db: Session = Depends(generate_session)): def delete_meal_plan(plan_id, session: Session = Depends(generate_session)):
""" Removes a meal plan from the database """ """ Removes a meal plan from the database """
MealPlan.delete(db, plan_id) MealPlan.delete(session, plan_id)
return SnackResponse.error("Mealplan Deleted") return SnackResponse.error("Mealplan Deleted")
@router.get("/today", tags=["Meal Plan"]) @router.get("/today", tags=["Meal Plan"])
def get_today(db: Session = Depends(generate_session)): def get_today(session: Session = Depends(generate_session)):
""" """
Returns the recipe slug for the meal scheduled for today. Returns the recipe slug for the meal scheduled for today.
If no meal is scheduled nothing is returned If no meal is scheduled nothing is returned
""" """
return MealPlan.today(db) return MealPlan.today(session)

View file

@ -37,14 +37,14 @@ def get_avaiable_nextcloud_imports():
@router.post("/{type}/{file_name}/import") @router.post("/{type}/{file_name}/import")
def import_nextcloud_directory( def import_nextcloud_directory(
type: str, file_name: str, db: Session = Depends(generate_session) type: str, file_name: str, session: Session = Depends(generate_session)
): ):
""" Imports all the recipes in a given directory """ """ Imports all the recipes in a given directory """
file_path = MIGRATION_DIR.joinpath(type, file_name) file_path = MIGRATION_DIR.joinpath(type, file_name)
if type == "nextcloud": if type == "nextcloud":
return nextcloud_migrate(db, file_path) return nextcloud_migrate(session, file_path)
elif type == "chowdown": elif type == "chowdown":
return chowdow_migrate(db, file_path) return chowdow_migrate(session, file_path)
else: else:
return SnackResponse.error("Incorrect Migration Type Selected") return SnackResponse.error("Incorrect Migration Type Selected")

View file

@ -9,10 +9,10 @@ router = APIRouter(prefix="/api/site-settings", tags=["Settings"])
@router.get("") @router.get("")
def get_main_settings(db: Session = Depends(generate_session)): def get_main_settings(session: Session = Depends(generate_session)):
""" Returns basic site settings """ """ Returns basic site settings """
return SiteSettings.get_site_settings(db) return SiteSettings.get_site_settings(session)
@router.post("/webhooks/test") @router.post("/webhooks/test")
@ -23,8 +23,8 @@ def test_webhooks():
@router.put("") @router.put("")
def update_settings(data: SiteSettings, db: Session = Depends(generate_session)): def update_settings(data: SiteSettings, session: Session = Depends(generate_session)):
""" Returns Site Settings """ """ Returns Site Settings """
data.update(db) data.update(session)
return SnackResponse.success("Settings Updated") return SnackResponse.success("Settings Updated")

View file

@ -8,39 +8,39 @@ router = APIRouter(prefix="/api", tags=["Themes"])
@router.get("/themes") @router.get("/themes")
def get_all_themes(db: Session = Depends(generate_session)): def get_all_themes(session: Session = Depends(generate_session)):
""" Returns all site themes """ """ Returns all site themes """
return SiteTheme.get_all(db) return SiteTheme.get_all(session)
@router.post("/themes/create") @router.post("/themes/create")
def create_theme(data: SiteTheme, db: Session = Depends(generate_session)): def create_theme(data: SiteTheme, session: Session = Depends(generate_session)):
""" Creates a site color theme database entry """ """ Creates a site color theme database entry """
data.save_to_db(db) data.save_to_db(session)
return SnackResponse.success("Theme Saved") return SnackResponse.success("Theme Saved")
@router.get("/themes/{theme_name}") @router.get("/themes/{theme_name}")
def get_single_theme(theme_name: str, db: Session = Depends(generate_session)): def get_single_theme(theme_name: str, session: Session = Depends(generate_session)):
""" Returns a named theme """ """ Returns a named theme """
return SiteTheme.get_by_name(db, theme_name) return SiteTheme.get_by_name(session, theme_name)
@router.put("/themes/{theme_name}") @router.put("/themes/{theme_name}")
def update_theme( def update_theme(
theme_name: str, data: SiteTheme, db: Session = Depends(generate_session) theme_name: str, data: SiteTheme, session: Session = Depends(generate_session)
): ):
""" Update a theme database entry """ """ Update a theme database entry """
data.update_document(db) data.update_document(session)
return SnackResponse.info(f"Theme Updated: {theme_name}") return SnackResponse.info(f"Theme Updated: {theme_name}")
@router.delete("/themes/{theme_name}") @router.delete("/themes/{theme_name}")
def delete_theme(theme_name: str, db: Session = Depends(generate_session)): def delete_theme(theme_name: str, session: Session = Depends(generate_session)):
""" Deletes theme from the database """ """ Deletes theme from the database """
SiteTheme.delete_theme(db, theme_name) SiteTheme.delete_theme(session, theme_name)
return SnackResponse.error(f"Theme Deleted: {theme_name}") return SnackResponse.error(f"Theme Deleted: {theme_name}")

View file

@ -110,7 +110,7 @@ class ExportDatabase:
ExportDatabase._write_json_file(meal_plans, out_file) ExportDatabase._write_json_file(meal_plans, out_file)
@staticmethod @staticmethod
def _write_json_file(data, out_file: Path): def _write_json_file(data: dict, out_file: Path):
json_data = json.dumps(data, indent=4, default=str) json_data = json.dumps(data, indent=4, default=str)
with open(out_file, "w") as f: with open(out_file, "w") as f: