import / export

This commit is contained in:
hay-kot 2021-05-29 19:55:20 -08:00
commit ab8c0d7e97
4 changed files with 38 additions and 3 deletions

View file

@ -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

View file

@ -28,6 +28,7 @@ class CommentSaveToDB(CommentIn):
class CommentOut(CommentIn):
id: int
uuid: str
recipe_slug: str
date_added: datetime
user: UserBase

View file

@ -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")

View file

@ -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 {