mirror of
https://github.com/hay-kot/mealie.git
synced 2025-08-22 22:43:34 -07:00
import / export
This commit is contained in:
parent
e8be339c43
commit
ab8c0d7e97
4 changed files with 38 additions and 3 deletions
|
@ -1,4 +1,5 @@
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
from uuid import uuid4
|
||||||
|
|
||||||
from mealie.db.models.model_base import BaseMixins, SqlAlchemyBase
|
from mealie.db.models.model_base import BaseMixins, SqlAlchemyBase
|
||||||
from mealie.db.models.recipe.recipe import RecipeModel
|
from mealie.db.models.recipe.recipe import RecipeModel
|
||||||
|
@ -6,20 +7,30 @@ from mealie.db.models.users import User
|
||||||
from sqlalchemy import Column, DateTime, ForeignKey, Integer, String, orm
|
from sqlalchemy import Column, DateTime, ForeignKey, Integer, String, orm
|
||||||
|
|
||||||
|
|
||||||
|
def generate_uuid():
|
||||||
|
return str(uuid4())
|
||||||
|
|
||||||
|
|
||||||
class RecipeComment(SqlAlchemyBase, BaseMixins):
|
class RecipeComment(SqlAlchemyBase, BaseMixins):
|
||||||
__tablename__ = "recipe_comments"
|
__tablename__ = "recipe_comments"
|
||||||
id = Column(Integer, primary_key=True)
|
id = Column(Integer, primary_key=True)
|
||||||
parent_id = Column(Integer, ForeignKey("recipes.id"))
|
uuid = Column(Integer, unique=True, nullable=False, default=generate_uuid)
|
||||||
|
parent_id = Column(Integer, ForeignKey("recipes.id"), nullable=False)
|
||||||
recipe = orm.relationship("RecipeModel", back_populates="comments")
|
recipe = orm.relationship("RecipeModel", back_populates="comments")
|
||||||
user_id = Column(Integer, ForeignKey("users.id"), nullable=False)
|
user_id = Column(Integer, ForeignKey("users.id"), nullable=False)
|
||||||
user = orm.relationship("User", back_populates="comments", single_parent=True, foreign_keys=[user_id])
|
user = orm.relationship("User", back_populates="comments", single_parent=True, foreign_keys=[user_id])
|
||||||
date_added = Column(DateTime, default=datetime.now)
|
date_added = Column(DateTime, default=datetime.now)
|
||||||
text = Column(String)
|
text = Column(String)
|
||||||
|
|
||||||
def __init__(self, recipe_slug, user, text, session, **_) -> None:
|
def __init__(self, recipe_slug, user, text, session, date_added=None, **_) -> None:
|
||||||
self.text = text
|
self.text = text
|
||||||
self.user = User.get_ref(session, user)
|
|
||||||
self.recipe = RecipeModel.get_ref(session, recipe_slug, "slug")
|
self.recipe = RecipeModel.get_ref(session, recipe_slug, "slug")
|
||||||
|
self.date_added = date_added or datetime.now()
|
||||||
|
|
||||||
|
if isinstance(user, dict):
|
||||||
|
user = user.get("id")
|
||||||
|
|
||||||
|
self.user = User.get_ref(session, user)
|
||||||
|
|
||||||
def update(self, text, **_) -> None:
|
def update(self, text, **_) -> None:
|
||||||
self.text = text
|
self.text = text
|
||||||
|
|
|
@ -28,6 +28,7 @@ class CommentSaveToDB(CommentIn):
|
||||||
|
|
||||||
class CommentOut(CommentIn):
|
class CommentOut(CommentIn):
|
||||||
id: int
|
id: int
|
||||||
|
uuid: str
|
||||||
recipe_slug: str
|
recipe_slug: str
|
||||||
date_added: datetime
|
date_added: datetime
|
||||||
user: UserBase
|
user: UserBase
|
||||||
|
|
|
@ -129,6 +129,9 @@ def backup_all(
|
||||||
db_export.export_items(all_recipes, "recipes", export_list=False, slug_folder=True)
|
db_export.export_items(all_recipes, "recipes", export_list=False, slug_folder=True)
|
||||||
db_export.export_templates(all_recipes)
|
db_export.export_templates(all_recipes)
|
||||||
|
|
||||||
|
all_comments = db.comments.get_all(session)
|
||||||
|
db_export.export_items(all_comments, "comments")
|
||||||
|
|
||||||
if export_settings:
|
if export_settings:
|
||||||
all_settings = db.settings.get_all(session)
|
all_settings = db.settings.get_all(session)
|
||||||
db_export.export_items(all_settings, "settings")
|
db_export.export_items(all_settings, "settings")
|
||||||
|
|
|
@ -6,6 +6,7 @@ from typing import Callable
|
||||||
|
|
||||||
from mealie.core.config import app_dirs
|
from mealie.core.config import app_dirs
|
||||||
from mealie.db.database import db
|
from mealie.db.database import db
|
||||||
|
from mealie.schema.comments import CommentOut
|
||||||
from mealie.schema.event_notifications import EventNotificationIn
|
from mealie.schema.event_notifications import EventNotificationIn
|
||||||
from mealie.schema.recipe import Recipe
|
from mealie.schema.recipe import Recipe
|
||||||
from mealie.schema.restore import (
|
from mealie.schema.restore import (
|
||||||
|
@ -85,6 +86,22 @@ class ImportDatabase:
|
||||||
|
|
||||||
return imports
|
return imports
|
||||||
|
|
||||||
|
def import_comments(self):
|
||||||
|
comment_dir: Path = self.import_dir.joinpath("comments", "comments.json")
|
||||||
|
|
||||||
|
comments = ImportDatabase.read_models_file(file_path=comment_dir, model=CommentOut)
|
||||||
|
|
||||||
|
for comment in comments:
|
||||||
|
comment: CommentOut
|
||||||
|
|
||||||
|
self.import_model(
|
||||||
|
db_table=db.comments,
|
||||||
|
model=comment,
|
||||||
|
return_model=ThemeImport,
|
||||||
|
name_attr="uuid",
|
||||||
|
search_key="uuid",
|
||||||
|
)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _recipe_migration(recipe_dict: dict) -> dict:
|
def _recipe_migration(recipe_dict: dict) -> dict:
|
||||||
if recipe_dict.get("categories", False):
|
if recipe_dict.get("categories", False):
|
||||||
|
@ -364,6 +381,9 @@ def import_database(
|
||||||
if import_notifications:
|
if import_notifications:
|
||||||
notification_report = import_session.import_notifications()
|
notification_report = import_session.import_notifications()
|
||||||
|
|
||||||
|
if import_recipes:
|
||||||
|
import_session.import_comments()
|
||||||
|
|
||||||
import_session.clean_up()
|
import_session.clean_up()
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue