Skip to content

Commit

Permalink
Test patch needed (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
allcaps authored Jun 19, 2024
1 parent a97d68f commit cbb6b7e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
14 changes: 12 additions & 2 deletions src/wagtail_translate/apps.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import wagtail

from django.apps import AppConfig


def patch_needed(version=wagtail.VERSION) -> bool:
"""
Wagtail 6.2 introduces the `copy_for_translation_done` signal.
Older Wagtail versions need to be patched.
"""
return version[0] < 6 or (version[0] == 6 and version[1] < 2)


class WagtailTranslateAppConfig(AppConfig):
label = "wagtail_translate"
name = "wagtail_translate"
verbose_name = "Wagtail Translate"

def ready(self):
# We patch Wagtail to inject the copy_for_translation_done signal.
from . import monkeypatches # noqa
if patch_needed():
from . import monkeypatches # noqa
30 changes: 13 additions & 17 deletions src/wagtail_translate/monkeypatches.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
"""
Ideally, Wagtail would fire a copy_for_translation_done signal.
Wagtail 6.2 introduces the copy_for_translation_done signal.
Wagtail Translate will patch older versions of Wagtail.
For now, we monkeypatch the `CopyPageForTranslationAction.walk` and
`CopyPageForTranslationAction.execute` methods
to register the copy_for_translation_done signals.
The `CopyPageForTranslationAction.walk` and
`CopyPageForTranslationAction.execute` methods are patched.
The new methods send the copy_for_translation_done signal.
"""

import logging

import wagtail

from wagtail.actions.copy_for_translation import CopyPageForTranslationAction


Expand Down Expand Up @@ -58,14 +57,11 @@ def new_execute(self, skip_permission_checks=False):
return translated_page


# Wagtail 6.2 introduces the `copy_for_translation_done` signal.
# Only apply the monkeypatch if Wagtail < 6.2.
if wagtail.VERSION[0] < 6 or (wagtail.VERSION[0] == 6 and wagtail.VERSION[1] < 2):
logger.warning(
"Monkeypatching wagtail.actions.copy_for_translation.CopyPageForTranslationAction.walk, send copy_for_translation_done signal"
)
logger.warning(
"Monkeypatching wagtail.actions.copy_for_translation.CopyPageForTranslationAction.execute, send copy_for_translation_done signal"
)
CopyPageForTranslationAction.walk = new_walk
CopyPageForTranslationAction.execute = new_execute
logger.warning(
"Monkeypatching wagtail.actions.copy_for_translation.CopyPageForTranslationAction.walk, send copy_for_translation_done signal"
)
logger.warning(
"Monkeypatching wagtail.actions.copy_for_translation.CopyPageForTranslationAction.execute, send copy_for_translation_done signal"
)
CopyPageForTranslationAction.walk = new_walk
CopyPageForTranslationAction.execute = new_execute

0 comments on commit cbb6b7e

Please sign in to comment.