forked from mozilla/bedrock
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support sending visual context to Smartling, for its CAT tool (mozill…
…a#15519) * Support sending visual context to Smartling, for its CAT tool * Adds a callback for wagtail-localize-smartling to send visual context to Smartling * Update wagtail-localize-smartling to 0.6.0 (with Smartling CAT support) * Add a light test of the visual_context callback * Update bedrock/cms/wagtail_localize_smartling/callbacks.py
- Loading branch information
1 parent
02db980
commit 3bb94e5
Showing
8 changed files
with
125 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,20 @@ | |
from bedrock.cms import models | ||
|
||
|
||
class WagtailUserFactory(factory.django.DjangoModelFactory): | ||
class Meta: | ||
model = "auth.User" # Equivalent to ``model = myapp.models.User`` | ||
django_get_or_create = ("username",) | ||
|
||
email = "[email protected]" | ||
password = factory.PostGenerationMethodCall("set_password", "te5tus3r") | ||
username = "testuser" | ||
|
||
is_superuser = False | ||
is_staff = True | ||
is_active = True | ||
|
||
|
||
class SimpleRichTextPageFactory(wagtail_factories.PageFactory): | ||
title = "Test SimpleRichTextPage" | ||
live = True | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this | ||
# file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
from unittest import mock | ||
|
||
import pytest | ||
import wagtail_factories | ||
from wagtaildraftsharing.models import WagtaildraftsharingLink | ||
|
||
from bedrock.cms.tests.factories import SimpleRichTextPageFactory, WagtailUserFactory | ||
from bedrock.cms.wagtail_localize_smartling.callbacks import visual_context | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_visual_context(client): | ||
top_level_page = SimpleRichTextPageFactory() | ||
|
||
page = SimpleRichTextPageFactory(parent=top_level_page, slug="visual-context-text-page") | ||
page.save_revision() | ||
|
||
wagtail_factories.SiteFactory( | ||
root_page=top_level_page, | ||
is_default_site=True, | ||
hostname=client._base_environ()["SERVER_NAME"], | ||
) | ||
|
||
user = WagtailUserFactory() | ||
|
||
mock_job = mock.Mock() | ||
mock_job.translation_source.get_source_instance.return_value = page | ||
mock_job.user = user | ||
|
||
url, html = visual_context(smartling_job=mock_job) | ||
|
||
# light checks because we're not testing wagtaildraftsharing itself | ||
assert "<body" in html | ||
assert "Test SimpleRichTextPage" in html | ||
|
||
sharing_link_key = WagtaildraftsharingLink.objects.get().key | ||
assert url == f"http://testserver:81/_internal_draft_preview/{sharing_link_key}/" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this | ||
# file, You can obtain one at https://mozilla.org/MPL/2.0/. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this | ||
# file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
# This file contains callback functions to be called by | ||
# github.com/mozilla/wagtail-localize-smartling/ and | ||
# hooked in via settings. | ||
# | ||
# The README.md of that repo covers what inputs are | ||
# provided and what outputs are needed | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
from django.test import RequestFactory | ||
|
||
if TYPE_CHECKING: | ||
from wagtail.models import Page | ||
from wagtail_localize_smartling.models import Job | ||
|
||
from wagtaildraftsharing.models import WagtaildraftsharingLink | ||
from wagtaildraftsharing.views import SharingLinkView | ||
|
||
|
||
def _get_html_for_sharing_link(sharing_link: WagtaildraftsharingLink) -> str: | ||
request = RequestFactory().get(sharing_link.url) | ||
view_func = SharingLinkView.as_view() | ||
resp = view_func( | ||
request=request, | ||
key=sharing_link.key, | ||
) | ||
return resp.content.decode("utf-8") | ||
|
||
|
||
def _get_full_url_for_sharing_link(sharing_link: WagtaildraftsharingLink, page: "Page") -> str: | ||
return f"{page.get_site().root_url}{sharing_link.url}" | ||
|
||
|
||
def visual_context(smartling_job: "Job") -> tuple[str, str]: | ||
# Needs to return two strings: | ||
# 1. A URL where the page (possibly a draft) can be viewed without authentication | ||
# 2. The HTML of the state of the page at this point in time | ||
|
||
page = smartling_job.translation_source.get_source_instance() | ||
revision = page.latest_revision | ||
|
||
sharing_link = WagtaildraftsharingLink.objects.get_or_create_for_revision( | ||
revision=revision, | ||
user=smartling_job.user, | ||
) | ||
|
||
url = _get_full_url_for_sharing_link(sharing_link=sharing_link, page=page) | ||
html = _get_html_for_sharing_link(sharing_link=sharing_link) | ||
|
||
return (url, html) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters