in src/olympia/addons/serializers.py [0:0]
def fake_object(self, data):
"""Create a fake instance of Addon and related models from ES data."""
obj = Addon(id=data['id'], created=None, slug=data['slug'])
# Attach base attributes that have the same name/format in ES and in
# the model.
self._attach_fields(
obj,
data,
(
'average_daily_users',
'bayesian_rating',
'contributions',
'created',
'default_locale',
'guid',
'has_eula',
'has_privacy_policy',
'hotness',
'icon_hash',
'icon_type',
'is_experimental',
'last_updated',
'modified',
'requires_payment',
'slug',
'status',
'type',
'weekly_downloads',
),
)
# Attach attributes that do not have the same name/format in ES.
obj.tag_list = data.get('tags', [])
obj.all_categories = [
CATEGORIES_BY_ID[cat_id]
for cat_id in data.get('category', [])
if cat_id in CATEGORIES_BY_ID
]
# Not entirely accurate, but enough in the context of the search API.
obj.disabled_by_user = data.get('is_disabled', False)
# Attach translations (they require special treatment).
self._attach_translations(obj, data, self.translated_fields)
# Attach related models (also faking them). `current_version` is a
# property we can't write to, so we use the underlying field which
# begins with an underscore.
data_version = data.get('current_version') or {}
obj._current_version = self.fake_version_object(
obj, data_version, amo.CHANNEL_LISTED
)
obj._current_version_id = data_version.get('id')
data_authors = data.get('listed_authors', [])
obj.listed_authors = [
UserProfile(
auth_id=None,
id=data_author['id'],
created=None,
display_name=data_author['name'],
username=data_author['username'],
)
for data_author in data_authors
]
is_static_theme = data.get('type') == amo.ADDON_STATICTHEME
preview_model_class = VersionPreview if is_static_theme else Preview
obj.current_previews = [
self.fake_preview_object(
obj, preview_data, idx, model_class=preview_model_class
)
for idx, preview_data in enumerate(data.get('previews', []))
]
promoted = data.get('promoted', None)
if promoted:
promoted_list = promoted if isinstance(promoted, list) else [promoted]
obj.promoted = []
approved_for_groups = []
for promotion in promoted_list:
# set .approved_for_groups cached_property because it's used in
# .approved_applications.
approved_for_apps = promotion.get('approved_for_apps')
group = PROMOTED_GROUPS_BY_ID[promotion['group_id']]
obj.promoted.append(
{
'group_id': group.id,
'category': group.api_name,
'apps': [
APP_IDS.get(app_id).short for app_id in approved_for_apps
],
}
)
approved_for_groups.extend(
(group, APP_IDS.get(app_id)) for app_id in approved_for_apps
)
# we can safely regenerate these tuples because
# .appproved_applications only cares about the current group
obj._current_version.approved_for_groups = tuple(approved_for_groups)
else:
obj.promoted = []
ratings = data.get('ratings', {})
obj.average_rating = ratings.get('average')
obj.total_ratings = ratings.get('count')
obj.text_ratings_count = ratings.get('text_count')
return obj