Update Page.copy_for_translation to create parents as aliases

pull/6450/head
Karl Hobley 2020-09-28 12:06:55 +01:00 zatwierdzone przez Karl Hobley
rodzic 674fa8e27a
commit 7af655d0d1
2 zmienionych plików z 40 dodań i 20 usunięć

Wyświetl plik

@ -2082,7 +2082,7 @@ class Page(AbstractPage, index.Indexed, ClusterableModel, metaclass=PageBase):
create_alias.alters_data = True create_alias.alters_data = True
@transaction.atomic @transaction.atomic
def copy_for_translation(self, locale, copy_parents=False, exclude_fields=None): def copy_for_translation(self, locale, copy_parents=False, alias=False, exclude_fields=None):
""" """
Copies this page for the specified locale. Copies this page for the specified locale.
""" """
@ -2098,7 +2098,7 @@ class Page(AbstractPage, index.Indexed, ClusterableModel, metaclass=PageBase):
raise ParentNotTranslatedError raise ParentNotTranslatedError
translated_parent = parent.copy_for_translation( translated_parent = parent.copy_for_translation(
locale, copy_parents=True locale, copy_parents=True, alias=True
) )
else: else:
# Don't duplicate the root page for translation. Create new locale as a sibling # Don't duplicate the root page for translation. Create new locale as a sibling
@ -2111,6 +2111,15 @@ class Page(AbstractPage, index.Indexed, ClusterableModel, metaclass=PageBase):
# Find available slug for new page # Find available slug for new page
slug = find_available_slug(translated_parent, slug) slug = find_available_slug(translated_parent, slug)
if alias:
return self.create_alias(
parent=translated_parent,
update_slug=slug,
update_locale=locale,
reset_translation_key=False,
)
else:
# Update locale on translatable child objects as well # Update locale on translatable child objects as well
def process_child_object( def process_child_object(
original_page, page_copy, child_relation, child_object original_page, page_copy, child_relation, child_object

Wyświetl plik

@ -2010,6 +2010,17 @@ class TestCopyForTranslation(TestCase):
# self.assertEqual(ffr_award.translation_key, en_award.translation_key) # self.assertEqual(ffr_award.translation_key, en_award.translation_key)
# self.assertEqual(list(fr_award.get_translations()), [en_award]) # self.assertEqual(list(fr_award.get_translations()), [en_award])
def test_copies_missing_parents_as_aliases(self):
fr_eventpage = self.en_eventpage.copy_for_translation(self.fr_locale, copy_parents=True)
fr_eventindex = fr_eventpage.get_parent()
# Check parent is a translation of its English original
self.assertEqual(fr_eventindex.locale, self.fr_locale)
self.assertEqual(fr_eventindex.translation_key, self.en_eventindex.translation_key)
# Check parent is also an alias of its English original
self.assertEqual(fr_eventindex.alias_of, self.en_eventindex)
class TestSubpageTypeBusinessRules(TestCase, WagtailTestUtils): class TestSubpageTypeBusinessRules(TestCase, WagtailTestUtils):
def test_allowed_subpage_models(self): def test_allowed_subpage_models(self):