Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add sync translations from latest revision support
Browse files Browse the repository at this point in the history
Dennis McGregor committed Sep 5, 2024
1 parent f465821 commit 8736f54
Showing 3 changed files with 25 additions and 6 deletions.
4 changes: 3 additions & 1 deletion wagtail_localize/models.py
Original file line number Diff line number Diff line change
@@ -448,7 +448,7 @@ def update_or_create_from_instance(cls, instance):
return source, created

@transaction.atomic
def update_from_db(self):
def update_from_db(self, from_latest_revision=False):
"""
Retrieves the source instance from the database and updates this TranslationSource
with its current contents.
@@ -457,6 +457,8 @@ def update_from_db(self):
Model.DoesNotExist: If the source instance has been deleted.
"""
instance = self.get_source_instance()
if from_latest_revision and isinstance(instance, RevisionMixin):
instance = instance.get_latest_revision_as_object()

if isinstance(instance, ClusterableModel):
self.content_json = instance.to_json()
Original file line number Diff line number Diff line change
@@ -37,7 +37,7 @@
<ul class="fields">
{% block visible_fields %}
{% for field in form.visible_fields %}
{% if field.name == 'publish_translations' %}
{% if field.name == 'publish_translations' or field.name == 'from_revision' %}
<li class="publish-translations-field">
<div class="field boolean_field checkbox_input">
<div class="field-content">
25 changes: 21 additions & 4 deletions wagtail_localize/views/update_translations.py
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@
from django.views.generic import TemplateView
from django.views.generic.detail import SingleObjectMixin
from wagtail.admin.views.pages.utils import get_valid_next_url_from_request
from wagtail.models import Page
from wagtail.models import Page, RevisionMixin
from wagtail.snippets.models import get_snippet_models
from wagtail.utils.version import get_main_version

@@ -29,6 +29,20 @@ class UpdateTranslationsForm(forms.Form):
),
required=False,
)
from_revision = forms.BooleanField(
label=gettext_lazy("Sync draft changes"),
help_text=gettext_lazy(
"This will update translations with current draft content. "
"If not set, then only published changes will be synced."
),
required=False,
)

def __init__(self, instance, *args, **kwargs):
super().__init__(*args, **kwargs)
source_instance = instance.get_source_instance()
if not isinstance(source_instance, RevisionMixin):
self.fields["from_revision"].widget = forms.HiddenInput()


class UpdateTranslationsView(SingleObjectMixin, TemplateView):
@@ -48,9 +62,9 @@ def get_subtitle(self):

def get_form(self):
if self.request.method == "POST":
return UpdateTranslationsForm(self.request.POST)
return UpdateTranslationsForm(self.object, self.request.POST)
else:
return UpdateTranslationsForm()
return UpdateTranslationsForm(self.object)

def get_success_message(self):
return _("Successfully updated translations for '{object}'").format(
@@ -130,7 +144,10 @@ def post(self, request, **kwargs):

@transaction.atomic
def form_valid(self, form):
self.object.update_from_db()
from_latest_revision = False
if form.cleaned_data["from_revision"]:
from_latest_revision = True
self.object.update_from_db(from_latest_revision)

enabled_translations = self.object.translations.filter(enabled=True)

0 comments on commit 8736f54

Please sign in to comment.