Skip to content

Commit

Permalink
Log when a page alias is converted into a regular page
Browse files Browse the repository at this point in the history
  • Loading branch information
kaedroho committed Dec 5, 2020
1 parent 1a9c64c commit 5171a31
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
27 changes: 23 additions & 4 deletions wagtail_localize/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
get_serializable_data_for_fields,
model_from_serializable_data,
)
from wagtail.core.models import Page, get_translatable_models
from wagtail.core.models import Page, get_translatable_models, PageLogEntry
from wagtail.core.utils import find_available_slug

from .compat import DATE_FORMAT
Expand Down Expand Up @@ -504,9 +504,27 @@ def create_or_update_translation(self, locale, user=None, publish=True, copy_par
ingest_segments(original, translation, self.locale, locale, segments)

if isinstance(translation, Page):
# Convert the page into a regular page
# TODO: Audit logging, etc
translation.alias_of_id = None
# If the page is an alias, convert it into a regular page
if translation.alias_of_id:
translation.alias_of_id = None
translation.save(update_fields=['alias_of_id'], clean=False)

# Create initial revision
revision = translation.save_revision(user=user, changed=False, clean=False)

# Log the alias conversion
PageLogEntry.objects.log_action(
instance=translation,
revision=revision,
action='wagtail.convert_alias',
user=user,
data={
'page': {
'id': translation.id,
'title': translation.get_admin_display_title()
},
},
)

# Make sure the slug is valid
translation.slug = find_available_slug(translation.get_parent(), slugify(translation.slug), ignore_page_id=translation.id)
Expand All @@ -517,6 +535,7 @@ def create_or_update_translation(self, locale, user=None, publish=True, copy_par

if publish:
page_revision.publish()

else:
translation.save()
page_revision = None
Expand Down
21 changes: 20 additions & 1 deletion wagtail_localize/tests/test_translationsource_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.test import TestCase
from django.utils import timezone
from wagtail.core.blocks import StreamValue
from wagtail.core.models import Page, Locale
from wagtail.core.models import Page, Locale, PageLogEntry

from wagtail_localize.models import (
TranslationSource,
Expand Down Expand Up @@ -526,6 +526,25 @@ def test_create_with_fallback_true(self):
self.assertEqual(translated_page.test_snippet, self.translated_snippet)
self.assertEqual(translated_page.test_charfield, "Ceci est du contenu de test")

def test_convert_alias(self):
self.page.copy_for_translation(self.dest_locale, alias=True)

new_page, created = self.source.create_or_update_translation(self.dest_locale)

self.assertFalse(created)
self.assertIsNone(new_page.alias_of)
self.assertEqual(new_page.title, "Test page")
self.assertEqual(new_page.slug, 'test-page-fr')
self.assertEqual(new_page.test_charfield, "Ceci est du contenu de test")
self.assertEqual(new_page.translation_key, self.page.translation_key)
self.assertEqual(new_page.locale, self.dest_locale)
self.assertTrue(
self.source.translation_logs.filter(locale=self.dest_locale).exists()
)

# Check a log was created for the alias conversion
self.assertTrue(PageLogEntry.objects.filter(page=new_page, action='wagtail.convert_alias').exists())


class TestGetEphemeralTranslatedInstance(TestCase):
def setUp(self):
Expand Down

0 comments on commit 5171a31

Please sign in to comment.