Skip to content

Commit

Permalink
Update page draft_title immediately when title segment is edited
Browse files Browse the repository at this point in the history
  • Loading branch information
kaedroho committed Nov 9, 2020
1 parent 4893a8b commit 4d57891
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
28 changes: 26 additions & 2 deletions wagtail_localize/models.py
Expand Up @@ -18,9 +18,11 @@
Subquery,
Exists,
OuterRef,
Q
Q,
F
)
from django.db.models.signals import post_delete
from django.db.models.signals import post_delete, post_save
from django.dispatch import receiver
from django.utils import timezone
from django.utils.encoding import force_text
from django.utils.text import capfirst, slugify
Expand Down Expand Up @@ -1050,6 +1052,28 @@ def get_comment(self):
return _("Machine translated on {date}").format(date=self.updated_at.strftime(DATE_FORMAT))


@receiver(post_save, sender=StringTranslation)
def post_save_string_translation(instance, **kwargs):
# If the StringTranslation is for a page title, update that page's draft_title field
if instance.context.path == 'title':
# Note: if this StringTranslation isn't for a page, this should do nothing
Page.objects.filter(
translation_key=instance.context.object_id,
locale_id=instance.locale_id
).update(draft_title=instance.data)


@receiver(post_delete, sender=StringTranslation)
def post_delete_string_translation(instance, **kwargs):
# If the StringTranslation is for a page title, reset that page's draft title to the main title
if instance.context.path == 'title':
# Note: if this StringTranslation isn't for a page, this should do nothing
Page.objects.filter(
translation_key=instance.context.object_id,
locale_id=instance.locale_id
).update(draft_title=F('title'))


class Template(models.Model):
BASE_UUID_NAMESPACE = uuid.UUID("4599eabc-3f8e-41a9-be61-95417d26a8cd")

Expand Down
Expand Up @@ -111,7 +111,8 @@ const EditorHeader: FunctionComponent<EditorHeaderProps> = ({
breadcrumb,
sourceLocale,
locale,
translations
translations,
stringTranslations
}) => {
// Build actions
let actions = [];
Expand Down Expand Up @@ -162,9 +163,22 @@ const EditorHeader: FunctionComponent<EditorHeaderProps> = ({
/>
];

// Title
// Allow the title to be overridden by the segment that represents the "title" field on Pages.
let title = object.title;
if (object.titleSegmentId) {
Array.from(stringTranslations.entries()).forEach(
([segmentId, stringTranslation]) => {
if (segmentId == object.titleSegmentId) {
title = stringTranslation.value;
}
}
);
}

return (
<Header
title={object.title}
title={title}
breadcrumb={breadcrumb}
actions={actions}
meta={meta}
Expand Down
Expand Up @@ -113,6 +113,7 @@ export interface EditorProps {
csrfToken: string;
object: {
title: string;
titleSegmentId: number | null;
isLive: boolean;
isLocked: boolean;
lastPublishedDate: string | null;
Expand Down
9 changes: 9 additions & 0 deletions wagtail_localize/views/edit_translation.py
Expand Up @@ -358,6 +358,7 @@ def edit_translation(request, translation, instance):
tab_helper = TabHelper(source_instance)

breadcrumb = []
title_segment_id = None
if isinstance(instance, Page):
# find the closest common ancestor of the pages that this user has direct explore permission
# (i.e. add/edit/publish/lock) over; this will be the root of the breadcrumb
Expand All @@ -373,6 +374,13 @@ def edit_translation(request, translation, instance):
for page in instance.get_ancestors(inclusive=False).descendant_of(cca, inclusive=True)
]

# Set to the ID of a string segment that represents the title.
# If this segment has a translation, the title will be replaced with that translation.
try:
title_segment_id = string_segments.get(context__path='title').id
except StringSegment.DoesNotExist:
pass

machine_translator = None
translator = get_machine_translator()
if translator and translator.can_translate(translation.source.locale, translation.target_locale):
Expand Down Expand Up @@ -414,6 +422,7 @@ def edit_translation(request, translation, instance):
'props': json.dumps({
'object': {
'title': str(instance),
'titleSegmentId': title_segment_id,
'isLive': is_live,
'isLocked': is_locked,
'lastPublishedDate': last_published_at.strftime('%-d %B %Y') if last_published_at is not None else None,
Expand Down

0 comments on commit 4d57891

Please sign in to comment.