launchpad-reviewers team mailing list archive
-
launchpad-reviewers team
-
Mailing list archive
-
Message #25299
[Merge] ~cjwatson/launchpad:remove-language-bycode into launchpad:master
Colin Watson has proposed merging ~cjwatson/launchpad:remove-language-bycode into launchpad:master.
Commit message:
Remove Language.byCode
Requested reviews:
Launchpad code reviewers (launchpad-reviewers)
For more details, see:
https://code.launchpad.net/~cjwatson/launchpad/+git/launchpad/+merge/390844
It's better to go through the ILanguageSet interface.
--
Your team Launchpad code reviewers is requested to review the proposed merge of ~cjwatson/launchpad:remove-language-bycode into launchpad:master.
diff --git a/lib/lp/services/worlddata/model/language.py b/lib/lp/services/worlddata/model/language.py
index 9cb3c36..3907187 100644
--- a/lib/lp/services/worlddata/model/language.py
+++ b/lib/lp/services/worlddata/model/language.py
@@ -61,8 +61,7 @@ class Language(SQLBase):
_table = 'Language'
- code = StringCol(
- dbName='code', notNull=True, unique=True, alternateID=True)
+ code = StringCol(dbName='code', notNull=True, unique=True)
uuid = StringCol(dbName='uuid', notNull=False, default=None)
nativename = StringCol(dbName='nativename')
englishname = StringCol(dbName='englishname')
@@ -127,13 +126,13 @@ class Language(SQLBase):
if self.code == 'pt_BR':
return None
elif self.code == 'nn':
- return Language.byCode('nb')
+ return IStore(Language).find(Language, code='nb').one()
elif self.code == 'nb':
- return Language.byCode('nn')
+ return IStore(Language).find(Language, code='nn').one()
codes = self.code.split('_')
if len(codes) == 2 and codes[0] != 'en':
- language = Language.byCode(codes[0])
- if language.visible == True:
+ language = IStore(Language).find(Language, code=codes[0]).one()
+ if language.visible:
return language
else:
return None
@@ -272,10 +271,7 @@ class LanguageSet:
"""See `ILanguageSet`."""
assert isinstance(code, six.string_types), (
"%s is not a valid type for 'code'" % type(code))
- try:
- return Language.byCode(code)
- except SQLObjectNotFound:
- return None
+ return IStore(Language).find(Language, code=code).one()
def keys(self):
"""See `ILanguageSet`."""
diff --git a/lib/lp/services/worlddata/vocabularies.py b/lib/lp/services/worlddata/vocabularies.py
index be963f4..6c96021 100644
--- a/lib/lp/services/worlddata/vocabularies.py
+++ b/lib/lp/services/worlddata/vocabularies.py
@@ -13,7 +13,7 @@ __metaclass__ = type
import pytz
import six
-from sqlobject import SQLObjectNotFound
+from zope.component import getUtility
from zope.interface import alsoProvides
from zope.schema.vocabulary import (
SimpleTerm,
@@ -21,7 +21,10 @@ from zope.schema.vocabulary import (
)
from lp.services.webapp.vocabulary import SQLObjectVocabularyBase
-from lp.services.worlddata.interfaces.language import ILanguage
+from lp.services.worlddata.interfaces.language import (
+ ILanguage,
+ ILanguageSet,
+ )
from lp.services.worlddata.interfaces.timezone import ITimezoneNameVocabulary
from lp.services.worlddata.model.country import Country
from lp.services.worlddata.model.language import Language
@@ -78,8 +81,7 @@ class LanguageVocabulary(SQLObjectVocabularyBase):
def getTermByToken(self, token):
"""See `IVocabulary`."""
- try:
- found_language = Language.byCode(token)
- except SQLObjectNotFound:
+ found_language = getUtility(ILanguageSet).getLanguageByCode(token)
+ if found_language is None:
raise LookupError(token)
return self.getTerm(found_language)
diff --git a/lib/lp/translations/model/potemplate.py b/lib/lp/translations/model/potemplate.py
index 9a622a0..06bf24f 100644
--- a/lib/lp/translations/model/potemplate.py
+++ b/lib/lp/translations/model/potemplate.py
@@ -68,6 +68,7 @@ from lp.services.database.sqlbase import (
from lp.services.helpers import shortlist
from lp.services.mail.helpers import get_email_template
from lp.services.propertycache import cachedproperty
+from lp.services.worlddata.interfaces.language import ILanguageSet
from lp.services.worlddata.model.language import Language
from lp.translations.enums import RosettaImportStatus
from lp.translations.interfaces.pofile import IPOFileSet
@@ -699,10 +700,10 @@ class POTemplate(SQLBase, RosettaStats):
def _lookupLanguage(self, language_code):
"""Look up named `Language` object, or raise `LanguageNotFound`."""
- try:
- return Language.byCode(language_code)
- except SQLObjectNotFound:
+ language = getUtility(ILanguageSet).getLanguageByCode(language_code)
+ if language is None:
raise LanguageNotFound(language_code)
+ return language
def isPOFilePathAvailable(self, path):
"""Can we assign given path to a new `POFile` without clashes?
diff --git a/lib/lp/translations/tests/test_autoapproval.py b/lib/lp/translations/tests/test_autoapproval.py
index bae2319..c7a9665 100644
--- a/lib/lp/translations/tests/test_autoapproval.py
+++ b/lib/lp/translations/tests/test_autoapproval.py
@@ -31,10 +31,7 @@ from lp.registry.model.sourcepackagename import (
SourcePackageNameSet,
)
from lp.services.database.interfaces import IMasterStore
-from lp.services.worlddata.model.language import (
- Language,
- LanguageSet,
- )
+from lp.services.worlddata.interfaces.language import ILanguageSet
from lp.testing import (
TestCaseWithFactory,
verifyObject,
@@ -96,7 +93,7 @@ class TestCustomLanguageCode(TestCaseWithFactory):
# Map "pt_PT" to "pt."
self.product_codes['pt_PT'] = CustomLanguageCode(
translation_target=self.product, language_code='pt_PT',
- language=Language.byCode('pt'))
+ language=getUtility(ILanguageSet).getLanguageByCode('pt'))
self.distro = Distribution.byName('ubuntu')
self.sourcepackagename = SourcePackageName.byName('evolution')
@@ -104,7 +101,7 @@ class TestCustomLanguageCode(TestCaseWithFactory):
translation_target=self.distro.getSourcePackage(
self.sourcepackagename),
language_code='Brazilian',
- language=Language.byCode('pt_BR'))
+ language=getUtility(ILanguageSet).getLanguageByCode('pt_BR'))
def test_ICustomLanguageCode(self):
# Does CustomLanguageCode conform to ICustomLanguageCode?
@@ -159,7 +156,9 @@ class TestCustomLanguageCode(TestCaseWithFactory):
self.assertEqual(
Brazilian_code.sourcepackagename, self.sourcepackagename)
self.assertEqual(Brazilian_code.language_code, 'Brazilian')
- self.assertEqual(Brazilian_code.language, Language.byCode('pt_BR'))
+ self.assertEqual(
+ Brazilian_code.language,
+ getUtility(ILanguageSet).getLanguageByCode('pt_BR'))
class TestGuessPOFileCustomLanguageCode(TestCaseWithFactory,
@@ -200,7 +199,8 @@ class TestGuessPOFileCustomLanguageCode(TestCaseWithFactory,
if target_language_code is None:
language = None
else:
- language = Language.byCode(target_language_code)
+ language = getUtility(ILanguageSet).getLanguageByCode(
+ target_language_code)
customcode = CustomLanguageCode(
translation_target=self.product, language_code=language_code,
language=language)
@@ -1099,7 +1099,7 @@ class TestAutoApprovalNewPOFile(TestCaseWithFactory, GardenerDbUserMixin):
super(TestAutoApprovalNewPOFile, self).setUp()
self.product = self.factory.makeProduct()
self.queue = TranslationImportQueue()
- self.language = LanguageSet().getLanguageByCode('nl')
+ self.language = getUtility(ILanguageSet).getLanguageByCode('nl')
def _makeTemplate(self, series):
"""Create a template."""