Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix 'ready for review'/'published' status bug #43

Merged
merged 4 commits into from
May 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion wagtail_localize_rws_languagecloud/importer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import polib

from django.core.exceptions import SuspiciousOperation, ValidationError
from django.core.exceptions import (
ObjectDoesNotExist,
SuspiciousOperation,
ValidationError,
)
from django.db import transaction
from wagtail.core.models import Page

Expand Down Expand Up @@ -72,4 +76,15 @@ def import_po(self, translation, target_file):
)

self.db_source_file.internal_status = LanguageCloudFile.STATUS_IMPORTED

try:
instance = translation.get_target_instance()
if isinstance(instance, Page):
self.db_source_file.revision = instance.get_latest_revision()
except ObjectDoesNotExist:
# we will hit this case if we failed with a
# `MissingRelatedObjectError` or `ValidationError`
# trying to call `save_target()` a few lines up
pass

self.db_source_file.save()
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 3.1.14 on 2022-05-06 14:08

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
("wagtail_localize_rws_languagecloud", "0005_languagecloudprojectsettings"),
]

operations = [
migrations.AddField(
model_name="languagecloudfile",
name="revision",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.CASCADE,
to="wagtailcore.pagerevision",
),
),
]
60 changes: 45 additions & 15 deletions wagtail_localize_rws_languagecloud/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.db import models
from django.utils.functional import cached_property
from django.utils.translation import gettext_lazy
from wagtail.core.models import Page
from wagtail.core.models import Page, PageRevision

from wagtail_localize.components import register_translation_component
from wagtail_localize.models import Translation, TranslationSource
Expand Down Expand Up @@ -95,6 +95,12 @@ class LanguageCloudFile(StatusModel):
project = models.ForeignKey(LanguageCloudProject, on_delete=models.CASCADE)
lc_source_file_id = models.CharField(blank=True, max_length=255)
create_attempts = models.IntegerField(default=0)
revision = models.ForeignKey(
PageRevision,
on_delete=models.CASCADE,
blank=True,
null=True,
)

class Meta:
unique_together = [
Expand All @@ -111,11 +117,44 @@ def is_failed(self):
return self.lc_source_file_id == "" and self.create_attempts >= 3

@property
def instance_is_published(self):
def published_status(self):
PUBLISHED = gettext_lazy("Translations published")
RECEIVED = gettext_lazy("Translations received")
READY_TO_REVIEW = gettext_lazy("Translations ready for review")

instance = self.translation.get_target_instance()

if not isinstance(instance, Page):
return True
return not instance.has_unpublished_changes
# snippets are always published immediately
return PUBLISHED

if not self.revision:
"""
There's a couple of reasons we can end up here:
1. For legacy reasons: we didn't always save a revision on this object.
Objects created with <=0.8.0 willl have a NULL revision
2. If we threw an error trying to call save_target() in import_po()

In this case, say we've got the translations back
but we don't know if they are published or not
"""
return RECEIVED

if self.revision == instance.live_revision:
# The PageRevision attached to this LanguageCloudFile is published
return PUBLISHED

recent_revisions = (
instance.revisions.all()
.filter(created_at__gt=self.revision.created_at)
.order_by("-created_at", "-id")
)
for revision in recent_revisions:
if revision == instance.live_revision:
# Some other more recent PageRevision is published
return PUBLISHED

return READY_TO_REVIEW

@property
def combined_status(self):
Expand All @@ -134,17 +173,8 @@ def combined_status(self):
if self.project.lc_project_status == LanguageCloudStatus.ARCHIVED:
return gettext_lazy("LanguageCloud project archived")

if (
self.internal_status == LanguageCloudFile.STATUS_IMPORTED
and not self.instance_is_published
):
return gettext_lazy("Translations ready for review")

if (
self.internal_status == LanguageCloudFile.STATUS_IMPORTED
and self.instance_is_published
):
return gettext_lazy("Translations published")
if self.internal_status == LanguageCloudFile.STATUS_IMPORTED:
return self.published_status

if self.internal_status == LanguageCloudFile.STATUS_ERROR:
return gettext_lazy("Error importing PO file")
Expand Down
45 changes: 43 additions & 2 deletions wagtail_localize_rws_languagecloud/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,33 @@ def test_translations_ready(self):
self.file.save()
self.assertEqual(self.file.combined_status, "Translations ready for review")

def test_translations_published(self):
def test_translations_no_revision(self):
self.project.lc_project_id = "12345"
self.file.lc_source_file_id = "67890"
self.file.internal_status = LanguageCloudFile.STATUS_IMPORTED
importer = Importer(self.file, logging.getLogger("dummy"))
importer.import_po(self.translation, str(self.po_file))
self.project.save()
self.file.revision = None
self.file.save()
self.assertEqual(self.file.combined_status, "Translations received")

def test_translations_published_directly(self):
self.project.lc_project_id = "12345"
self.file.lc_source_file_id = "67890"
self.file.internal_status = LanguageCloudFile.STATUS_IMPORTED
importer = Importer(self.file, logging.getLogger("dummy"))
importer.import_po(self.translation, str(self.po_file))
instance = self.translation.source.get_translated_instance(
self.translation.target_locale
)
instance.get_latest_revision().publish() # publish the translated revision directly
self.project.save()
self.file.save()
self.translation.save()
self.assertEqual(self.file.combined_status, "Translations published")

def test_translations_published_with_edits(self):
self.project.lc_project_id = "12345"
self.file.lc_source_file_id = "67890"
self.file.internal_status = LanguageCloudFile.STATUS_IMPORTED
Expand All @@ -85,12 +111,27 @@ def test_translations_published(self):
instance = self.translation.source.get_translated_instance(
self.translation.target_locale
)
instance.get_latest_revision().publish()
instance.save_revision().publish() # save a new revision and publish that
self.project.save()
self.file.save()
self.translation.save()
self.assertEqual(self.file.combined_status, "Translations published")

def test_translations_not_yet_published_with_later_edits(self):
self.project.lc_project_id = "12345"
self.file.lc_source_file_id = "67890"
self.file.internal_status = LanguageCloudFile.STATUS_IMPORTED
importer = Importer(self.file, logging.getLogger("dummy"))
importer.import_po(self.translation, str(self.po_file))
instance = self.translation.source.get_translated_instance(
self.translation.target_locale
)
instance.save_revision() # save a new revision, but don't publish it yet
self.project.save()
self.file.save()
self.translation.save()
self.assertEqual(self.file.combined_status, "Translations ready for review")

def test_project_create_failed(self):
self.project.create_attempts = 3
self.project.save()
Expand Down