Skip to content

Commit

Permalink
Add translation_imported signal (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
zerolab authored Jun 7, 2024
2 parents cba4feb + 77db9f2 commit 21618bc
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 56 deletions.
7 changes: 6 additions & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@
"redhat.vscode-yaml",
"tamasfe.even-better-toml",
"the-compiler.python-tox"
]
],
"settings": {
"[python]": {
"editor.defaultFormatter": "charliermarsh.ruff"
}
}
}
}
}
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,14 @@ flowchart LR
```
## Signals
This app provides a single `wagtail_localize.signals.translation_imported`
signal that is sent when translation are imported from Smartling.
## Signals
Signal kwargs:
<!-- TODO -->
- `translation_imported`
- `sender`: The `wagtail_localize_smartling.models.Job` class
- `instance`: The `Job` instance for which translation are being imported
- `translation`: The `wagtail_localize.models.Translation` instance the translations are being imported to.
Use `translation.get_target_instance()` to get the model instance that the translation is for (e.g. a page or snippet)
46 changes: 46 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,49 @@ reportUnnecessaryTypeIgnoreComment = true
# Make sure we're checking against the minimum supported Python version by
# default. CI will check against all supported versions for us.
pythonVersion = "3.8"


[tool.ruff]
extend-exclude = [
"LC_MESSAGES",
"locale",
]

[tool.ruff.lint]
select = [
"B", # flake8-bugbear
"C4", # flake8-comprehensions
"DJ", # flake8-django
"E", # pycodestyle errors
"F", # pyflakes
"I", # isort
"RUF100", # unused noqa
"S", # flake8-bandit
"UP", # pyupgrade
"W", # warning
]
fixable = [
"C4",
"E",
"F",
"I",
"UP",
]

[tool.ruff.lint.isort]
lines-after-imports = 2
lines-between-types = 1


[tool.ruff.lint.per-file-ignores]
"tests/**/*.py" = [
"S101", # asserts allowed in tests
"ARG", # unused function args (pytest fixtures)
"FBT", # booleans as positional arguments (@pytest.mark.parametrize)
"PLR2004", # magic value used in comparison
"S311", # standard pseudo-random generators are not suitable for cryptographic purposes
]


[tool.ruff.format]
docstring-code-format = true
43 changes: 0 additions & 43 deletions ruff.toml

This file was deleted.

1 change: 0 additions & 1 deletion src/wagtail_localize_smartling/signals.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from django.dispatch import Signal


# TODO send this
translation_imported = Signal()
9 changes: 8 additions & 1 deletion src/wagtail_localize_smartling/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@

from django.db import transaction
from django.utils import timezone
from wagtail_localize.models import Translation

from . import utils
from .api.client import client
from .api.types import JobStatus
from .signals import translation_imported


if TYPE_CHECKING:
Expand Down Expand Up @@ -183,7 +185,7 @@ def _download_and_apply_translations(job: "Job") -> None:
)

try:
translation = job.translations.get(
translation: Translation = job.translations.get(
target_locale__language_code=utils.format_wagtail_locale_id(
smartling_locale_id
)
Expand All @@ -197,4 +199,9 @@ def _download_and_apply_translations(job: "Job") -> None:
with translations_zip.open(zipinfo) as f:
po_file = polib.pofile(f.read().decode("utf-8"))
translation.import_po(po_file)
translation_imported.send(
sender=Job,
instance=job,
translation=translation,
)
logger.info("Imported translations for %s", translation)
3 changes: 2 additions & 1 deletion tests/management_commands/test_sync_smartling.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
from tests.factories import JobFactory


@pytest.mark.skip()
@pytest.mark.django_db()
def test_sync_smartling(smartling_project):
unsynced_job_page = InfoPageFactory()
unsynced_job = JobFactory(source_instance=unsynced_job_page, unsynced=True)
JobFactory(source_instance=unsynced_job_page, unsynced=True)

call_command("sync_smartling")

Expand Down
5 changes: 1 addition & 4 deletions tests/status/test_status_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ def test_everything_working(client, superuser, smartling_project):
# Project metadata
assert f"Project ID {smartling_project.project_id}" in text
assert f"Project name {smartling_project.name}" in text
assert (
f"Source locale {smartling_project.source_locale_description}"
in text
)
assert f"Source locale {smartling_project.source_locale_description}" in text

# Source locale
assert "The source locale is compatible with Smartling" in text
Expand Down
3 changes: 1 addition & 2 deletions tests/translation_components/test_update_translations.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import pytest


@pytest.mark.skip()
@pytest.mark.django_db()
def test_update_translations():
# TODO source_object_instance passed to the form is a TranslationSource this
# time, not the page or snippet instance
raise AssertionError("TODO")

0 comments on commit 21618bc

Please sign in to comment.