Merge ~pappacena/launchpad:update-stats-on-translation-copy into launchpad:master

Proposed by Thiago F. Pappacena
Status: Needs review
Proposed branch: ~pappacena/launchpad:update-stats-on-translation-copy
Merge into: launchpad:master
Diff against target: 87 lines (+34/-3)
2 files modified
lib/lp/translations/utilities/tests/test_file_importer.py (+26/-2)
lib/lp/translations/utilities/translation_import.py (+8/-1)
Reviewer Review Type Date Requested Status
Colin Watson (community) Needs Information
Review via email: mp+383669@code.launchpad.net

Commit message

Scheduling to run statistics update on PO and POT files when we copy translations.

To post a comment you must log in.
Revision history for this message
Thiago F. Pappacena (pappacena) wrote :

I'm not entirely sure that this is where the translations imports happen, so I would like a deeper review from someone that knows better about this translation part.

Revision history for this message
Ioana Lasc (ilasc) wrote (last edit ):

In theory this looks good to me too but I agree it needs a more senior review..

Revision history for this message
Colin Watson (cjwatson) wrote :

Hmm. Interesting, but I'm a bit confused. (Some of this confusion is because I don't know Translations very well.)

TranslationImporter.importFile is I think only called from POFile.importFromQueue and POTemplate.importFromQueue. I was going to say that I thought this sort of thing probably ought to go there rather than in the utility. But then I looked in more detail and found that the substance of it is supposed to be there already: if you look down towards the bottom of POFile.importFromQueue, it calls self.updateStatistics(), and if you look down towards the bottom of POTemplate.importFromQueue, it does a bunch of statistics updates as well. These are mostly similar to what POFileStatsJob does, so I think scheduling a POFileStatsJob here is mainly redundant and we shouldn't do it.

Of course, something is clearly wrong, but I'd approach it differently. Is it possible that the shared template handling in POFileStatsJob isn't done by the existing statistics update code in the models? I think that's probably what the problem is: we're already updating statistics for the POFile itself just fine (or for the POTemplate and all its POFiles), but we aren't dealing with other POFiles that share translations.

However, as I said, LP Translations really isn't my strong point and in particular I've never really got my head around translations sharing, so I'd suggest trying to get hold of William and discuss this with him.

review: Needs Information

Unmerged commits

b43979c... by Thiago F. Pappacena

Schedule statistic update on translation import

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/lib/lp/translations/utilities/tests/test_file_importer.py b/lib/lp/translations/utilities/tests/test_file_importer.py
index 013d13f..939b742 100644
--- a/lib/lp/translations/utilities/tests/test_file_importer.py
+++ b/lib/lp/translations/utilities/tests/test_file_importer.py
@@ -1,4 +1,4 @@
1# Copyright 2009-2010 Canonical Ltd. This software is licensed under the1# Copyright 2009-2020 Canonical Ltd. This software is licensed under the
2# GNU Affero General Public License version 3 (see the file LICENSE).2# GNU Affero General Public License version 3 (see the file LICENSE).
33
4"""Translation File Importer tests."""4"""Translation File Importer tests."""
@@ -12,13 +12,20 @@ from zope.component import getUtility
12from zope.security.proxy import removeSecurityProxy12from zope.security.proxy import removeSecurityProxy
1313
14from lp.registry.interfaces.person import IPersonSet14from lp.registry.interfaces.person import IPersonSet
15from lp.services.config import config
16from lp.services.job.runner import JobRunner
15from lp.services.librarianserver.testing.fake import FakeLibrarian17from lp.services.librarianserver.testing.fake import FakeLibrarian
16from lp.testing import TestCaseWithFactory18from lp.testing import (
19 admin_logged_in,
20 TestCaseWithFactory,
21 )
22from lp.testing.dbuser import dbuser
17from lp.testing.layers import (23from lp.testing.layers import (
18 LaunchpadZopelessLayer,24 LaunchpadZopelessLayer,
19 ZopelessDatabaseLayer,25 ZopelessDatabaseLayer,
20 )26 )
21from lp.translations.enums import TranslationPermission27from lp.translations.enums import TranslationPermission
28from lp.translations.interfaces.pofilestatsjob import IPOFileStatsJobSource
22from lp.translations.interfaces.potemplate import IPOTemplateSet29from lp.translations.interfaces.potemplate import IPOTemplateSet
23from lp.translations.interfaces.side import TranslationSide30from lp.translations.interfaces.side import TranslationSide
24from lp.translations.interfaces.translationfileformat import (31from lp.translations.interfaces.translationfileformat import (
@@ -395,6 +402,23 @@ class FileImporterTestCase(TestCaseWithFactory):
395 "POFileImporter.importFile did not create an "402 "POFileImporter.importFile did not create an "
396 "ITranslationMessage object in the database.")403 "ITranslationMessage object in the database.")
397404
405 # Checks if statistics update was scheduled correctly.
406 with admin_logged_in():
407 self.assertEqual(0, po_importer.pofile.translatedCount())
408 self.assertEqual(0, po_importer.pofile.untranslatedCount())
409 self.assertEqual(0, pot_importer.potemplate.translatedCount())
410 self.assertEqual(0, pot_importer.potemplate.untranslatedCount())
411
412 jobs = getUtility(IPOFileStatsJobSource).iterReady()
413 self.assertGreaterEqual(jobs.count(), 2)
414 with dbuser(config.IPOFileStatsJobSource.dbuser):
415 JobRunner(jobs).runAll()
416
417 self.assertEqual(1, po_importer.pofile.translatedCount())
418 self.assertEqual(0, po_importer.pofile.untranslatedCount())
419 self.assertEqual(0, pot_importer.potemplate.translatedCount())
420 self.assertEqual(1, pot_importer.potemplate.untranslatedCount())
421
398 def test_FileImporter_importFile_conflict(self):422 def test_FileImporter_importFile_conflict(self):
399 (pot_importer, po_importer) = (423 (pot_importer, po_importer) = (
400 self._createImporterForExportedEntries())424 self._createImporterForExportedEntries())
diff --git a/lib/lp/translations/utilities/translation_import.py b/lib/lp/translations/utilities/translation_import.py
index f9af932..5a3962a 100644
--- a/lib/lp/translations/utilities/translation_import.py
+++ b/lib/lp/translations/utilities/translation_import.py
@@ -1,4 +1,4 @@
1# Copyright 2009-2018 Canonical Ltd. This software is licensed under the1# Copyright 2009-2020 Canonical Ltd. This software is licensed under the
2# GNU Affero General Public License version 3 (see the file LICENSE).2# GNU Affero General Public License version 3 (see the file LICENSE).
33
4__metaclass__ = type4__metaclass__ = type
@@ -570,6 +570,7 @@ class FileImporter(object):
570570
571 :return: The errors encountered during the import.571 :return: The errors encountered during the import.
572 """572 """
573 from lp.translations.model import pofilestatsjob
573 # Collect errors here.574 # Collect errors here.
574 self.errors = []575 self.errors = []
575576
@@ -577,6 +578,12 @@ class FileImporter(object):
577 if message.msgid_singular:578 if message.msgid_singular:
578 self.importMessage(message)579 self.importMessage(message)
579580
581 # Update statistics in background.
582 if self.pofile is not None:
583 pofilestatsjob.schedule(self.pofile.id)
584 elif self.potemplate is not None:
585 pofilestatsjob.schedule(self.potemplate.id)
586
580 return self.errors, self.translation_file.syntax_warnings587 return self.errors, self.translation_file.syntax_warnings
581588
582 @property589 @property