Skip to content

Commit

Permalink
feat: Migrate from Copy Me That (mealie-recipes#2212)
Browse files Browse the repository at this point in the history
* implemented copymethat migration

* added migration tree

* added translation support

* genericized example jpgs

* added test data

* fixed test archive

* switched recipe create to service
added test for timeline event creation

* linting

* lxml go brrr
  • Loading branch information
michael-genson authored Mar 12, 2023
1 parent 3ce8fa9 commit 3118b0e
Show file tree
Hide file tree
Showing 17 changed files with 288 additions and 36 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ docs/site/
frontend/dist/

dev/code-generation/generated/*
dev/data/mealie.db-journal
dev/data/backups/*
dev/data/debug/*
dev/data/img/*
Expand Down
23 changes: 20 additions & 3 deletions frontend/lang/messages/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -283,17 +283,34 @@
"meal-plan-settings": "Meal Plan Settings"
},
"migration": {
"migration-data-removed": "Migration data removed",
"new-migration": "New Migration",
"no-file-selected": "No File Selected",
"no-migration-data-available": "No Migration Data Available",
"previous-migrations": "Previous Migrations",
"recipe-migration": "Recipe Migration",
"chowdown": {
"description": "Migrate data from Chowdown",
"description-long": "Mealie natively supports the chowdown repository format. Download the code repository as a .zip file and upload it below.",
"title": "Chowdown"
},
"migration-data-removed": "Migration data removed",
"nextcloud": {
"description": "Migrate data from a Nextcloud Cookbook instance",
"description-long": "Nextcloud recipes can be imported from a zip file that contains the data stored in Nextcloud. See the example folder structure below to ensure your recipes are able to be imported.",
"title": "Nextcloud Cookbook"
},
"no-migration-data-available": "No Migration Data Available",
"recipe-migration": "Recipe Migration"
"copymethat": {
"description-long": "Mealie can import recipes from Copy Me That. Export your recipes in HTML format, then upload the .zip below.",
"title": "Copy Me That Recipe Manager"
},
"paprika": {
"description-long": "Mealie can import recipes from the Paprika application. Export your recipes from paprika, rename the export extension to .zip and upload it below.",
"title": "Paprika Recipe Manager"
},
"mealie-pre-v1": {
"description-long": "Mealie can import recipes from the Mealie application from a pre v1.0 release. Export your recipes from your old instance, and upload the zip file below. Note that only recipes can be imported from the export.",
"title": "Mealie Pre v1.0"
}
},
"new-recipe": {
"bulk-add": "Bulk Add",
Expand Down
2 changes: 1 addition & 1 deletion frontend/lib/api/types/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

export type WebhookType = "mealplan";
export type SupportedMigrations = "nextcloud" | "chowdown" | "paprika" | "mealie_alpha";
export type SupportedMigrations = "nextcloud" | "chowdown" | "copymethat" | "paprika" | "mealie_alpha";

export interface CreateGroupPreferences {
privateGroup?: boolean;
Expand Down
52 changes: 40 additions & 12 deletions frontend/pages/group/migrations.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
Mealie.
</BasePageTitle>
<v-container>
<BaseCardSectionTitle title="New Migration"> </BaseCardSectionTitle>
<BaseCardSectionTitle :title="$i18n.tc('migration.new-migration')"> </BaseCardSectionTitle>
<v-card outlined :loading="loading">
<v-card-title> Choose Migration Type </v-card-title>
<v-card-text v-if="content" class="pb-0">
Expand All @@ -39,7 +39,7 @@
:text-btn="false"
@uploaded="setFileObject"
/>
{{ fileObject.name || "No file selected" }}
{{ fileObject.name || $i18n.tc('migration.no-file-selected') }}
</v-card-text>

<v-card-text>
Expand All @@ -58,7 +58,7 @@
</v-card>
</v-container>
<v-container>
<BaseCardSectionTitle title="Previous Migrations"> </BaseCardSectionTitle>
<BaseCardSectionTitle :title="$i18n.tc('migration.previous-migrations')"> </BaseCardSectionTitle>
<ReportTable :items="reports" @delete="deleteReport" />
</v-container>
</v-container>
Expand All @@ -74,13 +74,14 @@ import { SupportedMigrations } from "~/lib/api/types/group";
const MIGRATIONS = {
nextcloud: "nextcloud",
chowdown: "chowdown",
copymethat: "copymethat",
paprika: "paprika",
mealie: "mealie_alpha",
};
export default defineComponent({
setup() {
const { $globals } = useContext();
const { $globals, i18n } = useContext();
const api = useUserApi();
Expand All @@ -95,26 +96,30 @@ export default defineComponent({
const items: MenuItem[] = [
{
text: "Nextcloud",
text: i18n.tc("migration.nextcloud.title"),
value: MIGRATIONS.nextcloud,
},
{
text: "Chowdown",
text: i18n.tc("migration.chowdown.title"),
value: MIGRATIONS.chowdown,
},
{
text: "Paprika",
text: i18n.tc("migration.copymethat.title"),
value: MIGRATIONS.copymethat,
},
{
text: i18n.tc("migration.paprika.title"),
value: MIGRATIONS.paprika,
},
{
text: "Mealie",
text: i18n.tc("migration.mealie-pre-v1.title"),
value: MIGRATIONS.mealie,
},
];
const _content = {
[MIGRATIONS.nextcloud]: {
text: "Nextcloud recipes can be imported from a zip file that contains the data stored in Nextcloud. See the example folder structure below to ensure your recipes are able to be imported.",
text: i18n.tc("migration.nextcloud.description-long"),
tree: [
{
id: 1,
Expand Down Expand Up @@ -146,7 +151,7 @@ export default defineComponent({
],
},
[MIGRATIONS.chowdown]: {
text: "Mealie natively supports the chowdown repository format. Download the code repository as a .zip file and upload it below",
text: i18n.tc("migration.chowdown.description-long"),
tree: [
{
id: 1,
Expand Down Expand Up @@ -177,12 +182,35 @@ export default defineComponent({
},
],
},
[MIGRATIONS.copymethat]: {
text: i18n.tc("migration.copymethat.description-long"),
tree: [
{
id: 1,
icon: $globals.icons.zip,
name: "Copy_Me_That_20230306.zip",
children: [
{
id: 2,
name: "images",
icon: $globals.icons.folderOutline,
children: [
{ id: 3, name: "recipe_1_an5zy.jpg", icon: $globals.icons.fileImage },
{ id: 4, name: "recipe_2_82el8.jpg", icon: $globals.icons.fileImage },
{ id: 5, name: "recipe_3_j75qg.jpg", icon: $globals.icons.fileImage },
],
},
{ id: 6, name: "recipes.html", icon: $globals.icons.codeJson }
]
}
],
},
[MIGRATIONS.paprika]: {
text: "Mealie can import recipes from the Paprika application. Export your recipes from paprika, rename the export extension to .zip and upload it below.",
text: i18n.tc("migration.paprika.description-long"),
tree: false,
},
[MIGRATIONS.mealie]: {
text: "Mealie can import recipes from the Mealie application from a pre v1.0 release. Export your recipes from your old instance, and upload the zip file below. Note that only recipes can be imported from the export.",
text: i18n.tc("migration.mealie-pre-v1.description-long"),
tree: [
{
id: 1,
Expand Down
2 changes: 1 addition & 1 deletion mealie/routes/groups/controller_group_reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ def get_one(self, item_id: UUID4):
def delete_one(self, item_id: UUID4):
try:
self.mixins.delete_one(item_id) # type: ignore
return SuccessResponse.respond(self.t("report-deleted"))
return SuccessResponse.respond(self.t("group.report-deleted"))
except Exception as ex:
raise HTTPException(500, ErrorResponse.respond("Failed to delete report")) from ex
2 changes: 2 additions & 0 deletions mealie/routes/groups/controller_migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from mealie.services.migrations import (
BaseMigrator,
ChowdownMigrator,
CopyMeThatMigrator,
MealieAlphaMigrator,
NextcloudMigrator,
PaprikaMigrator,
Expand Down Expand Up @@ -45,6 +46,7 @@ def start_data_migration(

table: dict[SupportedMigrations, type[BaseMigrator]] = {
SupportedMigrations.chowdown: ChowdownMigrator,
SupportedMigrations.copymethat: CopyMeThatMigrator,
SupportedMigrations.mealie_alpha: MealieAlphaMigrator,
SupportedMigrations.nextcloud: NextcloudMigrator,
SupportedMigrations.paprika: PaprikaMigrator,
Expand Down
1 change: 1 addition & 0 deletions mealie/schema/group/group_migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
class SupportedMigrations(str, enum.Enum):
nextcloud = "nextcloud"
chowdown = "chowdown"
copymethat = "copymethat"
paprika = "paprika"
mealie_alpha = "mealie_alpha"

Expand Down
1 change: 1 addition & 0 deletions mealie/services/migrations/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .chowdown import *
from .copymethat import *
from .mealie_alpha import *
from .nextcloud import *
from .paprika import *
42 changes: 26 additions & 16 deletions mealie/services/migrations/_migration_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from pydantic import UUID4

from mealie.core import root_logger
from mealie.core.exceptions import UnexpectedNone
from mealie.repos.all_repositories import AllRepositories
from mealie.schema.recipe import Recipe
from mealie.schema.recipe.recipe_settings import RecipeSettings
Expand All @@ -16,6 +17,7 @@
ReportSummary,
ReportSummaryStatus,
)
from mealie.services.recipe.recipe_service import RecipeService
from mealie.services.scraper import cleaner

from .._base_service import BaseService
Expand All @@ -38,17 +40,27 @@ def __init__(
self.archive = archive
self.db = db
self.session = session
self.user_id = user_id
self.group_id = group_id
self.add_migration_tag = add_migration_tag

user = db.users.get_one(user_id)
if not user:
raise UnexpectedNone(f"Cannot find user {user_id}")

group = db.groups.get_one(group_id)
if not group:
raise UnexpectedNone(f"Cannot find group {group_id}")

self.user = user
self.group = group

self.name = "migration"

self.report_entries = []

self.logger = root_logger.get_logger()

self.helpers = DatabaseMigrationHelpers(self.db, self.session, self.group_id, self.user_id)
self.helpers = DatabaseMigrationHelpers(self.db, self.session, self.group.id, self.user.id)
self.recipe_service = RecipeService(db, user, group)

super().__init__()

Expand All @@ -60,7 +72,7 @@ def _create_report(self, report_name: str) -> None:
name=report_name,
category=ReportCategory.migration,
status=ReportSummaryStatus.in_progress,
group_id=self.group_id,
group_id=self.group.id,
)

self.report = self.db.group_reports.create(report_to_save)
Expand Down Expand Up @@ -117,25 +129,23 @@ def import_recipes_to_database(self, validated_recipes: list[Recipe]) -> list[tu

return_vars: list[tuple[str, UUID4, bool]] = []

group = self.db.groups.get_one(self.group_id)

if not group or not group.preferences:
if not self.group.preferences:
raise ValueError("Group preferences not found")

default_settings = RecipeSettings(
public=group.preferences.recipe_public,
show_nutrition=group.preferences.recipe_show_nutrition,
show_assets=group.preferences.recipe_show_assets,
landscape_view=group.preferences.recipe_landscape_view,
disable_comments=group.preferences.recipe_disable_comments,
disable_amount=group.preferences.recipe_disable_amount,
public=self.group.preferences.recipe_public,
show_nutrition=self.group.preferences.recipe_show_nutrition,
show_assets=self.group.preferences.recipe_show_assets,
landscape_view=self.group.preferences.recipe_landscape_view,
disable_comments=self.group.preferences.recipe_disable_comments,
disable_amount=self.group.preferences.recipe_disable_amount,
)

for recipe in validated_recipes:
recipe.settings = default_settings

recipe.user_id = self.user_id
recipe.group_id = self.group_id
recipe.user_id = self.user.id
recipe.group_id = self.group.id

if recipe.tags:
recipe.tags = self.helpers.get_or_set_tags(x.name for x in recipe.tags)
Expand All @@ -151,7 +161,7 @@ def import_recipes_to_database(self, validated_recipes: list[Recipe]) -> list[tu
exception: str | Exception = ""
status = False
try:
recipe = self.db.recipes.create(recipe)
recipe = self.recipe_service.create_one(recipe)
status = True

except Exception as inst:
Expand Down
Loading

0 comments on commit 3118b0e

Please sign in to comment.