def to_native(self, obj): # fake_app is a fake instance because we need to access a couple # properties and methods on Webapp. It should never hit the database. fake_app = Webapp(id=obj['id'], icon_type='image/png', modified=datetime.strptime(obj['modified'], '%Y-%m-%dT%H:%M:%S')) ESTranslationSerializerField.attach_translations(fake_app, obj, 'name') return { 'name': self.fields['name'].field_to_native(fake_app, 'name'), 'icon' : fake_app.get_icon_url(64), 'slug': obj['slug'], 'manifest_url': obj['manifest_url'], }
def to_native(self, obj): # fake_app is a fake instance because we need to access a couple # properties and methods on Webapp. It should never hit the database. fake_app = Webapp( id=obj['id'], icon_type='image/png', type=amo.ADDON_WEBAPP, default_locale=obj.get('default_locale', settings.LANGUAGE_CODE), icon_hash=obj.get('icon_hash'), modified=datetime.strptime(obj['modified'], '%Y-%m-%dT%H:%M:%S')) ESTranslationSerializerField.attach_translations(fake_app, obj, 'name') return { 'name': self.fields['name'].field_to_native(fake_app, 'name'), 'icon': fake_app.get_icon_url(64), 'slug': obj['slug'], 'manifest_url': obj['manifest_url'], }
def to_native(self, obj): # fake_app is a fake instance because we need to access a couple # properties and methods on Webapp. It should never hit the database. self.fake_app = Webapp( id=obj['id'], icon_type='image/png', default_locale=obj.get('default_locale', settings.LANGUAGE_CODE), icon_hash=obj.get('icon_hash'), modified=es_to_datetime(obj['modified'])) ESTranslationSerializerField.attach_translations( self.fake_app, obj, 'name') return { 'name': self.fields['name'].field_to_native(self.fake_app, 'name'), 'icon': self.fake_app.get_icon_url(64), 'slug': obj['slug'], 'manifest_url': obj['manifest_url'], }
def to_native(self, obj): # fake_app is a fake instance because we need to access a couple # properties and methods on Webapp. It should never hit the database. self.fake_app = Webapp( id=obj["id"], icon_type="image/png", default_locale=obj.get("default_locale", settings.LANGUAGE_CODE), icon_hash=obj.get("icon_hash"), modified=es_to_datetime(obj["modified"]), ) ESTranslationSerializerField.attach_translations(self.fake_app, obj, "name") return { "name": self.fields["name"].field_to_native(self.fake_app, "name"), "icon": self.fake_app.get_icon_url(64), "slug": obj["slug"], "manifest_url": obj["manifest_url"], }
def to_representation(self, obj): # fake_app is a fake instance because we need to access a couple # properties and methods on Webapp. It should never hit the database. self.fake_app = Webapp( id=obj['id'], icon_type='image/png', default_locale=obj.get('default_locale', settings.LANGUAGE_CODE), icon_hash=obj.get('icon_hash'), modified=es_to_datetime(obj['modified'])) ESTranslationSerializerField.attach_translations( self.fake_app, obj, 'name') return { 'name': self.fields['name'].to_representation( self.fields['name'].get_attribute(self.fake_app)), 'icon': self.fake_app.get_icon_url(64), 'slug': obj['slug'], 'manifest_url': obj['manifest_url'], }
def create_fake_app(self, data): """Create a fake instance of Webapp and related models from ES data.""" is_packaged = data['app_type'] != amo.ADDON_WEBAPP_HOSTED is_privileged = data['app_type'] == amo.ADDON_WEBAPP_PRIVILEGED obj = Webapp(id=data['id'], app_slug=data['app_slug'], is_packaged=is_packaged, type=amo.ADDON_WEBAPP, icon_type='image/png') # Set relations and attributes we need on those relations. # The properties set on latest_version and current_version differ # because we are only setting what the serializer is going to need. # In particular, latest_version.is_privileged needs to be set because # it's used by obj.app_type_id. obj.listed_authors = [] obj._current_version = Version() obj._current_version.addon = obj obj._current_version._developer_name = data['author'] obj._current_version.supported_locales = data['supported_locales'] obj._current_version.version = data['current_version'] obj._latest_version = Version() obj._latest_version.is_privileged = is_privileged obj._geodata = Geodata() obj.all_categories = [Category(slug=cat) for cat in data['category']] obj.all_previews = [Preview(id=p['id'], modified=p['modified'], filetype=p['filetype']) for p in data['previews']] obj._device_types = [DEVICE_TYPES[d] for d in data['device']] # Set base attributes on the "fake" app using the data from ES. # It doesn't mean they'll get exposed in the serializer output, that # depends on what the fields/exclude attributes in Meta. for field_name in ('created', 'modified', 'default_locale', 'icon_hash', 'is_escalated', 'is_offline', 'manifest_url', 'premium_type', 'regions', 'reviewed', 'status', 'weekly_downloads'): setattr(obj, field_name, data.get(field_name)) # Attach translations for all translated attributes. for field_name in ('name', 'description', 'homepage', 'support_email', 'support_url'): 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'] # Override obj.get_region() with a static list of regions generated # from the region_exclusions stored in ES. obj.get_regions = obj.get_regions(obj.get_region_ids(restofworld=True, excluded=data['region_exclusions'])) # Some methods below will need the raw data from ES, put it on obj. obj.es_data = data return obj
def fake_object(self, data): """Create a fake instance of Webapp and related models from ES data.""" is_packaged = data['app_type'] != mkt.ADDON_WEBAPP_HOSTED is_privileged = data['app_type'] == mkt.ADDON_WEBAPP_PRIVILEGED obj = Webapp(id=data['id'], app_slug=data['app_slug'], is_packaged=is_packaged, icon_type='image/png') # Set relations and attributes we need on those relations. # The properties set on latest_version and current_version differ # because we are only setting what the serializer is going to need. # In particular, latest_version.is_privileged needs to be set because # it's used by obj.app_type_id. obj.listed_authors = [] obj._current_version = Version() obj._current_version.addon = obj obj._current_version._developer_name = data['author'] obj._current_version.supported_locales = data['supported_locales'] obj._current_version.version = data['current_version'] obj._latest_version = Version() obj._latest_version.is_privileged = is_privileged obj._geodata = Geodata() obj.all_previews = [ Preview(id=p['id'], modified=self.to_datetime(p['modified']), filetype=p['filetype'], sizes=p.get('sizes', {})) for p in data['previews'] ] obj.categories = data['category'] obj.tags_list = data['tags'] obj._device_types = [DEVICE_TYPES[d] for d in data['device']] obj._is_disabled = data['is_disabled'] # Set base attributes on the "fake" app using the data from ES. self._attach_fields( obj, data, ('created', 'default_locale', 'guid', 'icon_hash', 'is_escalated', 'is_offline', 'last_updated', 'hosted_url', 'manifest_url', 'modified', 'premium_type', 'promo_img_hash', 'regions', 'reviewed', 'status')) # Attach translations for all translated attributes. self._attach_translations(obj, data, ('name', 'description', 'homepage', 'support_email', 'support_url')) if data.get('group_translations'): self._attach_translations(obj, data, ('group', )) # Feed group. else: obj.group_translations = None # Release notes target and source name differ (ES stores it as # release_notes but the db field we are emulating is called # releasenotes without the "_"). 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'] # Override obj.get_excluded_region_ids() to just return the list of # regions stored in ES instead of making SQL queries. obj.get_excluded_region_ids = lambda: data['region_exclusions'] # Set up payments stuff to avoid extra queries later (we'll still make # some, because price info is not in ES). if obj.is_premium(): Webapp.attach_premiums([obj]) # Some methods below will need the raw data from ES, put it on obj. obj.es_data = data return obj
def fake_object(self, data): """Create a fake instance of Webapp and related models from ES data.""" is_packaged = data['app_type'] != mkt.ADDON_WEBAPP_HOSTED is_privileged = data['app_type'] == mkt.ADDON_WEBAPP_PRIVILEGED obj = Webapp(id=data['id'], app_slug=data['app_slug'], is_packaged=is_packaged, icon_type='image/png') # Set relations and attributes we need on those relations. # The properties set on latest_version and current_version differ # because we are only setting what the serializer is going to need. # In particular, latest_version.is_privileged needs to be set because # it's used by obj.app_type_id. obj.listed_authors = [] obj._current_version = Version() obj._current_version.addon = obj obj._current_version._developer_name = data['author'] obj._current_version.supported_locales = data['supported_locales'] obj._current_version.version = data['current_version'] obj._latest_version = Version() obj._latest_version.is_privileged = is_privileged obj._geodata = Geodata() obj.all_previews = [ Preview(id=p['id'], modified=self.to_datetime(p['modified']), filetype=p['filetype'], sizes=p.get('sizes', {})) for p in data['previews']] obj.categories = data['category'] obj.tags_list = data['tags'] obj._device_types = [DEVICE_TYPES[d] for d in data['device']] obj._is_disabled = data['is_disabled'] # Set base attributes on the "fake" app using the data from ES. self._attach_fields( obj, data, ('created', 'default_locale', 'guid', 'icon_hash', 'is_escalated', 'is_offline', 'last_updated', 'manifest_url', 'modified', 'premium_type', 'regions', 'reviewed', 'status')) # Attach translations for all translated attributes. self._attach_translations( obj, data, ('name', 'description', 'homepage', 'support_email', 'support_url')) if data.get('group_translations'): self._attach_translations(obj, data, ('group',)) # Feed group. else: obj.group_translations = None self._attach_translations(obj._geodata, data, ('banner_message',)) # Release notes target and source name differ (ES stores it as # release_notes but the db field we are emulating is called # releasenotes without the "_"). 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'] # Override obj.get_excluded_region_ids() to just return the list of # regions stored in ES instead of making SQL queries. obj.get_excluded_region_ids = lambda: data['region_exclusions'] # Set up payments stuff to avoid extra queries later (we'll still make # some, because price info is not in ES). if obj.is_premium(): Webapp.attach_premiums([obj]) # Some methods below will need the raw data from ES, put it on obj. obj.es_data = data return obj
def _attach_translations(self, obj, data, field_names): """Deserialize ES translation fields.""" for field_name in field_names: ESTranslationSerializerField.attach_translations( obj, data, field_name) return obj
def fake_object(self, data): """Create a fake instance of Webapp and related models from ES data.""" is_packaged = data["app_type"] != mkt.ADDON_WEBAPP_HOSTED is_privileged = data["app_type"] == mkt.ADDON_WEBAPP_PRIVILEGED obj = Webapp(id=data["id"], app_slug=data["app_slug"], is_packaged=is_packaged, icon_type="image/png") # Set relations and attributes we need on those relations. # The properties set on latest_version and current_version differ # because we are only setting what the serializer is going to need. # In particular, latest_version.is_privileged needs to be set because # it's used by obj.app_type_id. obj.listed_authors = [] obj._current_version = Version() obj._current_version.addon = obj obj._current_version._developer_name = data["author"] obj._current_version.supported_locales = data["supported_locales"] obj._current_version.version = data["current_version"] obj._latest_version = Version() obj._latest_version.is_privileged = is_privileged obj._geodata = Geodata() obj.all_previews = [ Preview( id=p["id"], modified=self.to_datetime(p["modified"]), filetype=p["filetype"], sizes=p.get("sizes", {}) ) for p in data["previews"] ] obj.categories = data["category"] obj._device_types = [DEVICE_TYPES[d] for d in data["device"]] obj._is_disabled = data["is_disabled"] # Set base attributes on the "fake" app using the data from ES. self._attach_fields( obj, data, ( "created", "default_locale", "icon_hash", "is_escalated", "is_offline", "last_updated", "manifest_url", "modified", "premium_type", "regions", "reviewed", "status", ), ) # Attach translations for all translated attributes. self._attach_translations(obj, data, ("name", "description", "homepage", "support_email", "support_url")) if data.get("group_translations"): self._attach_translations(obj, data, ("group",)) # Feed group. else: obj.group_translations = None self._attach_translations(obj._geodata, data, ("banner_message",)) # Release notes target and source name differ (ES stores it as # release_notes but the db field we are emulating is called # releasenotes without the "_"). 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"] # Override obj.get_region() with a static list of regions generated # from the region_exclusions stored in ES. obj.get_regions = obj.get_regions(obj.get_region_ids(restofworld=True, excluded=data["region_exclusions"])) # Some methods below will need the raw data from ES, put it on obj. obj.es_data = data return obj
def create_fake_app(self, data): """Create a fake instance of Webapp and related models from ES data.""" is_packaged = data['app_type'] != amo.ADDON_WEBAPP_HOSTED is_privileged = data['app_type'] == amo.ADDON_WEBAPP_PRIVILEGED obj = Webapp(id=data['id'], app_slug=data['app_slug'], is_packaged=is_packaged, type=amo.ADDON_WEBAPP, icon_type='image/png') # Set relations and attributes we need on those relations. # The properties set on latest_version and current_version differ # because we are only setting what the serializer is going to need. # In particular, latest_version.is_privileged needs to be set because # it's used by obj.app_type_id. obj.listed_authors = [] obj._current_version = Version() obj._current_version.addon = obj obj._current_version._developer_name = data['author'] obj._current_version.supported_locales = data['supported_locales'] obj._current_version.version = data['current_version'] obj._latest_version = Version() obj._latest_version.is_privileged = is_privileged obj._geodata = Geodata() obj.all_categories = [Category(slug=cat) for cat in data['category']] obj.all_previews = [Preview(id=p['id'], modified=p['modified'], filetype=p['filetype']) for p in data['previews']] # Apps indexed under the old code don't have the 'platforms' or # 'form_factor' fields, so fall back to the 'device' field translating # the device to the new platforms and form_factors. if data.get('platforms'): obj._platforms = [mkt.PLATFORM_TYPES[p] for p in data['platforms']] else: # Use the old device type field. # TODO: Remove when we no longer support API v1. platforms = [] for d in data['device']: platform = mkt.DEVICE_TO_PLATFORM.get(d) if platform: platforms.append(platform) obj._platforms = platforms if data.get('form_factors'): obj._form_factors = [mkt.FORM_FACTOR_CHOICES[ff] for ff in data['form_factors']] else: # Use the old device type field. # TODO: Remove when we no longer support API v1. form_factors = [] for d in data['device']: ff = mkt.DEVICE_TO_FORM_FACTOR.get(d) if ff: form_factors.append(ff) obj._form_factors = form_factors # TODO: Remove when we no longer support API v1. if data.get('devices'): obj._device_types = [amo.DEVICE_TYPES[d] for d in data['device']] # Set base attributes on the "fake" app using the data from ES. # It doesn't mean they'll get exposed in the serializer output, that # depends on what the fields/exclude attributes in Meta. for field_name in ('created', 'modified', 'default_locale', 'is_escalated', 'is_offline', 'manifest_url', 'premium_type', 'regions', 'reviewed', 'status', 'weekly_downloads'): setattr(obj, field_name, data.get(field_name)) # Attach translations for all translated attributes. for field_name in ('name', 'description', 'homepage', 'support_email', 'support_url'): 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'] # Avoid a query for payment_account if the app is not premium. if not obj.is_premium(): obj.payment_account = None # Override obj.get_region() with a static list of regions generated # from the region_exclusions stored in ES. obj.get_regions = obj.get_regions(obj.get_region_ids(restofworld=True, excluded=data['region_exclusions'])) # Some methods below will need the raw data from ES, put it on obj. obj.es_data = data return obj