Skip to content

Commit

Permalink
Merge pull request #2 from DylannCordel/fix-unreduce-wagtail52
Browse files Browse the repository at this point in the history
update reduce check: it's know fixed in wagtail 5.2 💖
  • Loading branch information
DylannCordel authored Sep 18, 2023
2 parents 904d4bf + fcce5c0 commit 7aeb003
Showing 1 changed file with 21 additions and 23 deletions.
44 changes: 21 additions & 23 deletions wagtail_parler/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,21 @@
import importlib
from typing import Optional
from typing import Tuple
import warnings

# Django imports
from django.apps import AppConfig
from django.utils.translation import gettext_lazy as _

# Third Party
from wagtail.blocks.base import Block
from wagtail import VERSION as wagtail_version


class WagtailParlerConfig(AppConfig):
name = "wagtail_parler"
verbose_name = _("Wagtail Parler 🧀 🐦")

def ready(self) -> None:
if Block.__reduce__.__qualname__ == "object.__reduce__":
if wagtail_version < (5, 2):
"""
Monkey Patch wagtail to be able to Pickle Blocks.
FIXME: remove this when related PR will be merged into wagtail.
Expand All @@ -27,27 +26,26 @@ def ready(self) -> None:
we need one to be able to Pickle instances of Blocks.
"""

# Third Party
from wagtail.blocks.base import Block

def block_reduce(self: Block) -> Tuple:
path, args, kwargs = self.deconstruct()
return unreduce, (path, (args, kwargs))
return block_unreduce, (path, (args, kwargs))

Block.__reduce__ = block_reduce
else: # pragma: no cover
warnings.warn(
"Monkey Patch `block_reduce` is deprecated with this version of Wagtail",
DeprecationWarning,
stacklevel=2,
)


def unreduce(path: str, args_and_kwargs: Optional[Tuple] = None) -> object:
path_part = path.rsplit(".", 1)
module = importlib.import_module(path_part[0])
cls = getattr(module, path_part[1])
args: Tuple = tuple()
kwargs = {}
if args_and_kwargs and len(args_and_kwargs) >= 1:
args = args_and_kwargs[0]
if len(args_and_kwargs) >= 2:
kwargs = args_and_kwargs[1]
return cls(*args, **kwargs) # type: ignore


if wagtail_version < (5, 2):

def block_unreduce(path: str, args_and_kwargs: Optional[Tuple] = None) -> object:
path_part = path.rsplit(".", 1)
module = importlib.import_module(path_part[0])
cls = getattr(module, path_part[1])
args: Tuple = tuple()
kwargs = {}
if args_and_kwargs and len(args_and_kwargs) >= 1:
args = args_and_kwargs[0]
if len(args_and_kwargs) >= 2:
kwargs = args_and_kwargs[1]
return cls(*args, **kwargs) # type: ignore

0 comments on commit 7aeb003

Please sign in to comment.