← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] ~cjwatson/launchpad:stormify-distroserieslanguage into launchpad:master

 

Colin Watson has proposed merging ~cjwatson/launchpad:stormify-distroserieslanguage into launchpad:master.

Commit message:
Convert DistroSeriesLanguage to Storm

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~cjwatson/launchpad/+git/launchpad/+merge/392120
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~cjwatson/launchpad:stormify-distroserieslanguage into launchpad:master.
diff --git a/lib/lp/registry/model/distroseries.py b/lib/lp/registry/model/distroseries.py
index d1940f3..9f0fc8c 100644
--- a/lib/lp/registry/model/distroseries.py
+++ b/lib/lp/registry/model/distroseries.py
@@ -102,7 +102,10 @@ from lp.services.database.sqlbase import (
     SQLBase,
     sqlvalues,
     )
-from lp.services.database.stormexpr import fti_search
+from lp.services.database.stormexpr import (
+    fti_search,
+    IsTrue,
+    )
 from lp.services.librarian.interfaces import ILibraryFileAliasSet
 from lp.services.librarian.model import (
     LibraryFileAlias,
@@ -717,15 +720,11 @@ class DistroSeries(SQLBase, BugTargetBase, HasSpecificationsMixin,
 
     @property
     def distroserieslanguages(self):
-        result = DistroSeriesLanguage.select(
-            "DistroSeriesLanguage.language = Language.id AND "
-            "DistroSeriesLanguage.distroseries = %d AND "
-            "Language.visible = TRUE" % self.id,
-            prejoinClauseTables=["Language"],
-            clauseTables=["Language"],
-            prejoins=["distroseries"],
-            orderBy=["Language.englishname"])
-        return result
+        return IStore(DistroSeriesLanguage).find(
+            DistroSeriesLanguage,
+            DistroSeriesLanguage.language == Language.id,
+            DistroSeriesLanguage.distroseries == self,
+            IsTrue(Language.visible)).order_by(Language.englishname)
 
     @property
     def bug_reporting_guidelines(self):
@@ -899,8 +898,8 @@ class DistroSeries(SQLBase, BugTargetBase, HasSpecificationsMixin,
 
     def getDistroSeriesLanguage(self, language):
         """See `IDistroSeries`."""
-        return DistroSeriesLanguage.selectOneBy(
-            distroseries=self, language=language)
+        return IStore(DistroSeriesLanguage).find(
+            DistroSeriesLanguage, distroseries=self, language=language).one()
 
     def getDistroSeriesLanguageOrDummy(self, language):
         """See `IDistroSeries`."""
@@ -913,25 +912,25 @@ class DistroSeries(SQLBase, BugTargetBase, HasSpecificationsMixin,
         """See `IDistroSeries`."""
         # first find the set of all languages for which we have pofiles in
         # the distribution that are visible and not English
-        langidset = set(IStore(Language).find(
-            Language.id,
-            Language.visible == True,
+        langset = set(IStore(Language).find(
+            Language,
+            IsTrue(Language.visible),
             Language.id == POFile.languageID,
             Language.code != 'en',
             POFile.potemplateID == POTemplate.id,
             POTemplate.distroseries == self,
-            POTemplate.iscurrent == True).config(distinct=True))
+            IsTrue(POTemplate.iscurrent)).config(distinct=True))
 
         # now run through the existing DistroSeriesLanguages for the
         # distroseries, and update their stats, and remove them from the
         # list of languages we need to have stats for
         for distroserieslanguage in self.distroserieslanguages:
             distroserieslanguage.updateStatistics(ztm)
-            langidset.discard(distroserieslanguage.language.id)
+            langset.discard(distroserieslanguage.language)
         # now we should have a set of languages for which we NEED
         # to have a DistroSeriesLanguage
-        for langid in langidset:
-            drl = DistroSeriesLanguage(distroseries=self, languageID=langid)
+        for lang in langset:
+            drl = DistroSeriesLanguage(distroseries=self, language=lang)
             drl.updateStatistics(ztm)
         # lastly, we need to update the message count for this distro
         # series itself
diff --git a/lib/lp/translations/model/distroserieslanguage.py b/lib/lp/translations/model/distroserieslanguage.py
index 1d5a9ae..28e79d7 100644
--- a/lib/lp/translations/model/distroserieslanguage.py
+++ b/lib/lp/translations/model/distroserieslanguage.py
@@ -12,23 +12,28 @@ __all__ = [
     ]
 
 from datetime import datetime
+from operator import itemgetter
 
 import pytz
-from sqlobject import (
-    ForeignKey,
-    IntCol,
+from storm.expr import LeftJoin
+from storm.locals import (
+    DateTime,
+    Desc,
+    Int,
+    Join,
+    Reference,
     )
 from zope.interface import implementer
 
+from lp.registry.model.sourcepackagename import SourcePackageName
 from lp.services.database.constants import (
     DEFAULT,
     UTC_NOW,
     )
-from lp.services.database.datetimecol import UtcDateTimeCol
-from lp.services.database.sqlbase import (
-    SQLBase,
-    sqlvalues,
-    )
+from lp.services.database.decoratedresultset import DecoratedResultSet
+from lp.services.database.interfaces import IStore
+from lp.services.database.stormbase import StormBase
+from lp.services.database.stormexpr import IsTrue
 from lp.translations.interfaces.distroserieslanguage import (
     IDistroSeriesLanguage,
     IDistroSeriesLanguageSet,
@@ -37,30 +42,41 @@ from lp.translations.model.pofile import (
     DummyPOFile,
     POFile,
     )
-from lp.translations.model.potemplate import get_pofiles_for
+from lp.translations.model.potemplate import (
+    get_pofiles_for,
+    POTemplate,
+    )
+from lp.translations.model.translationgroup import TranslationGroup
 from lp.translations.model.translator import Translator
 from lp.translations.utilities.rosettastats import RosettaStats
 
 
 @implementer(IDistroSeriesLanguage)
-class DistroSeriesLanguage(SQLBase, RosettaStats):
+class DistroSeriesLanguage(StormBase, RosettaStats):
     """See `IDistroSeriesLanguage`.
 
     A SQLObject based implementation of IDistroSeriesLanguage.
     """
 
-    _table = 'DistroSeriesLanguage'
+    __storm_table__ = 'DistroSeriesLanguage'
+
+    id = Int(primary=True)
+    distroseries_id = Int(name='distroseries', allow_none=True, default=None)
+    distroseries = Reference(distroseries_id, 'DistroSeries.id')
+    language_id = Int(name='language', allow_none=False)
+    language = Reference(language_id, 'Language.id')
+    currentcount = Int(allow_none=False, default=0)
+    updatescount = Int(allow_none=False, default=0)
+    rosettacount = Int(allow_none=False, default=0)
+    unreviewed_count = Int(allow_none=False, default=0)
+    contributorcount = Int(allow_none=False, default=0)
+    dateupdated = DateTime(
+        name='dateupdated', tzinfo=pytz.UTC, default=DEFAULT)
 
-    distroseries = ForeignKey(foreignKey='DistroSeries',
-        dbName='distroseries', notNull=False, default=None)
-    language = ForeignKey(foreignKey='Language', dbName='language',
-        notNull=True)
-    currentcount = IntCol(notNull=True, default=0)
-    updatescount = IntCol(notNull=True, default=0)
-    rosettacount = IntCol(notNull=True, default=0)
-    unreviewed_count = IntCol(notNull=True, default=0)
-    contributorcount = IntCol(notNull=True, default=0)
-    dateupdated = UtcDateTimeCol(dbName='dateupdated', default=DEFAULT)
+    def __init__(self, distroseries, language):
+        super(DistroSeriesLanguage, self).__init__()
+        self.distroseries = distroseries
+        self.language = language
 
     @property
     def title(self):
@@ -71,15 +87,20 @@ class DistroSeriesLanguage(SQLBase, RosettaStats):
 
     @property
     def pofiles(self):
-        return POFile.select('''
-            POFile.language = %s AND
-            POFile.potemplate = POTemplate.id AND
-            POTemplate.distroseries = %s AND
-            POTemplate.iscurrent = TRUE
-            ''' % sqlvalues(self.language.id, self.distroseries.id),
-            clauseTables=['POTemplate'],
-            prejoins=["potemplate.sourcepackagename"],
-            orderBy=['-POTemplate.priority', 'POFile.id'])
+        tables = [
+            POFile,
+            Join(POTemplate, POFile.potemplate == POTemplate.id),
+            LeftJoin(
+                SourcePackageName,
+                POTemplate.sourcepackagename == SourcePackageName.id),
+            ]
+        result = IStore(POFile).using(*tables).find(
+            (POFile, SourcePackageName),
+            POFile.language == self.language,
+            POTemplate.distroseries == self.distroseries,
+            IsTrue(POTemplate.iscurrent),
+            ).order_by(Desc(POTemplate.priority), POFile.id)
+        return DecoratedResultSet(result, itemgetter(0))
 
     def getPOFilesFor(self, potemplates):
         """See `IDistroSeriesLanguage`."""
@@ -87,16 +108,15 @@ class DistroSeriesLanguage(SQLBase, RosettaStats):
 
     @property
     def translators(self):
-        return Translator.select('''
-            Translator.translationgroup = TranslationGroup.id AND
-            Distribution.translationgroup = TranslationGroup.id AND
-            Distribution.id = %s
-            Translator.language = %s
-            ''' % sqlvalues(self.distroseries.distribution.id,
-                            self.language.id),
-            orderBy=['id'],
-            clauseTables=['TranslationGroup', 'Distribution'],
-            distinct=True)
+        # Circular import.
+        from lp.registry.model.distribution import Distribution
+        return IStore(Translator).find(
+            Translator,
+            Translator.translationgroup == TranslationGroup.id,
+            Distribution.translationgroup == TranslationGroup.id,
+            Distribution.id == self.distroseries.distribution.id,
+            Translator.language == self.language,
+            ).order_by(Translator.id).config(distinct=True)
 
     @property
     def contributor_count(self):
diff --git a/lib/lp/translations/templates/distroseries-translations.pt b/lib/lp/translations/templates/distroseries-translations.pt
index 8592c4e..43c72b4 100644
--- a/lib/lp/translations/templates/distroseries-translations.pt
+++ b/lib/lp/translations/templates/distroseries-translations.pt
@@ -139,7 +139,7 @@
             </div>
           </div>
         </div>
-        <tal:stats condition="view/distroserieslanguages">
+        <tal:stats condition="not: view/distroserieslanguages/is_empty">
           <div class="yui-b top-portlet">
             <h2>Translation statistics</h2>
             <div tal:replace="structure context/@@+langchart" />