Skip to content

Commit

Permalink
Implement construct_synced_page_tree_list
Browse files Browse the repository at this point in the history
  • Loading branch information
zerolab committed Apr 5, 2022
1 parent 592a053 commit 8f88742
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions wagtail_localize/wagtail_hooks.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import List
from urllib.parse import urlencode

from django.contrib.admin.utils import quote
Expand Down Expand Up @@ -466,3 +467,45 @@ def convert_to_alias_message(data):
def register_icons(icons):
# icon id "wagtail-localize-convert" (which translates to `.icon-wagtail-localize-convert`)
return icons + ["wagtail_localize/icons/wagtail-localize-convert.svg"]


if WAGTAIL_VERSION >= (2, 17):
from .models import LocaleSynchronization

@hooks.register("construct_synced_page_tree_list")
def construct_synced_page_tree_list(pages: List[Page], action: str):

locale_sync_map = {}
for page in pages:
# TODO: what about locale C follows B which follows A, when we come in from A?
if page.locale_id not in locale_sync_map:
locale_sync_map[page.locale_id] = list(
LocaleSynchronization.objects.filter(
sync_from=page.locale_id
).values_list("locale", flat=True)
)

page_list = {}
if action == "unpublish":
for page in pages:
if not locale_sync_map[page.locale_id]:
# no locales sync from this page locale
continue

page_list[page] = Page.objects.translation_of(
page, inclusive=False
).filter(
alias_of__isnull=True, locale__in=locale_sync_map[page.locale_id]
)
elif action in ["move", "delete"]:
for page in pages:
if not locale_sync_map[page.locale_id]:
# no locales sync from this page locale
continue

# only include those translations or aliases from locales that sync from this locale
page_list[page] = Page.objects.translation_of(
page, inclusive=False
).filter(locale__in=locale_sync_map[page.locale_id])

return page_list

0 comments on commit 8f88742

Please sign in to comment.