diff --git a/mandelstudio/management/commands/fix_contact_cta_anchor.py b/mandelstudio/management/commands/fix_contact_cta_anchor.py index 287bb7d..f1e6f36 100644 --- a/mandelstudio/management/commands/fix_contact_cta_anchor.py +++ b/mandelstudio/management/commands/fix_contact_cta_anchor.py @@ -67,40 +67,34 @@ class Command(BaseCommand): raise CommandError(f"Page id={page_id} not found") self._fix_page(page, apply_changes=apply_changes) else: - source = self._find_contact_source_page() - if source is None: - raise CommandError("Could not find a source contact page") - - for locale in Locale.objects.all().order_by("language_code"): - page = ( - Page.objects.filter( - translation_key=source.translation_key, - locale=locale, - ) - .specific() - .first() - ) - if page is None: - self.stdout.write( - f"SKIP {locale.language_code}: no contact translation" - ) - continue + any_found = False + for page in self._iter_contact_pages(): + any_found = True self._fix_page(page, apply_changes=apply_changes) + if not any_found: + raise CommandError("Could not find any localized contact pages") if not apply_changes: raise CommandError( "Dry-run complete. Re-run with --apply to persist changes." ) - def _find_contact_source_page(self) -> Page | None: + def _iter_contact_pages(self): + yielded = False for code, slug in CONTACT_SLUGS.items(): locale = Locale.objects.filter(language_code=code).first() if locale is None: + self.stdout.write(f"SKIP {code}: locale not found") continue - page = Page.objects.filter(locale=locale, slug=slug).specific().first() - if page is not None: - return page - return None + pages = list(Page.objects.filter(locale=locale, slug=slug).specific()) + if not pages: + self.stdout.write(f"SKIP {code}: no contact page for slug={slug}") + continue + for page in pages: + yielded = True + yield page + if not yielded: + return def _fix_page(self, page: Page, *, apply_changes: bool) -> None: specific = page.specific