Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete the related Translation object when converting a page back to alias #532

Merged
merged 2 commits into from
Mar 11, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion wagtail_localize/tests/test_convert_to_alias.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from wagtail.core.models import Locale, Page, PageLogEntry
from wagtail.tests.utils import WagtailTestUtils

from wagtail_localize.models import LocaleSynchronization
from wagtail_localize.models import LocaleSynchronization, Translation
from wagtail_localize.test.models import TestPage
from wagtail_localize.wagtail_hooks import ConvertToAliasPageActionMenuItem

Expand Down Expand Up @@ -195,3 +195,22 @@ def test_convert_redirect_with_invalid_next_url(self):
self.assertRedirects(
response, reverse("wagtailadmin_pages:edit", args=[self.fr_page.id])
)

def test_convert_removes_translation_objects(self):
self.assertTrue(
Translation.objects.filter(
source__object_id=self.fr_page.translation_key,
source__specific_content_type=self.fr_page.content_type_id,
target_locale=self.fr_page.locale_id,
).exists()
)

self.client.post(self.convert_url)

self.assertFalse(
Translation.objects.filter(
source__object_id=self.fr_page.translation_key,
source__specific_content_type=self.fr_page.content_type_id,
target_locale=self.fr_page.locale_id,
).exists()
)
11 changes: 10 additions & 1 deletion wagtail_localize/views/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
)
from wagtail.core.signals import page_published

from wagtail_localize.models import TranslationSource
from wagtail_localize.models import Translation, TranslationSource


def convert_to_alias(request, page_id):
Expand Down Expand Up @@ -70,6 +70,15 @@ def convert_to_alias(request, page_id):
},
)

# Now clean up the old translation
zerolab marked this conversation as resolved.
Show resolved Hide resolved
try:
translation = Translation.objects.get(
source=translation_source, target_locale=page_to_alias.locale_id
)
translation.delete()
except Translation.DoesNotExist:
pass

messages.success(
request,
_("Page '{}' has been converted into an alias.").format(page_title),
Expand Down