Example #1
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")
Example #2
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)
Example #3
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)
Example #4
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)
Example #5
0
 def setUp(self):
     self.snippet = TestSnippet.objects.create(
         field="This is some test content")
     self.revision = TranslatableRevision.from_instance(self.snippet)[0]