Skip to content

Commit

Permalink
Fix release_notes in ESAppSerializer (bug 945820)
Browse files Browse the repository at this point in the history
  • Loading branch information
diox committed Jan 27, 2014
1 parent 9eba05d commit f9eacd0
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 13 deletions.
16 changes: 10 additions & 6 deletions mkt/api/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,22 +144,26 @@ def __init__(self, *args, **kwargs):
super(ESTranslationSerializerField, self).__init__(*args, **kwargs)

@classmethod
def attach_translations(cls, obj, field_name, data):
def attach_translations(cls, obj, data, source_name, target_name=None):
"""
Look for the translation of `field_name` in `data` and create a dict
Look for the translation of `source_name` in `data` and create a dict
with all translations for this field (which will look like
{'en-US': 'mytranslation'}) and attach it to a property on `obj`.
The property name is built with `field_name` and `cls.suffix`.
The property name is built with `target_name` and `cls.suffix`. If
`target_name` is None, `source_name` is used instead.
The suffix is necessary for two reasons:
1) The translations app won't let us set the dict on the real field
without making db queries
2) This also exactly matches how we store translations in ES, so we can
directly fetch the translations in the data passed to this method.
"""
key = '%s%s' % (field_name, cls.suffix)
setattr(obj, key, dict((v.get('lang', ''), v.get('string', ''))
for v in data.get(key, {})))
if target_name is None:
target_name = source_name
target_key = '%s%s' % (target_name, cls.suffix)
source_key = '%s%s' % (source_name, cls.suffix)
setattr(obj, target_key, dict((v.get('lang', ''), v.get('string', ''))
for v in data.get(source_key, {})))

def fetch_all_translations(self, obj, source, field):
return field or None
Expand Down
18 changes: 17 additions & 1 deletion mkt/api/tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,26 @@ def test_attach_translations(self):
}]
}
self.app = Webapp()
self.field_class().attach_translations(self.app, 'foo', data)
self.field_class().attach_translations(self.app, data, 'foo')
eq_(self.app.foo_translations, {'testlang': 'teststring',
'testlang2': 'teststring2'})

def test_attach_translations_target_name(self):
data = {
'foo_translations' : [{
'lang': 'testlang',
'string': 'teststring'
}, {
'lang': 'testlang2',
'string': 'teststring2'
}]
}
self.app = Webapp()
self.field_class().attach_translations(self.app, data, 'bar',
target_name='foo')
eq_(self.app.bar_translations, {'testlang': 'teststring',
'testlang2': 'teststring2'})

def _test_expected_dict(self, field):
result = field.field_to_native(self.app, 'name')
expected = self.app.name_translations
Expand Down
12 changes: 6 additions & 6 deletions mkt/search/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,12 @@ def create_fake_app(self, data):
# Attach translations for all translated attributes.
for field_name in ('name', 'description', 'homepage', 'support_email',
'support_url'):
ESTranslationSerializerField.attach_translations(
obj, field_name, data)
ESTranslationSerializerField.attach_translations(
obj._geodata, 'banner_message', data)
ESTranslationSerializerField.attach_translations(
obj._current_version, 'releasenotes', data)
ESTranslationSerializerField.attach_translations(obj,
data, field_name)
ESTranslationSerializerField.attach_translations(obj._geodata,
data, 'banner_message')
ESTranslationSerializerField.attach_translations(obj._current_version,
data, 'release_notes', target_name='releasenotes')

# Set attributes that have a different name in ES.
obj.public_stats = data['has_public_stats']
Expand Down
16 changes: 16 additions & 0 deletions mkt/webapps/tests/test_utils_.py
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,22 @@ def test_payment_account(self):
eq_(res['payment_account'], reverse('payment-account-detail',
kwargs={'pk': addon_payment_account.pk}))

def test_release_notes(self):
res = self.serialize()
eq_(res['release_notes'], None)
version = self.app.current_version
version.releasenotes = u'These are nötes.'
version.save()
self.app.save()
self.refresh('webapp')
res = self.serialize()
eq_(res['release_notes'], {u'en-US': unicode(version.releasenotes)})

self.request = RequestFactory().get('/?lang=whatever')
self.request.REGION = mkt.regions.US
res = self.serialize()
eq_(res['release_notes'], unicode(version.releasenotes))

def test_upsell(self):
upsell = amo.tests.app_factory()
self.make_premium(upsell)
Expand Down

0 comments on commit f9eacd0

Please sign in to comment.