예제 #1
0
    def transformer_promoted(cls, versions):
        """Attach the promoted approvals to the versions."""
        if not versions:
            return

        PromotedApproval = versions[0].promoted_approvals.model

        ids = {v.id for v in versions}

        approvals = list(
            PromotedApproval.objects.filter(version_id__in=ids).values_list(
                'version_id', 'group_id', 'application_id', named=True))

        approval_dict = {
            version_id: list(groups)
            for version_id, groups in sorted_groupby(approvals, 'version_id')
        }
        for version in versions:
            v_id = version.id
            groups = [(
                PROMOTED_GROUPS_BY_ID.get(approval.group_id),
                APP_IDS.get(approval.application_id),
            ) for approval in approval_dict.get(v_id, [])
                      if approval.group_id in PROMOTED_GROUPS_BY_ID]
            version.approved_for_groups = groups
예제 #2
0
 def all_applications(self):
     application = APP_IDS.get(self.application_id)
     return [application] if application else [app for app in APP_USAGE]
예제 #3
0
 def application(self):
     return APP_IDS.get(self.application_id)
예제 #4
0
    def fake_object(self, data):
        """Create a fake instance of Addon and related models from ES data."""
        obj = Addon(id=data['id'], 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', [])
        ]

        # 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.RELEASE_CHANNEL_LISTED)
        obj._current_version_id = data_version.get('id')

        data_authors = data.get('listed_authors', [])
        obj.listed_authors = [
            UserProfile(
                id=data_author['id'],
                display_name=data_author['name'],
                username=data_author['username'],
                is_public=data_author.get('is_public', False),
            ) 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,
                                     model_class=preview_model_class)
            for preview_data in data.get('previews', [])
        ]

        promoted = data.get('promoted', None)
        if promoted:
            # set .approved_for_groups cached_property because it's used in
            # .approved_applications.
            approved_for_apps = promoted.get('approved_for_apps')
            obj.promoted = PromotedAddon(
                addon=obj,
                approved_application_ids=approved_for_apps,
                group_id=promoted['group_id'],
            )
            # we can safely regenerate these tuples because
            # .appproved_applications only cares about the current group
            obj._current_version.approved_for_groups = (
                (obj.promoted.group, APP_IDS.get(app_id))
                for app_id in approved_for_apps)
        else:
            obj.promoted = None

        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