示例#1
0
    def setUp(self):
        self.snippet = TestSnippet.objects.create(field="Test snippet content")
        self.page = create_test_page(
            title="Test page",
            slug="test-page",
            test_charfield="This is some test content",
            test_snippet=self.snippet,
        )
        self.revision = TranslatableRevision.get_or_create_from_page_revision(
            self.page.get_latest_revision())[0]
        self.source_locale = Locale.objects.get(language_code="en")
        self.dest_locale = Locale.objects.create(language_code="fr")

        # Translate the snippet
        self.translated_snippet = self.snippet.copy_for_translation(
            self.dest_locale)
        self.translated_snippet.field = "Tester le contenu de l'extrait"
        self.translated_snippet.save()

        # Add translation for test_charfield
        self.segment = Segment.from_text(self.source_locale,
                                         "This is some test content")
        self.translation = SegmentTranslation.objects.create(
            translation_of=self.segment,
            locale=self.dest_locale,
            context=SegmentTranslationContext.objects.get(
                object_id=self.page.translation_key, path="test_charfield"),
            text="Ceci est du contenu de test",
        )
示例#2
0
    def test_create_child(self):
        child_page = create_test_page(
            title="Child page",
            slug="child-page",
            parent=self.page,
            test_charfield="This is some test content",
        )
        child_revision = TranslatableRevision.get_or_create_from_page_revision(
            child_page.get_latest_revision())[0]

        translated_parent = self.page.copy_for_translation(self.dest_locale)

        # Create a translation for the new context
        SegmentTranslation.objects.create(
            translation_of=self.segment,
            locale=self.dest_locale,
            context=SegmentTranslationContext.objects.get(
                object_id=child_page.translation_key, path="test_charfield"),
            text="Ceci est du contenu de test",
        )

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

        self.assertTrue(created)
        self.assertEqual(new_page.get_parent(), translated_parent)
        self.assertEqual(new_page.title, "Child page")
        self.assertEqual(new_page.test_charfield,
                         "Ceci est du contenu de test")
        self.assertEqual(new_page.translation_key, child_page.translation_key)
        self.assertEqual(new_page.locale, self.dest_locale)
        self.assertFalse(new_page.is_source_translation)
        self.assertTrue(
            child_revision.translation_logs.filter(
                locale=self.dest_locale).exists())
示例#3
0
    def test_creates_new_revision_if_forced(self):
        revision = TranslatableRevision.objects.create(
            object_id=self.snippet.translation_key,
            locale=self.snippet.locale,
            content_json=json.dumps({
                "pk":
                self.snippet.pk,
                "field":
                "This is some test content",
                "translation_key":
                str(self.snippet.translation_key),
                "locale":
                self.snippet.locale_id,
                "is_source_translation":
                True,
            }),
            created_at=timezone.now(),
        )

        new_revision, created = TranslatableRevision.from_instance(
            self.snippet, force=True)

        self.assertTrue(created)
        self.assertNotEqual(revision, new_revision)
        self.assertEqual(
            json.loads(revision.content_json)["field"],
            "This is some test content")
        self.assertEqual(
            json.loads(new_revision.content_json)["field"],
            "This is some test content")
示例#4
0
    def test_update_synchronised_fields(self):
        translated = self.page.copy_for_translation(self.dest_locale)

        self.page.test_synchronized_charfield = "Test synchronised content"
        self.page.test_synchronized_textfield = "Test synchronised content"
        self.page.test_synchronized_emailfield = "*****@*****.**"
        self.page.test_synchronized_slugfield = "test-synchronised-content"
        self.page.test_synchronized_urlfield = "https://test.synchronised/content"
        self.page.test_synchronized_richtextfield = "<p>Test synchronised content</p>"
        # self.page.test_synchronized_streamfield = ""
        synchronized_snippet = TestSnippet.objects.create(
            field="Synchronised snippet")
        self.page.test_synchronized_snippet = synchronized_snippet
        self.page.test_synchronized_customfield = "Test synchronised content"

        # Save the page
        revision = self.page.save_revision()
        revision.publish()
        revision_with_synchronised_content = TranslatableRevision.get_or_create_from_page_revision(
            revision)[0]

        # Check translation hasn't been updated yet
        translated.refresh_from_db()
        self.assertEqual(translated.test_synchronized_charfield, "")

        # Update the original page again. This will make sure it's taking the content from the revision and not the live version
        self.page.test_synchronized_charfield = (
            "Test synchronised content updated again")
        self.page.save_revision().publish()

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

        self.assertFalse(created)
        self.assertEqual(new_page, translated)
        self.assertEqual(new_page.test_synchronized_charfield,
                         "Test synchronised content")
        self.assertEqual(new_page.test_synchronized_charfield,
                         "Test synchronised content")
        self.assertEqual(new_page.test_synchronized_textfield,
                         "Test synchronised content")
        self.assertEqual(new_page.test_synchronized_emailfield,
                         "*****@*****.**")
        self.assertEqual(new_page.test_synchronized_slugfield,
                         "test-synchronised-content")
        self.assertEqual(new_page.test_synchronized_urlfield,
                         "https://test.synchronised/content")
        self.assertEqual(new_page.test_synchronized_richtextfield,
                         "<p>Test synchronised content</p>")
        self.assertEqual(new_page.test_synchronized_snippet,
                         synchronized_snippet)
        self.assertEqual(new_page.test_synchronized_customfield,
                         "Test synchronised content")
