Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
kaedroho committed Jan 2, 2020
1 parent f82b125 commit 453286e
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 28 deletions.
2 changes: 2 additions & 0 deletions wagtail_localize/translation_engines/pontoon/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
Segment,
SegmentLocation,
TemplateLocation,
SegmentTranslationContext,
)

from .models import (
Expand All @@ -42,6 +43,7 @@ def import_resource(self, resource, language, old_po, new_po):
)
translation, created = segment.translations.get_or_create(
language=language,
context=SegmentTranslationContext.from_msgctxt(changed_entry.msgctxt) if changed_entry.msgctxt else None,
defaults={
"text": changed_entry.msgstr,
"updated_at": timezone.now(),
Expand Down
7 changes: 6 additions & 1 deletion wagtail_localize/translation_engines/pontoon/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def get_segments(self):
"""
Gets all segments that are in the latest submission to Pontoon.
"""
return Segment.objects.filter(locations__revision_id=self.current_revision_id)
return SegmentLocation.objects.filter(revision_id=self.current_revision_id)

def get_all_segments(self, annotate_obsolete=False):
"""
Expand All @@ -163,6 +163,11 @@ def get_all_segments(self, annotate_obsolete=False):

return segments.distinct()

def get_obsolete_segments(self):
"""
"""

def find_translatable_submission(self, language):
"""
Look to see if a submission is ready for translating.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Generated by Django 2.2.9 on 2019-12-31 15:30

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


class Migration(migrations.Migration):

dependencies = [
('wagtail_localize', '0002_initial_data'),
('wagtail_localize_translation_memory', '0011_relatedobjectlocation'),
]

operations = [
migrations.CreateModel(
name='SegmentTranslationContext',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('path_id', models.UUIDField()),
('path', models.TextField()),
('object', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='+', to='wagtail_localize_translation_memory.TranslatableObject')),
],
options={
'unique_together': {('object', 'path_id')},
},
),
migrations.AddField(
model_name='relatedobjectlocation',
name='context',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='wagtail_localize_translation_memory.SegmentTranslationContext'),
),
migrations.AddField(
model_name='segmentlocation',
name='context',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='wagtail_localize_translation_memory.SegmentTranslationContext'),
),
migrations.AddField(
model_name='segmenttranslation',
name='context',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='translations', to='wagtail_localize_translation_memory.SegmentTranslationContext'),
),
migrations.AddField(
model_name='templatelocation',
name='context',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='wagtail_localize_translation_memory.SegmentTranslationContext'),
),
migrations.AlterUniqueTogether(
name='segmenttranslation',
unique_together={('language', 'translation_of', 'context')},
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Generated by Django 2.2.9 on 2019-12-31 16:30

from django.db import migrations


def populate_segmenttranslationcontext(apps, schema_editor):
SegmentLocation = apps.get_model('wagtail_localize_translation_memory.SegmentLocation')
TemplateLocation = apps.get_model('wagtail_localize_translation_memory.TemplateLocation')
RelatedObjectLocation = apps.get_model('wagtail_localize_translation_memory.RelatedObjectLocation')
SegmentTranslation = apps.get_model('wagtail_localize_translation_memory.SegmentTranslation')
SegmentTranslationContext = apps.get_model('wagtail_localize_translation_memory.SegmentTranslationContext')

def update_location_model(model):
for location in model.objects.select_related('revision'):
context, created = SegmentTranslationContext.objects.get_or_create(
object_id=location.revision.object_id,
path_id=SegmentTranslationContext.get_path_id(location.path),
defaults={
'path': location.path,
}
)

location.context = context
location.save(update_fields=['context'])

update_location_model(SegmentLocation)
update_location_model(TemplateLocation)
update_location_model(RelatedObjectLocation)

import pdb; pdb.set_trace()

raise Exception


class Migration(migrations.Migration):

dependencies = [
('wagtail_localize_translation_memory', '0012_segmenttranslationcontext'),
]

operations = [
migrations.RunPython(populate_segmenttranslationcontext),
]
64 changes: 43 additions & 21 deletions wagtail_localize/translation_memory/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,31 +232,13 @@ def create_or_update_translation(self, locale, translation=None):
return translation, created


class SegmentQuerySet(models.QuerySet):
def annotate_translation(self, language):
"""
Adds a 'translation' field to the segments containing the
text content of the segment translated into the specified
language.
"""
return self.annotate(
translation=Subquery(
SegmentTranslation.objects.filter(
translation_of_id=OuterRef("pk"), language_id=pk(language)
).values("text")
)
)


class Segment(models.Model):
UUID_NAMESPACE = uuid.UUID("59ed7d1c-7eb5-45fa-9c8b-7a7057ed56d7")

language = models.ForeignKey("wagtail_localize.Language", on_delete=models.CASCADE)
text_id = models.UUIDField()
text = models.TextField()

objects = SegmentQuerySet.as_manager()

@classmethod
def get_text_id(cls, text):
return uuid.uuid5(cls.UUID_NAMESPACE, text)
Expand All @@ -281,23 +263,60 @@ class Meta:
unique_together = [("language", "text_id")]


class SegmentTranslationContext(models.Model):
object = models.ForeignKey(
TranslatableObject, on_delete=models.CASCADE, related_name="+"
)
path_id = models.UUIDField()
path = models.TextField()

class Meta:
unique_together = [
("object", "path_id"),
]

@classmethod
def get_path_id(cls, path):
return uuid.uuid5(uuid.UUID("fcab004a-2b50-11ea-978f-2e728ce88125"), path)

def save(self, *args, **kwargs):
if self.path and self.path_id is None:
self.path_id = self.get_path_id(self.path)

return super().save(*args, **kwargs)

def as_string(self):
"""
Creates a string that can be used in the "msgctxt" field of PO files.
"""
pass

def get_from_string(self, msgctxt):
"""
Looks for the SegmentTranslationContext that the given string represents.
"""
pass


class SegmentTranslation(models.Model):
translation_of = models.ForeignKey(
Segment, on_delete=models.CASCADE, related_name="translations"
)
language = models.ForeignKey("wagtail_localize.Language", on_delete=models.CASCADE)
context = models.ForeignKey(SegmentTranslationContext, on_delete=models.SET_NULL, null=True, blank=True, related_name="translations")
text = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)

