fix: use locale for Recipe Created timeline event (#4497) (#7623)

This commit is contained in:
Hayden
2026-05-14 09:07:15 -05:00
committed by GitHub
parent 074ec7aab2
commit 1cebfd56ab
3 changed files with 62 additions and 2 deletions

View File

@@ -15,6 +15,7 @@ from mealie.schema.recipe.recipe_timeline_events import (
RecipeTimelineEventPagination, RecipeTimelineEventPagination,
RecipeTimelineEventUpdate, RecipeTimelineEventUpdate,
TimelineEventImage, TimelineEventImage,
TimelineEventType,
) )
from mealie.schema.recipe.request_helpers import UpdateImageResponse from mealie.schema.recipe.request_helpers import UpdateImageResponse
from mealie.schema.response.pagination import PaginationQuery from mealie.schema.response.pagination import PaginationQuery
@@ -50,6 +51,10 @@ class RecipeTimelineEventsController(BaseCrudController):
override=RecipeTimelineEventOut, override=RecipeTimelineEventOut,
) )
for event in response.items:
if event.event_type == TimelineEventType.system.value:
event.subject = self.t(event.subject)
response.set_pagination_guides(router.url_path_for("get_all"), q.model_dump()) response.set_pagination_guides(router.url_path_for("get_all"), q.model_dump())
return response return response
@@ -83,7 +88,10 @@ class RecipeTimelineEventsController(BaseCrudController):
@router.get("/{item_id}", response_model=RecipeTimelineEventOut) @router.get("/{item_id}", response_model=RecipeTimelineEventOut)
def get_one(self, item_id: UUID4): def get_one(self, item_id: UUID4):
return self.mixins.get_one(item_id) event = self.mixins.get_one(item_id)
if event.event_type == TimelineEventType.system.value:
event.subject = self.t(event.subject)
return event
@router.put("/{item_id}", response_model=RecipeTimelineEventOut) @router.put("/{item_id}", response_model=RecipeTimelineEventOut)
def update_one(self, item_id: UUID4, data: RecipeTimelineEventUpdate): def update_one(self, item_id: UUID4, data: RecipeTimelineEventUpdate):

View File

@@ -38,6 +38,8 @@ from mealie.services.scraper import cleaner
from .template_service import TemplateService from .template_service import TemplateService
RECIPE_CREATED_EVENT_SUBJECT = "recipe.recipe-created"
class RecipeServiceBase(BaseService): class RecipeServiceBase(BaseService):
def __init__(self, repos: AllRepositories, user: PrivateUser, household: HouseholdInDB, translator: Translator): def __init__(self, repos: AllRepositories, user: PrivateUser, household: HouseholdInDB, translator: Translator):
@@ -224,7 +226,7 @@ class RecipeService(RecipeServiceBase):
timeline_event_data = RecipeTimelineEventCreate( timeline_event_data = RecipeTimelineEventCreate(
user_id=new_recipe.user_id, user_id=new_recipe.user_id,
recipe_id=new_recipe.id, recipe_id=new_recipe.id,
subject=self.t("recipe.recipe-created"), subject=RECIPE_CREATED_EVENT_SUBJECT,
event_type=TimelineEventType.system, event_type=TimelineEventType.system,
timestamp=new_recipe.created_at or datetime.now(UTC), timestamp=new_recipe.created_at or datetime.now(UTC),
) )

View File

@@ -3,18 +3,24 @@ from uuid import uuid4
import pytest import pytest
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
from mealie.lang.providers import get_all_translations
from mealie.schema.recipe.recipe import Recipe from mealie.schema.recipe.recipe import Recipe
from mealie.schema.recipe.recipe_timeline_events import ( from mealie.schema.recipe.recipe_timeline_events import (
RecipeTimelineEventOut, RecipeTimelineEventOut,
RecipeTimelineEventPagination, RecipeTimelineEventPagination,
TimelineEventImage, TimelineEventImage,
TimelineEventType,
) )
from mealie.schema.recipe.request_helpers import UpdateImageResponse from mealie.schema.recipe.request_helpers import UpdateImageResponse
from mealie.services.recipe.recipe_service import RECIPE_CREATED_EVENT_SUBJECT
from tests.utils import api_routes from tests.utils import api_routes
from tests.utils.factories import random_string from tests.utils.factories import random_string
from tests.utils.fixture_schemas import TestUser from tests.utils.fixture_schemas import TestUser
PERSISTED_TRANSLATION_KEYS = [RECIPE_CREATED_EVENT_SUBJECT]
@pytest.fixture(scope="function") @pytest.fixture(scope="function")
def recipes(api_client: TestClient, unique_user: TestUser): def recipes(api_client: TestClient, unique_user: TestUser):
recipes = [] recipes = []
@@ -341,6 +347,50 @@ def test_create_recipe_with_timeline_event(
assert events_pagination.items assert events_pagination.items
@pytest.mark.parametrize("translation_key", PERSISTED_TRANSLATION_KEYS)
def test_persisted_translation_keys_have_translations(translation_key: str):
translations = get_all_translations(translation_key)
missing_translations = [locale for locale, translation in translations.items() if translation == translation_key]
assert missing_translations == []
def test_recipe_created_system_event_is_translated(
api_client: TestClient,
unique_user: TestUser,
recipes: list[Recipe],
):
recipe = recipes[0]
params = {"queryFilter": f"recipe_id={recipe.id}"}
# fetch events in French — the system "recipe created" event should be translated
fr_headers = {**unique_user.token, "Accept-Language": "fr-FR"}
events_response = api_client.get(api_routes.recipes_timeline_events, params=params, headers=fr_headers)
assert events_response.status_code == 200
events_pagination = RecipeTimelineEventPagination.model_validate(events_response.json())
system_events = [e for e in events_pagination.items if e.event_type == TimelineEventType.system.value]
assert system_events, "expected at least one system event for a newly created recipe"
for event in system_events:
assert event.subject == "Recette créée", f"expected French translation, got: {event.subject!r}"
# also verify the individual GET endpoint translates correctly
single_response = api_client.get(api_routes.recipes_timeline_events_item_id(event.id), headers=fr_headers)
assert single_response.status_code == 200
single_event = RecipeTimelineEventOut.model_validate(single_response.json())
assert single_event.subject == "Recette créée"
# fetch the same events in English — subject should be the English string
en_headers = {**unique_user.token, "Accept-Language": "en-US"}
events_response = api_client.get(api_routes.recipes_timeline_events, params=params, headers=en_headers)
events_pagination = RecipeTimelineEventPagination.model_validate(events_response.json())
system_events = [e for e in events_pagination.items if e.event_type == TimelineEventType.system.value]
for event in system_events:
assert event.subject == "Recipe Created", f"expected English string, got: {event.subject!r}"
@pytest.mark.parametrize("use_other_household_user", [True, False]) @pytest.mark.parametrize("use_other_household_user", [True, False])
def test_invalid_recipe_id( def test_invalid_recipe_id(
api_client: TestClient, unique_user: TestUser, h2_user: TestUser, use_other_household_user: bool api_client: TestClient, unique_user: TestUser, h2_user: TestUser, use_other_household_user: bool