示例#5
0
def create_test_page(**kwargs):
    parent = kwargs.pop("parent", None) or Page.objects.get(id=1)
    page = parent.add_child(instance=TestPage(**kwargs))
    page_revision = page.save_revision()
    page_revision.publish()

    revision, created = TranslatableRevision.get_or_create_from_page_revision(
        page_revision)

    prepare_revision(revision)

    return page
示例#6
0
def prepare_revision(revision):
    # Extract segments from revision and save them into translation memory
    segments = extract_segments(revision.as_instance())
    insert_segments(revision, revision.locale_id, segments)

    # Recurse into any related objects
    for segment in segments:
        if not isinstance(segment, RelatedObjectValue):
            continue

        related_revision, created = TranslatableRevision.from_instance(
            segment.get_instance(revision.locale))
        prepare_revision(related_revision)
示例#7
0
    def test_update_streamfields(self):
        # Streamfields are special in that they contain content that needs to be synchronised as well as
        # translatable content.

        # Copy page for translation, this will have a blank streamfield
        translated = self.page.copy_for_translation(self.dest_locale)

        # Set streamfield value on original
        self.page.test_streamfield = StreamValue(
            TestPage.test_streamfield.field.stream_block,
            [{
                "id": "id",
                "type": "test_charblock",
                "value": "This is some test content",
            }],
            is_lazy=True,
        )

        # Save the page
        revision = self.page.save_revision()
        revision.publish()
        revision_with_streamfield = TranslatableRevision.get_or_create_from_page_revision(
            revision)[0]
        revision_with_streamfield.extract_segments()

        # Create a translation for the new context
        SegmentTranslation.objects.create(
            translation_of=self.segment,
            locale=self.dest_locale,
            context=SegmentTranslationContext.objects.get(
                object_id=self.page.translation_key,
                path="test_streamfield.id"),
            text="Ceci est du contenu de test",
        )

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

        self.assertFalse(created)
        self.assertEqual(new_page, translated)

        # Check the block was copied into translation
        self.assertEqual(new_page.test_streamfield[0].id, "id")
        self.assertEqual(new_page.test_streamfield[0].value,
                         "Ceci est du contenu de test")
示例#8
0
    def test_create(self):
        revision, created = TranslatableRevision.from_instance(self.snippet)

        self.assertTrue(created)

        self.assertEqual(revision.object_id, self.snippet.translation_key)
        self.assertEqual(revision.locale, self.snippet.locale)
        self.assertIsNone(revision.page_revision)
        self.assertEqual(
            json.loads(revision.content_json),
            {
                "pk": self.snippet.pk,
                "field": "This is some test content",
                "translation_key": str(self.snippet.translation_key),
                "locale": self.snippet.locale_id,
                "is_source_translation": True,
            },
        )
        self.assertTrue(revision.created_at)
示例#9
0
    def test_create(self):
        # Delete the revision that the Pontoon module creates on page publish
        TranslatableRevision.objects.all().delete()

        page_revision = self.page.get_latest_revision()

        # Refetch the page revision so that it has the generic Page object associated
        page_revision.refresh_from_db()

        revision, created = TranslatableRevision.get_or_create_from_page_revision(
            page_revision)

        self.assertTrue(created)

        self.assertEqual(revision.object_id, self.page.translation_key)
        self.assertEqual(revision.locale, self.page.locale)
        self.assertEqual(revision.page_revision, page_revision)
        self.assertEqual(revision.content_json, page_revision.content_json)
        self.assertEqual(revision.created_at, page_revision.created_at)
示例#10
0
    def test_reuses_existing_revision_if_not_changed(self):
        revision = TranslatableRevision.objects.create(
            object_id=self.snippet.translation_key,
            locale=self.snippet.locale,
            content_json=json.dumps({
                "pk":
                self.snippet.pk,
                "field":
                "This is some test content",
                "translation_key":
                str(self.snippet.translation_key),
                "locale":
                self.snippet.locale_id,
                "is_source_translation":
                True,
            }),
            created_at=timezone.now(),
        )

        new_revision, created = TranslatableRevision.from_instance(
            self.snippet)

        self.assertFalse(created)
        self.assertEqual(revision, new_revision)
示例#11
0
    def test_get(self):
        page_revision = self.page.get_latest_revision()
        revision, created = TranslatableRevision.get_or_create_from_page_revision(
            page_revision)

        self.assertFalse(created)
示例#12
0
 def setUp(self):
     self.snippet = TestSnippet.objects.create(
         field="This is some test content")
     self.revision = TranslatableRevision.from_instance(self.snippet)[0]
示例#13
0
 def setUp(self):
     self.page = create_test_page(title="Test page", slug="test-page")
     self.revision = TranslatableRevision.get_or_create_from_page_revision(
         self.page.get_latest_revision())[0]