class Meta:
unique_together = [("language", "translation_of")]
unique_together = [("language", "translation_of", "context")]

@classmethod
def from_text(cls, translation_of, language, text):
def from_text(cls, translation_of, language, context, text):
segment, created = cls.objects.get_or_create(
translation_of=translation_of,
language_id=pk(language),
context_id=pk(context),
defaults={"text": text},
)

Expand Down Expand Up @@ -332,6 +351,7 @@ class BaseLocation(models.Model):
revision = models.ForeignKey(TranslatableRevision, on_delete=models.CASCADE)
path = models.TextField()
order = models.PositiveIntegerField()
context = models.ForeignKey(SegmentTranslationContext, on_delete=models.SET_NULL, null=True, blank=True, related_name="+")

class Meta:
abstract = True
Expand All @@ -347,7 +367,9 @@ def annotate_translation(self, language):
return self.annotate(
translation=Subquery(
SegmentTranslation.objects.filter(
translation_of_id=OuterRef("segment_id"), language_id=pk(language)
translation_of_id=OuterRef("segment_id"),
language_id=pk(language),
context_id=OuterRef("context_id"),
).values("text")
)
)
Expand Down
11 changes: 5 additions & 6 deletions wagtail_localize/translation_memory/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,16 @@ def get_translation_progress(revision_id, language):
- The number of segments that have been translated into the language
"""
# Get QuerySet of Segments that need to be translated
required_segments = Segment.objects.filter(
id__in=SegmentLocation.objects.filter(revision_id=revision_id).values_list(
"segment_id"
)
)
required_segments = SegmentLocation.objects.filter(revision_id=revision_id)

# Annotate each Segment with a flag that indicates whether the segment is translated
# into the specified language
required_segments = required_segments.annotate(
is_translated=Exists(
SegmentTranslation.objects.filter(
translation_of_id=OuterRef("pk"), language=language
translation_of_id=OuterRef("segment_id"),
#context_id=OuterRef("context_id"),
language=language,
)
)
)
Expand All @@ -58,6 +56,7 @@ def get_translation_progress(revision_id, language):
)
).aggregate(total_segments=Count("pk"), translated_segments=Sum("is_translated_i"))

print(aggs["total_segments"], aggs["translated_segments"])
return aggs["total_segments"], aggs["translated_segments"]


Expand Down

0 comments on commit 453286e

Please sign in to comment.