diff --git a/mealie/db/models/recipe/comment.py b/mealie/db/models/recipe/comment.py index 189b1dfc3..7ccdbe1c7 100644 --- a/mealie/db/models/recipe/comment.py +++ b/mealie/db/models/recipe/comment.py @@ -1,4 +1,5 @@ from datetime import datetime +from uuid import uuid4 from mealie.db.models.model_base import BaseMixins, SqlAlchemyBase 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 +def generate_uuid(): + return str(uuid4()) + + class RecipeComment(SqlAlchemyBase, BaseMixins): __tablename__ = "recipe_comments" 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") user_id = Column(Integer, ForeignKey("users.id"), nullable=False) user = orm.relationship("User", back_populates="comments", single_parent=True, foreign_keys=[user_id]) date_added = Column(DateTime, default=datetime.now) 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.user = User.get_ref(session, user) 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: self.text = text diff --git a/mealie/schema/comments.py b/mealie/schema/comments.py index 6da1c78b3..6abd8121e 100644 --- a/mealie/schema/comments.py +++ b/mealie/schema/comments.py @@ -28,6 +28,7 @@ class CommentSaveToDB(CommentIn): class CommentOut(CommentIn): id: int + uuid: str recipe_slug: str date_added: datetime user: UserBase diff --git a/mealie/services/backups/exports.py b/mealie/services/backups/exports.py index c127ad6a3..f2feaca58 100644 --- a/mealie/services/backups/exports.py +++ b/mealie/services/backups/exports.py @@ -129,6 +129,9 @@ def backup_all( db_export.export_items(all_recipes, "recipes", export_list=False, slug_folder=True) db_export.export_templates(all_recipes) + all_comments = db.comments.get_all(session) + db_export.export_items(all_comments, "comments") + if export_settings: all_settings = db.settings.get_all(session) db_export.export_items(all_settings, "settings") diff --git a/mealie/services/backups/imports.py b/mealie/services/backups/imports.py index e497a0424..d4f626257 100644 --- a/mealie/services/backups/imports.py +++ b/mealie/services/backups/imports.py @@ -6,6 +6,7 @@ from typing import Callable from mealie.core.config import app_dirs from mealie.db.database import db +from mealie.schema.comments import CommentOut from mealie.schema.event_notifications import EventNotificationIn from mealie.schema.recipe import Recipe from mealie.schema.restore import ( @@ -85,6 +86,22 @@ class ImportDatabase: 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 def _recipe_migration(recipe_dict: dict) -> dict: if recipe_dict.get("categories", False): @@ -364,6 +381,9 @@ def import_database( if import_notifications: notification_report = import_session.import_notifications() + if import_recipes: + import_session.import_comments() + import_session.clean_up() return {