Esempio n. 1
0
    def filter_queryset(self, request, queryset, view):

        device_id = get_device_id(request)
        if device_id:
            queryset = queryset.filter(Bool(must=[F("term", device=device_id)]))

        return queryset
Esempio n. 2
0
    def filter_queryset(self, request, queryset, view):

        device_id = get_device_id(request)
        if device_id:
            queryset = queryset.filter(
                Bool(must=[F('term', device=device_id)]))

        return queryset
Esempio n. 3
0
    def filter_queryset(self, request, queryset, view):

        device_id = get_device_id(request)
        data = {
            "gaia": getattr(request, "GAIA", False),
            "mobile": getattr(request, "MOBILE", False),
            "tablet": getattr(request, "TABLET", False),
        }
        flash_incompatible = data["mobile"] or data["gaia"]

        if device_id:
            queryset = queryset.filter(Bool(must=[F("term", device=device_id)]))
        if flash_incompatible:
            queryset = queryset.filter(Bool(must_not=[F("term", uses_flash=True)]))

        return queryset
Esempio n. 4
0
    def list(self, request, *args, **kwargs):
        if (not settings.RECOMMENDATIONS_ENABLED
                or not settings.RECOMMENDATIONS_API_URL
                or not self.request.user.is_authenticated()):
            return self._popular()
        else:
            app_ids = []
            url = '{base_url}/api/v2/recommend/{limit}/{user_hash}/'.format(
                base_url=settings.RECOMMENDATIONS_API_URL,
                limit=20,
                user_hash=self.request.user.recommendation_hash)

            try:
                with statsd.timer('recommendation.get'):
                    resp = requests.get(
                        url, timeout=settings.RECOMMENDATIONS_API_TIMEOUT)
                if resp.status_code == 200:
                    app_ids = resp.json()['recommendations']
            except Timeout as e:
                log.warning(u'Recommendation timeout: {error}'.format(error=e))
            except RequestException as e:
                # On recommendation API exceptions we return popular.
                log.error(u'Recommendation exception: {error}'.format(error=e))

            if not app_ids:
                # Fall back to a popularity search.
                return self._popular()

            # Get list of installed apps and remove from app_ids.
            installed = list(
                request.user.installed_set.values_list('addon_id', flat=True))
            app_ids = filter(lambda a: a not in installed, app_ids)

            device = get_device_id(request)
            filters = {'device': device} if device else None

            sq = WebappIndexer.get_app_filter(self.request,
                                              filters,
                                              app_ids=app_ids)
            return Response({
                'objects':
                self.serializer_class(sq.execute().hits,
                                      many=True,
                                      context={
                                          'request': self.request
                                      }).data
            })
Esempio n. 5
0
    def filter_queryset(self, request, queryset, view):

        device_id = get_device_id(request)
        data = {
            'gaia': getattr(request, 'GAIA', False),
            'mobile': getattr(request, 'MOBILE', False),
            'tablet': getattr(request, 'TABLET', False),
        }
        flash_incompatible = data['mobile'] or data['gaia']

        if device_id:
            queryset = queryset.filter(
                Bool(must=[F('term', device=device_id)]))
        if flash_incompatible:
            queryset = queryset.filter(
                Bool(must_not=[F('term', uses_flash=True)]))

        return queryset
Esempio n. 6
0
    def filter_queryset(self, request, queryset, view):

        device_id = get_device_id(request)
        data = {
            'gaia': getattr(request, 'GAIA', False),
            'mobile': getattr(request, 'MOBILE', False),
            'tablet': getattr(request, 'TABLET', False),
        }
        flash_incompatible = data['mobile'] or data['gaia']

        if device_id:
            queryset = queryset.filter(
                Bool(must=[F('term', device=device_id)]))
        if flash_incompatible:
            queryset = queryset.filter(
                Bool(must_not=[F('term', uses_flash=True)]))

        return queryset
Esempio n. 7
0
    def get_apps(self, request, app_ids):
        """
        Takes a list of app_ids. Gets the apps, including filters.
        Returns an app_map for serializer context.
        """
        if request.QUERY_PARAMS.get('filtering', '1') == '0':
            # Without filtering.
            sq = WebappIndexer.search().filter(es_filter.Bool(
                should=[es_filter.Terms(id=app_ids)]
            ))[0:len(app_ids)]
        else:
            # With filtering.
            sq = WebappIndexer.get_app_filter(request, {
                'device': get_device_id(request)
            }, app_ids=app_ids)

        # Store the apps to attach to feed elements later.
        with statsd.timer('mkt.feed.views.apps_query'):
            apps = sq.execute().hits
        return dict((app.id, app) for app in apps)
Esempio n. 8
0
    def get_apps(self, request, app_ids):
        """
        Takes a list of app_ids. Gets the apps, including filters.
        Returns an app_map for serializer context.
        """
        if request.QUERY_PARAMS.get('filtering', '1') == '0':
            # Without filtering.
            sq = WebappIndexer.search().filter(es_filter.Bool(
                should=[es_filter.Terms(id=app_ids)]
            ))[0:len(app_ids)]
        else:
            # With filtering.
            sq = WebappIndexer.get_app_filter(request, {
                'device': get_device_id(request)
            }, app_ids=app_ids)

        # Store the apps to attach to feed elements later.
        with statsd.timer('mkt.feed.views.apps_query'):
            apps = sq.execute().hits
        return dict((app.id, app) for app in apps)
Esempio n. 9
0
    def list(self, request, *args, **kwargs):
        if (not settings.RECOMMENDATIONS_ENABLED or
                not settings.RECOMMENDATIONS_API_URL or
                not self.request.user.is_authenticated()):
            return self._popular()
        else:
            app_ids = []
            url = '{base_url}/api/v2/recommend/{limit}/{user_hash}/'.format(
                base_url=settings.RECOMMENDATIONS_API_URL,
                limit=20, user_hash=self.request.user.recommendation_hash)

            try:
                with statsd.timer('recommendation.get'):
                    resp = requests.get(
                        url, timeout=settings.RECOMMENDATIONS_API_TIMEOUT)
                if resp.status_code == 200:
                    app_ids = resp.json()['recommendations']
            except Timeout as e:
                log.warning(u'Recommendation timeout: {error}'.format(error=e))
            except RequestException as e:
                # On recommendation API exceptions we return popular.
                log.error(u'Recommendation exception: {error}'.format(error=e))

            if not app_ids:
                # Fall back to a popularity search.
                return self._popular()

            # Get list of installed apps and remove from app_ids.
            installed = list(
                request.user.installed_set.values_list('addon_id', flat=True))
            app_ids = filter(lambda a: a not in installed, app_ids)

            device = get_device_id(request)
            filters = {'device': device} if device else None

            sq = WebappIndexer.get_app_filter(self.request, filters,
                                              app_ids=app_ids)
            return Response({
                'objects': self.serializer_class(
                    sq.execute().hits, many=True,
                    context={'request': self.request}).data})