← Back to team overview

launchpad-reviewers team mailing list archive

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

 

Colin Watson has proposed merging ~cjwatson/launchpad:stormify-translations-queries into launchpad:master with ~cjwatson/launchpad:stormify-potranslation as a prerequisite.

Commit message:
Convert SQLObject-style queries in lp.translations to Storm

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~cjwatson/launchpad/+git/launchpad/+merge/395062
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~cjwatson/launchpad:stormify-translations-queries into launchpad:master.
diff --git a/lib/lp/registry/model/distroseries.py b/lib/lp/registry/model/distroseries.py
index 2811a42..e83a7dc 100644
--- a/lib/lp/registry/model/distroseries.py
+++ b/lib/lp/registry/model/distroseries.py
@@ -178,7 +178,6 @@ from lp.translations.model.hastranslationtemplates import (
     )
 from lp.translations.model.languagepack import LanguagePack
 from lp.translations.model.pofile import POFile
-from lp.translations.model.pofiletranslator import POFileTranslator
 from lp.translations.model.potemplate import (
     POTemplate,
     TranslationTemplatesCollection,
@@ -1362,6 +1361,9 @@ class DistroSeries(SQLBase, BugTargetBase, HasSpecificationsMixin,
 
     def getPOFileContributorsByLanguage(self, language):
         """See `IDistroSeries`."""
+        # Circular import.
+        from lp.translations.model.pofiletranslator import POFileTranslator
+
         contributors = IStore(Person).find(
             Person,
             POFileTranslator.personID == Person.id,
diff --git a/lib/lp/translations/doc/poexport-request-productseries.txt b/lib/lp/translations/doc/poexport-request-productseries.txt
index 1978e3e..53303a2 100644
--- a/lib/lp/translations/doc/poexport-request-productseries.txt
+++ b/lib/lp/translations/doc/poexport-request-productseries.txt
@@ -7,9 +7,9 @@
 
 This is a dummy logger class to capture the export's log messages.
 
-    >>> from lp.registry.model.person import Person
+    >>> from lp.registry.interfaces.person import IPersonSet
     >>> from lp.services.log.logger import FakeLogger
-    >>> person = Person.selectOneBy(name='name12')
+    >>> person = getUtility(IPersonSet).getByName('name12')
 
 An arbitrary logged-in user requests an export of all translations for
 Evolution series trunk.
@@ -18,16 +18,15 @@ At the UI level, this is easy.  At the level we are looking at now, this
 consists of a series of requests for all templates and translations attached
 to the product series.
 
-    >>> from lp.registry.model.product import Product
-    >>> from lp.registry.model.productseries import ProductSeries
-    >>> from lp.translations.model.potemplate import POTemplate
-    >>> evolution_product = Product.selectOneBy(name='evolution')
-    >>> evolution_trunk = ProductSeries.selectOneBy(
-    ...     product=evolution_product, name='trunk')
-    >>> potemplates = list(POTemplate.selectBy(productseries=evolution_trunk))
+    >>> from lp.registry.interfaces.product import IProductSet
+    >>> from lp.translations.interfaces.potemplate import IPOTemplateSet
+    >>> evolution_product = getUtility(IProductSet).getByName('evolution')
+    >>> evolution_trunk = evolution_product.getSeries('trunk')
+    >>> potemplates = list(getUtility(IPOTemplateSet).getSubset(
+    ...     productseries=evolution_trunk))
     >>> pofiles = []
     >>> for template in potemplates:
-    ...     pofiles += template.pofiles
+    ...     pofiles.extend(template.pofiles)
 
     >>> request_set = getUtility(IPOExportRequestSet)
     >>> request_set.addRequest(person, potemplates, pofiles)
diff --git a/lib/lp/translations/doc/poexport-request.txt b/lib/lp/translations/doc/poexport-request.txt
index cf6d13f..8e87314 100644
--- a/lib/lp/translations/doc/poexport-request.txt
+++ b/lib/lp/translations/doc/poexport-request.txt
@@ -233,9 +233,11 @@ just PO files.
     >>> product_template.productseries is None
     False
     >>> request_set.addRequest(person, product_template)
-    >>> alsa_template = POTemplate.selectOneBy(path='po/alsa-utils.pot')
+    >>> alsa_template = IStore(POTemplate).find(
+    ...     POTemplate, path='po/alsa-utils.pot').one()
     >>> alsa_es = alsa_template.getPOFileByLang('es')
-    >>> netapplet_template = POTemplate.selectOneBy(path='po/netapplet.pot')
+    >>> netapplet_template = IStore(POTemplate).find(
+    ...     POTemplate, path='po/netapplet.pot').one()
     >>> request_set.addRequest(
     ...     person, [alsa_template, netapplet_template], [alsa_es])
     >>> transaction.commit()
diff --git a/lib/lp/translations/doc/pofile.txt b/lib/lp/translations/doc/pofile.txt
index 9caf91d..286080b 100644
--- a/lib/lp/translations/doc/pofile.txt
+++ b/lib/lp/translations/doc/pofile.txt
@@ -611,10 +611,8 @@ If you have a distroseries and want to know all the people who
 contributed translations on a given language for that distroseries, you
 can use the getPOFileContributorsByLanguage() method of IDistroSeries.
 
-    >>> from lp.registry.model.distroseries import DistroSeries
-    >>> from lp.services.worlddata.model.language import Language
-    >>> hoary = DistroSeries.selectOneBy(name="hoary")
-    >>> spanish = Language.selectOneBy(code="es")
+    >>> hoary = distribution.getSeries("hoary")
+    >>> spanish = getUtility(ILanguageSet)["es"]
     >>> print_names(hoary.getPOFileContributorsByLanguage(spanish))
     jorge-gonzalez-gonzalez
     carlos
diff --git a/lib/lp/translations/doc/potmsgset.txt b/lib/lp/translations/doc/potmsgset.txt
index ade9470..d07038c 100644
--- a/lib/lp/translations/doc/potmsgset.txt
+++ b/lib/lp/translations/doc/potmsgset.txt
@@ -612,20 +612,14 @@ POTMsgSet.setSequence
 Finally, the new `IPOTMsgSet` should have an entry in the
 `TranslationTemplateItem` table once we assign a sequence number.
 
-    >>> from lp.translations.model.translationtemplateitem import (
-    ...     TranslationTemplateItem)
-
 First, we need a helper function to check whether the potmsgset exists
 in the table or not.
 
-
     >>> def is_potmsgset_in_potemplate(potmsgset, potemplate):
-    ...     items = [
-    ...         item.potmsgset.id
-    ...         for item in TranslationTemplateItem.selectBy(
-    ...             potemplate=potemplate, potmsgset=potmsgset)
-    ...         if item.sequence > 0
-    ...         ]
+    ...     items = {
+    ...         potmsgset.id
+    ...         for potmsgset in potemplate.getPOTMsgSets(prefetch=False)
+    ...         }
     ...     return potmsgset.id in items
 
 Let's create a new potmsgset object.
diff --git a/lib/lp/translations/doc/translationmessage-destroy.txt b/lib/lp/translations/doc/translationmessage-destroy.txt
index 8b52fcf..6709859 100644
--- a/lib/lp/translations/doc/translationmessage-destroy.txt
+++ b/lib/lp/translations/doc/translationmessage-destroy.txt
@@ -36,7 +36,7 @@ translation, we get two POFileTranslator records for each of the POFiles.
     # We need to be able to create persons and projects so let's just use
     # a global 'postgres' permission which allows everything.
     >>> switch_dbuser('postgres')
-    >>> from lp.services.database.sqlbase import sqlvalues
+    >>> from lp.services.database.interfaces import IStore
     >>> from lp.app.enums import ServiceUsage
     >>> from lp.testing.factory import LaunchpadObjectFactory
     >>> from lp.translations.model.pofiletranslator import POFileTranslator
@@ -62,7 +62,8 @@ translation, we get two POFileTranslator records for each of the POFiles.
     ...     pofile=devel_sr_pofile,
     ...     potmsgset=potmsgset,
     ...     translations=[u"blah"])
-    >>> print(POFileTranslator.select(
-    ...     "pofile IN (%s, %s)"
-    ...     % sqlvalues(devel_sr_pofile, stable_sr_pofile)).count())
+    >>> print(IStore(POFileTranslator).find(
+    ...     POFileTranslator,
+    ...     POFileTranslator.pofileID.is_in(
+    ...         (devel_sr_pofile.id, stable_sr_pofile.id))).count())
     2
diff --git a/lib/lp/translations/doc/translationrelicensingagreement.txt b/lib/lp/translations/doc/translationrelicensingagreement.txt
index ad6156d..c40f41b 100644
--- a/lib/lp/translations/doc/translationrelicensingagreement.txt
+++ b/lib/lp/translations/doc/translationrelicensingagreement.txt
@@ -5,6 +5,7 @@ Launchpad can decide whether they want their translations relicensed
 under BSD or not.
 
     >>> from zope.component import getUtility
+    >>> from lp.services.database.interfaces import IStore
     >>> from lp.testing import verifyObject
     >>> from lp.translations.model.translationrelicensingagreement \
     ...     import TranslationRelicensingAgreement
@@ -23,7 +24,8 @@ When Karl has not made his selection yet, it is marked as None.
 
     >>> print(translations_person.translations_relicensing_agreement)
     None
-    >>> choice = TranslationRelicensingAgreement.selectOneBy(person=person)
+    >>> choice = IStore(TranslationRelicensingAgreement).find(
+    ...     TranslationRelicensingAgreement, person=person).one()
     >>> print(choice)
     None
 
@@ -33,7 +35,8 @@ object.
     >>> translations_person.translations_relicensing_agreement = True
     >>> print(translations_person.translations_relicensing_agreement)
     True
-    >>> choice = TranslationRelicensingAgreement.selectOneBy(person=person)
+    >>> choice = IStore(TranslationRelicensingAgreement).find(
+    ...     TranslationRelicensingAgreement, person=person).one()
     >>> print(choice.allow_relicensing)
     True
 
@@ -47,6 +50,7 @@ A translator can also change their mind later.
     >>> translations_person.translations_relicensing_agreement = False
     >>> print(translations_person.translations_relicensing_agreement)
     False
-    >>> choice = TranslationRelicensingAgreement.selectOneBy(person=person)
+    >>> choice = IStore(TranslationRelicensingAgreement).find(
+    ...     TranslationRelicensingAgreement, person=person).one()
     >>> print(choice.allow_relicensing)
     False
diff --git a/lib/lp/translations/interfaces/translationmessage.py b/lib/lp/translations/interfaces/translationmessage.py
index f06c9e6..5b66292 100644
--- a/lib/lp/translations/interfaces/translationmessage.py
+++ b/lib/lp/translations/interfaces/translationmessage.py
@@ -333,17 +333,6 @@ class ITranslationMessageSet(Interface):
     def getByID(id):
         """Return the TranslationMessage with the given ID or None."""
 
-    def selectDirect(where=None, order_by=None):
-        """Return sequence of `TranslationMessage`s matching arguments.
-
-        This is meant for maintenance use.  If you find yourself using
-        it anywhere except in a manually-run script, try something else.
-
-        :param where: An SQL WHERE clause describing which messages to
-            return.
-        :param order_by: An SQL ORDER BY clause.
-        """
-
     def preloadDetails(messages, pofile=None, need_pofile=False,
                        need_potemplate=False, need_potemplate_context=False,
                        need_potranslation=False, need_potmsgset=False,
diff --git a/lib/lp/translations/model/pofiletranslator.py b/lib/lp/translations/model/pofiletranslator.py
index 3f1bf02..a8ea10a 100644
--- a/lib/lp/translations/model/pofiletranslator.py
+++ b/lib/lp/translations/model/pofiletranslator.py
@@ -7,23 +7,32 @@ __all__ = [
     'POFileTranslatorSet',
     ]
 
+from operator import itemgetter
 
 from sqlobject import ForeignKey
-from storm.expr import And
+from storm.expr import (
+    And,
+    Join,
+    LeftJoin,
+    )
 from storm.store import Store
 from zope.interface import implementer
 
 from lp.registry.interfaces.person import validate_public_person
+from lp.registry.model.distroseries import DistroSeries
+from lp.registry.model.product import Product
+from lp.registry.model.productseries import ProductSeries
+from lp.registry.model.sourcepackagename import SourcePackageName
 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.sqlbase import SQLBase
 from lp.translations.interfaces.pofiletranslator import (
     IPOFileTranslator,
     IPOFileTranslatorSet,
     )
 from lp.translations.model.pofile import POFile
+from lp.translations.model.potemplate import POTemplate
 
 
 @implementer(IPOFileTranslator)
@@ -47,17 +56,24 @@ class POFileTranslatorSet:
         if not ids:
             return None
 
+        origin = [
+            POFileTranslator,
+            Join(POFile, POFileTranslator.pofile == POFile.id),
+            Join(POTemplate, POFile.potemplate == POTemplate.id),
+            LeftJoin(
+                ProductSeries, POTemplate.productseries == ProductSeries.id),
+            LeftJoin(Product, ProductSeries.product == Product.id),
+            LeftJoin(DistroSeries, POTemplate.distroseries == DistroSeries.id),
+            LeftJoin(
+                SourcePackageName,
+                POTemplate.sourcepackagename == SourcePackageName.id),
+            ]
+        rows = IStore(POFileTranslator).using(*origin).find(
+            (POFileTranslator, POFile, POTemplate,
+             ProductSeries, Product, DistroSeries, SourcePackageName),
+            POFileTranslator.id.is_in(ids))
         # Listify prefetch query to force its execution here.
-        return list(POFileTranslator.select(
-            "POFileTranslator.id IN %s" % sqlvalues(ids),
-            prejoins=[
-                'pofile',
-                'pofile.potemplate',
-                'pofile.potemplate.productseries',
-                'pofile.potemplate.productseries.product',
-                'pofile.potemplate.distroseries',
-                'pofile.potemplate.sourcepackagename',
-                ]))
+        return list(DecoratedResultSet(rows, itemgetter(0)))
 
     def getForPersonPOFile(self, person, pofile):
         """See `IPOFileTranslatorSet`."""
diff --git a/lib/lp/translations/model/potemplate.py b/lib/lp/translations/model/potemplate.py
index b0b5e2a..ebdbc42 100644
--- a/lib/lp/translations/model/potemplate.py
+++ b/lib/lp/translations/model/potemplate.py
@@ -542,7 +542,7 @@ class POTemplate(SQLBase, RosettaStats):
 
     def getPOFileByPath(self, path):
         """See `IPOTemplate`."""
-        return POFile.selectOneBy(potemplate=self, path=path)
+        return IStore(POFile).find(POFile, potemplate=self, path=path).one()
 
     def getPOFileByLang(self, language_code):
         """See `IPOTemplate`."""
@@ -1281,17 +1281,18 @@ class POTemplateSet:
 
     def __iter__(self):
         """See `IPOTemplateSet`."""
-        res = POTemplate.select()
-        for potemplate in res:
+        for potemplate in IStore(POTemplate).find(POTemplate):
             yield potemplate
 
     def getAllByName(self, name):
         """See `IPOTemplateSet`."""
-        return POTemplate.selectBy(name=name, orderBy=['name', 'id'])
+        return IStore(POTemplate).find(POTemplate, name=name).order_by(
+            POTemplate.name, POTemplate.id)
 
     def getAllOrderByDateLastUpdated(self):
         """See `IPOTemplateSet`."""
-        return POTemplate.select(orderBy=['-date_last_updated'])
+        return IStore(POTemplate).find(POTemplate).order_by(
+            Desc(POTemplate.date_last_updated))
 
     def getSubset(self, distroseries=None, sourcepackagename=None,
                   productseries=None, iscurrent=None,
diff --git a/lib/lp/translations/model/translationgroup.py b/lib/lp/translations/model/translationgroup.py
index 994172d..80267ba 100644
--- a/lib/lp/translations/model/translationgroup.py
+++ b/lib/lp/translations/model/translationgroup.py
@@ -17,6 +17,7 @@ from sqlobject import (
     StringCol,
     )
 from storm.expr import (
+    Desc,
     Join,
     LeftJoin,
     )
@@ -100,8 +101,8 @@ class TranslationGroup(SQLBase):
     # get a translator by language or code
     def query_translator(self, language):
         """See ITranslationGroup."""
-        return Translator.selectOneBy(language=language,
-                                      translationgroup=self)
+        return IStore(Translator).find(
+            Translator, language=language, translationgroup=self).one()
 
     @property
     def products(self):
@@ -109,7 +110,8 @@ class TranslationGroup(SQLBase):
         # Avoid circular imports.
         from lp.registry.model.product import Product
 
-        return Product.selectBy(translationgroup=self.id, active=True)
+        return IStore(Product).find(
+            Product, translationgroup=self, active=True)
 
     @property
     def projects(self):
@@ -117,7 +119,8 @@ class TranslationGroup(SQLBase):
         # Avoid circular imports.
         from lp.registry.model.projectgroup import ProjectGroup
 
-        return ProjectGroup.selectBy(translationgroup=self.id, active=True)
+        return IStore(ProjectGroup).find(
+            ProjectGroup, translationgroup=self, active=True)
 
     # A limit of projects to get for the `top_projects`.
     TOP_PROJECTS_LIMIT = 6
@@ -260,10 +263,10 @@ class TranslationGroupSet:
         # group names from their respective celebrities.  For now,
         # just hard-code them so they show up at the top of the
         # listing of all translation groups.
-        for group in TranslationGroup.select(
-            orderBy=[
-                "-(name in ('launchpad-translators', 'ubuntu-translators'))",
-                "title"]):
+        for group in IStore(TranslationGroup).find(TranslationGroup).order_by(
+                Desc(TranslationGroup.name.is_in((
+                    'launchpad-translators', 'ubuntu-translators'))),
+                TranslationGroup.title):
             yield group
 
     def __getitem__(self, name):
@@ -306,4 +309,4 @@ class TranslationGroupSet:
 
     def getGroupsCount(self):
         """See ITranslationGroupSet."""
-        return TranslationGroup.select().count()
+        return IStore(TranslationGroup).find(TranslationGroup).count()
diff --git a/lib/lp/translations/model/translationmessage.py b/lib/lp/translations/model/translationmessage.py
index b323a20..cdbdca6 100644
--- a/lib/lp/translations/model/translationmessage.py
+++ b/lib/lp/translations/model/translationmessage.py
@@ -546,10 +546,6 @@ class TranslationMessageSet:
         except SQLObjectNotFound:
             return None
 
-    def selectDirect(self, where=None, order_by=None):
-        """See `ITranslationMessageSet`."""
-        return TranslationMessage.select(where, orderBy=order_by)
-
     def preloadDetails(self, messages, pofile=None, need_pofile=False,
                        need_potemplate=False, need_potemplate_context=False,
                        need_potranslation=False, need_potmsgset=False,
diff --git a/lib/lp/translations/model/translationsperson.py b/lib/lp/translations/model/translationsperson.py
index 0fa28d9..6c28d7b 100644
--- a/lib/lp/translations/model/translationsperson.py
+++ b/lib/lp/translations/model/translationsperson.py
@@ -30,11 +30,12 @@ from lp.app.interfaces.launchpad import ILaunchpadCelebrities
 from lp.registry.interfaces.person import IPerson
 from lp.registry.model.distribution import Distribution
 from lp.registry.model.distroseries import DistroSeries
+from lp.registry.model.person import PersonLanguage
 from lp.registry.model.product import Product
 from lp.registry.model.productseries import ProductSeries
 from lp.registry.model.projectgroup import ProjectGroup
 from lp.registry.model.teammembership import TeamParticipation
-from lp.services.database.sqlbase import sqlvalues
+from lp.services.database.interfaces import IStore
 from lp.services.propertycache import (
     cachedproperty,
     get_property_cache,
@@ -64,12 +65,12 @@ class TranslationsPerson:
     @property
     def translatable_languages(self):
         """See `ITranslationsPerson`."""
-        return Language.select("""
-            Language.id = PersonLanguage.language AND
-            PersonLanguage.person = %s AND
-            Language.code <> 'en' AND
-            Language.visible""" % sqlvalues(self.person),
-            clauseTables=['PersonLanguage'], orderBy='englishname')
+        return IStore(Language).find(
+            Language,
+            PersonLanguage.language == Language.id,
+            PersonLanguage.person == self.person,
+            Language.code != 'en',
+            Language.visible).order_by(Language.englishname)
 
     def getTranslationHistory(self, no_older_than=None):
         """See `ITranslationsPerson`."""
@@ -108,8 +109,8 @@ class TranslationsPerson:
 
         If they have made no explicit decision yet, return None.
         """
-        relicensing_agreement = TranslationRelicensingAgreement.selectOneBy(
-            person=self.person)
+        relicensing_agreement = IStore(TranslationRelicensingAgreement).find(
+            TranslationRelicensingAgreement, person=self.person).one()
         if relicensing_agreement is None:
             return None
         else:
@@ -123,8 +124,8 @@ class TranslationsPerson:
 
         If they have already made a decision, overrides it with the new one.
         """
-        relicensing_agreement = TranslationRelicensingAgreement.selectOneBy(
-            person=self.person)
+        relicensing_agreement = IStore(TranslationRelicensingAgreement).find(
+            TranslationRelicensingAgreement, person=self.person).one()
         if relicensing_agreement is None:
             relicensing_agreement = TranslationRelicensingAgreement(
                 person=self.person,
diff --git a/lib/lp/translations/scripts/fix_plural_forms.py b/lib/lp/translations/scripts/fix_plural_forms.py
index 8d264ae..df8e25c 100644
--- a/lib/lp/translations/scripts/fix_plural_forms.py
+++ b/lib/lp/translations/scripts/fix_plural_forms.py
@@ -11,12 +11,11 @@ __all__ = [
 
 from sqlobject import SQLObjectNotFound
 
-from lp.services.database.sqlbase import (
-    cursor,
-    sqlvalues,
-    )
+from lp.services.database.interfaces import IStore
+from lp.services.database.sqlbase import cursor
 from lp.translations.interfaces.translations import TranslationConstants
 from lp.translations.model.pofile import POFile
+from lp.translations.model.potmsgset import POTMsgSet
 from lp.translations.model.translationmessage import TranslationMessage
 from lp.translations.utilities.gettext_po_parser import POHeader
 from lp.translations.utilities.pluralforms import plural_form_mapper
@@ -49,11 +48,11 @@ def fix_pofile_plurals(pofile, logger, ztm):
     plural_forms_mapping = get_mapping_for_pofile_plurals(pofile)
     if plural_forms_mapping is not None:
         logger.info("Fixing PO file %s" % pofile.title)
-        pluralmessages = TranslationMessage.select("""
-            POTMsgSet.id = TranslationMessage.potmsgset AND
-            POTMsgSet.msgid_plural IS NOT NULL AND
-            TranslationMessage.pofile = %s""" % sqlvalues(pofile),
-            clauseTables=["POTMsgSet"])
+        pluralmessages = IStore(TranslationMessage).find(
+            TranslationMessage,
+            TranslationMessage.potmsgset == POTMsgSet.id,
+            POTMsgSet.msgid_plural != None,
+            TranslationMessage.pofile == pofile)
         for message in pluralmessages:
             logger.debug("\tFixing translations for '%s'" % (
                 message.potmsgset.singular_text))
diff --git a/lib/lp/translations/scripts/gettext_check_messages.py b/lib/lp/translations/scripts/gettext_check_messages.py
index fa6a388..b4e4345 100644
--- a/lib/lp/translations/scripts/gettext_check_messages.py
+++ b/lib/lp/translations/scripts/gettext_check_messages.py
@@ -11,13 +11,12 @@ from datetime import (
     )
 
 import six
-from zope.component import getUtility
+from storm.locals import SQL
 from zope.security.proxy import removeSecurityProxy
 
+from lp.services.database.interfaces import IStore
 from lp.services.scripts.base import LaunchpadScript
-from lp.translations.interfaces.translationmessage import (
-    ITranslationMessageSet,
-    )
+from lp.translations.model.translationmessage import TranslationMessage
 from lp.translations.utilities.validate import (
     GettextValidationError,
     validate_translation,
@@ -66,8 +65,11 @@ class GettextCheckMessages(LaunchpadScript):
         self.logger.debug(
             "Checking messages matching: %s" % self.options.where)
 
-        messages = getUtility(ITranslationMessageSet).selectDirect(
-            self.options.where, order_by=self.options.order_by)
+        messages = IStore(TranslationMessage).find(TranslationMessage)
+        if self.options.where is not None:
+            messages = messages.find(SQL(self.options.where))
+        if self.options.order_by is not None:
+            messages = messages.order_by(SQL(self.options.order_by))
         self._iterate(messages)
 
         self.logger.info("Done.")
diff --git a/lib/lp/translations/stories/importqueue/xx-translation-import-queue.txt b/lib/lp/translations/stories/importqueue/xx-translation-import-queue.txt
index 8e9f427..9c15385 100644
--- a/lib/lp/translations/stories/importqueue/xx-translation-import-queue.txt
+++ b/lib/lp/translations/stories/importqueue/xx-translation-import-queue.txt
@@ -294,12 +294,13 @@ Ubuntu uploads
 As a special case, the owners of Ubuntu's translation group are allowed
 to manage Ubuntu uploads.
 
-    >>> from lp.registry.model.distribution import Distribution
+    >>> from zope.component import getUtility
+    >>> from lp.registry.interfaces.distribution import IDistributionSet
     >>> from lp.translations.model.translationimportqueue import (
     ...     TranslationImportQueue)
     >>> login('admin@xxxxxxxxxxxxx')
     >>> queue = TranslationImportQueue()
-    >>> ubuntu = Distribution.selectOneBy(name='ubuntu')
+    >>> ubuntu = getUtility(IDistributionSet)['ubuntu']
     >>> hoary = ubuntu['hoary']
 
 There is a translation group for Ubuntu.  Its owner has no special
@@ -307,17 +308,16 @@ privileges or roles other than running the group.
 
 Somebody else has uploaded a translation template for an Ubuntu package.
 
-    >>> login(ANONYMOUS)
-
     >>> package = factory.makeSourcePackageName()
     >>> group_owner = factory.makePerson(
     ...     email='go@xxxxxxxxxxx', name='groupowner')
     >>> uploader = factory.makePerson()
     >>> ubuntu.translationgroup = factory.makeTranslationGroup(group_owner)
+
+    >>> login(ANONYMOUS)
     >>> ubuntu_upload = queue.addOrUpdateEntry(
     ...     'messages.pot', b'(content)', False, uploader,
     ...     sourcepackagename=package, distroseries=hoary)
-
     >>> logout()
 
 The owner of Ubuntu's translation group, despite not being the owner or
diff --git a/lib/lp/translations/stories/standalone/xx-potemplate-admin.txt b/lib/lp/translations/stories/standalone/xx-potemplate-admin.txt
index bf9852a..59a9600 100644
--- a/lib/lp/translations/stories/standalone/xx-potemplate-admin.txt
+++ b/lib/lp/translations/stories/standalone/xx-potemplate-admin.txt
@@ -186,10 +186,11 @@ Distribution templates
 Distributions get slightly wider permissions to manage their templates
 autonomously.
 
-    >>> from lp.registry.model.distribution import Distribution
+    >>> from zope.component import getUtility
+    >>> from lp.registry.interfaces.distribution import IDistributionSet
     >>> from lp.translations.model.potemplate import POTemplateSet
     >>> login('admin@xxxxxxxxxxxxx')
-    >>> ubuntu = Distribution.selectOneBy(name='ubuntu')
+    >>> ubuntu = getUtility(IDistributionSet)['ubuntu']
     >>> hoary = ubuntu['hoary']
     >>> templateset = POTemplateSet()
 
@@ -201,6 +202,7 @@ autonomously.
     >>> template = templatesubset.new(
     ...     'foo', 'foo', 'foo.pot', template_owner)
 
+    >>> login('admin@xxxxxxxxxxxxx')
     >>> distro_owner = factory.makePerson('do@xxxxxxxxxxx')
     >>> ubuntu.owner = distro_owner
 
diff --git a/lib/lp/translations/stories/standalone/xx-translation-access-display.txt b/lib/lp/translations/stories/standalone/xx-translation-access-display.txt
index d374966..adcc0bd 100644
--- a/lib/lp/translations/stories/standalone/xx-translation-access-display.txt
+++ b/lib/lp/translations/stories/standalone/xx-translation-access-display.txt
@@ -38,20 +38,25 @@ translation group of its own, that too is shown here.
 
     >>> import re
 
+    >>> from zope.component import getUtility
+    >>> from zope.security.proxy import removeSecurityProxy
+
     >>> from lp.services.database.constants import UTC_NOW
-    >>> from lp.registry.model.person import Person
-    >>> from lp.registry.model.product import Product
-    >>> from lp.services.worlddata.model.language import Language
+    >>> from lp.registry.interfaces.person import IPersonSet
+    >>> from lp.registry.interfaces.product import IProductSet
+    >>> from lp.services.worlddata.interfaces.language import ILanguageSet
     >>> from lp.translations.model.translationgroup import (
     ...     TranslationGroup)
 
-    >>> spanish = Language.selectOneBy(code='es')
-    >>> evolution = Product.selectOneBy(name='evolution')
-    >>> foobar = Person.selectOneBy(name='name16')
+    >>> login('admin@xxxxxxxxxxxxx')
+    >>> spanish = getUtility(ILanguageSet)['es']
+    >>> evolution = removeSecurityProxy(getUtility(IProductSet)['evolution'])
+    >>> foobar = getUtility(IPersonSet).getByName('name16')
     >>> gnomegroup = TranslationGroup(name='gnomegroup',
     ...     title="Gnome translation group", summary="Testing group",
     ...     datecreated=UTC_NOW, owner=foobar)
     >>> evolution.projectgroup.translationgroup = gnomegroup
+    >>> logout()
 
     >>> admin_browser.open(
     ...     'http://translations.launchpad.test/'