Merge ~cjwatson/launchpad:remove-language-bycode into launchpad:master

Proposed by Colin Watson
Status: Merged
Approved by: Colin Watson
Approved revision: 320ab8b04ba6c969096faa5324dbd2c5916e05cb
Merge reported by: Otto Co-Pilot
Merged at revision: not available
Proposed branch: ~cjwatson/launchpad:remove-language-bycode
Merge into: launchpad:master
Diff against target: 170 lines (+26/-27)
4 files modified
lib/lp/services/worlddata/model/language.py (+6/-10)
lib/lp/services/worlddata/vocabularies.py (+7/-5)
lib/lp/translations/model/potemplate.py (+4/-3)
lib/lp/translations/tests/test_autoapproval.py (+9/-9)
Reviewer Review Type Date Requested Status
Thiago F. Pappacena (community) Approve
Review via email: mp+390844@code.launchpad.net

Commit message

Remove Language.byCode

Description of the change

It's better to go through the ILanguageSet interface.

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

LGTM

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/lib/lp/services/worlddata/model/language.py b/lib/lp/services/worlddata/model/language.py
2index 9cb3c36..3907187 100644
3--- a/lib/lp/services/worlddata/model/language.py
4+++ b/lib/lp/services/worlddata/model/language.py
5@@ -61,8 +61,7 @@ class Language(SQLBase):
6
7 _table = 'Language'
8
9- code = StringCol(
10- dbName='code', notNull=True, unique=True, alternateID=True)
11+ code = StringCol(dbName='code', notNull=True, unique=True)
12 uuid = StringCol(dbName='uuid', notNull=False, default=None)
13 nativename = StringCol(dbName='nativename')
14 englishname = StringCol(dbName='englishname')
15@@ -127,13 +126,13 @@ class Language(SQLBase):
16 if self.code == 'pt_BR':
17 return None
18 elif self.code == 'nn':
19- return Language.byCode('nb')
20+ return IStore(Language).find(Language, code='nb').one()
21 elif self.code == 'nb':
22- return Language.byCode('nn')
23+ return IStore(Language).find(Language, code='nn').one()
24 codes = self.code.split('_')
25 if len(codes) == 2 and codes[0] != 'en':
26- language = Language.byCode(codes[0])
27- if language.visible == True:
28+ language = IStore(Language).find(Language, code=codes[0]).one()
29+ if language.visible:
30 return language
31 else:
32 return None
33@@ -272,10 +271,7 @@ class LanguageSet:
34 """See `ILanguageSet`."""
35 assert isinstance(code, six.string_types), (
36 "%s is not a valid type for 'code'" % type(code))
37- try:
38- return Language.byCode(code)
39- except SQLObjectNotFound:
40- return None
41+ return IStore(Language).find(Language, code=code).one()
42
43 def keys(self):
44 """See `ILanguageSet`."""
45diff --git a/lib/lp/services/worlddata/vocabularies.py b/lib/lp/services/worlddata/vocabularies.py
46index be963f4..6c96021 100644
47--- a/lib/lp/services/worlddata/vocabularies.py
48+++ b/lib/lp/services/worlddata/vocabularies.py
49@@ -13,7 +13,7 @@ __metaclass__ = type
50
51 import pytz
52 import six
53-from sqlobject import SQLObjectNotFound
54+from zope.component import getUtility
55 from zope.interface import alsoProvides
56 from zope.schema.vocabulary import (
57 SimpleTerm,
58@@ -21,7 +21,10 @@ from zope.schema.vocabulary import (
59 )
60
61 from lp.services.webapp.vocabulary import SQLObjectVocabularyBase
62-from lp.services.worlddata.interfaces.language import ILanguage
63+from lp.services.worlddata.interfaces.language import (
64+ ILanguage,
65+ ILanguageSet,
66+ )
67 from lp.services.worlddata.interfaces.timezone import ITimezoneNameVocabulary
68 from lp.services.worlddata.model.country import Country
69 from lp.services.worlddata.model.language import Language
70@@ -78,8 +81,7 @@ class LanguageVocabulary(SQLObjectVocabularyBase):
71
72 def getTermByToken(self, token):
73 """See `IVocabulary`."""
74- try:
75- found_language = Language.byCode(token)
76- except SQLObjectNotFound:
77+ found_language = getUtility(ILanguageSet).getLanguageByCode(token)
78+ if found_language is None:
79 raise LookupError(token)
80 return self.getTerm(found_language)
81diff --git a/lib/lp/translations/model/potemplate.py b/lib/lp/translations/model/potemplate.py
82index 9a622a0..06bf24f 100644
83--- a/lib/lp/translations/model/potemplate.py
84+++ b/lib/lp/translations/model/potemplate.py
85@@ -68,6 +68,7 @@ from lp.services.database.sqlbase import (
86 from lp.services.helpers import shortlist
87 from lp.services.mail.helpers import get_email_template
88 from lp.services.propertycache import cachedproperty
89+from lp.services.worlddata.interfaces.language import ILanguageSet
90 from lp.services.worlddata.model.language import Language
91 from lp.translations.enums import RosettaImportStatus
92 from lp.translations.interfaces.pofile import IPOFileSet
93@@ -699,10 +700,10 @@ class POTemplate(SQLBase, RosettaStats):
94
95 def _lookupLanguage(self, language_code):
96 """Look up named `Language` object, or raise `LanguageNotFound`."""
97- try:
98- return Language.byCode(language_code)
99- except SQLObjectNotFound:
100+ language = getUtility(ILanguageSet).getLanguageByCode(language_code)
101+ if language is None:
102 raise LanguageNotFound(language_code)
103+ return language
104
105 def isPOFilePathAvailable(self, path):
106 """Can we assign given path to a new `POFile` without clashes?
107diff --git a/lib/lp/translations/tests/test_autoapproval.py b/lib/lp/translations/tests/test_autoapproval.py
108index bae2319..c7a9665 100644
109--- a/lib/lp/translations/tests/test_autoapproval.py
110+++ b/lib/lp/translations/tests/test_autoapproval.py
111@@ -31,10 +31,7 @@ from lp.registry.model.sourcepackagename import (
112 SourcePackageNameSet,
113 )
114 from lp.services.database.interfaces import IMasterStore
115-from lp.services.worlddata.model.language import (
116- Language,
117- LanguageSet,
118- )
119+from lp.services.worlddata.interfaces.language import ILanguageSet
120 from lp.testing import (
121 TestCaseWithFactory,
122 verifyObject,
123@@ -96,7 +93,7 @@ class TestCustomLanguageCode(TestCaseWithFactory):
124 # Map "pt_PT" to "pt."
125 self.product_codes['pt_PT'] = CustomLanguageCode(
126 translation_target=self.product, language_code='pt_PT',
127- language=Language.byCode('pt'))
128+ language=getUtility(ILanguageSet).getLanguageByCode('pt'))
129
130 self.distro = Distribution.byName('ubuntu')
131 self.sourcepackagename = SourcePackageName.byName('evolution')
132@@ -104,7 +101,7 @@ class TestCustomLanguageCode(TestCaseWithFactory):
133 translation_target=self.distro.getSourcePackage(
134 self.sourcepackagename),
135 language_code='Brazilian',
136- language=Language.byCode('pt_BR'))
137+ language=getUtility(ILanguageSet).getLanguageByCode('pt_BR'))
138
139 def test_ICustomLanguageCode(self):
140 # Does CustomLanguageCode conform to ICustomLanguageCode?
141@@ -159,7 +156,9 @@ class TestCustomLanguageCode(TestCaseWithFactory):
142 self.assertEqual(
143 Brazilian_code.sourcepackagename, self.sourcepackagename)
144 self.assertEqual(Brazilian_code.language_code, 'Brazilian')
145- self.assertEqual(Brazilian_code.language, Language.byCode('pt_BR'))
146+ self.assertEqual(
147+ Brazilian_code.language,
148+ getUtility(ILanguageSet).getLanguageByCode('pt_BR'))
149
150
151 class TestGuessPOFileCustomLanguageCode(TestCaseWithFactory,
152@@ -200,7 +199,8 @@ class TestGuessPOFileCustomLanguageCode(TestCaseWithFactory,
153 if target_language_code is None:
154 language = None
155 else:
156- language = Language.byCode(target_language_code)
157+ language = getUtility(ILanguageSet).getLanguageByCode(
158+ target_language_code)
159 customcode = CustomLanguageCode(
160 translation_target=self.product, language_code=language_code,
161 language=language)
162@@ -1099,7 +1099,7 @@ class TestAutoApprovalNewPOFile(TestCaseWithFactory, GardenerDbUserMixin):
163 super(TestAutoApprovalNewPOFile, self).setUp()
164 self.product = self.factory.makeProduct()
165 self.queue = TranslationImportQueue()
166- self.language = LanguageSet().getLanguageByCode('nl')
167+ self.language = getUtility(ILanguageSet).getLanguageByCode('nl')
168
169 def _makeTemplate(self, series):
170 """Create a template."""

Subscribers

People subscribed via source and target branches

to status/vote changes: