Example #1
0
def _filter_search(qs, query, filters=None, sorting=None,
                   sorting_default='-weekly_downloads'):
    """Filter an ES queryset based on a list of filters."""
    # Intersection of the form fields present and the filters we want to apply.
    filters = filters or DEFAULT_FILTERS
    sorting = sorting or DEFAULT_SORTING
    show = [f for f in filters if query.get(f)]

    if query.get('q'):
        qs = qs.query(or_=name_query(query['q'].lower()))
    if 'cat' in show:
        qs = qs.filter(category=query['cat'])
    if 'price' in show:
        if query['price'] == 'paid':
            qs = qs.filter(premium_type__in=amo.ADDON_PREMIUMS)
        elif query['price'] == 'free':
            qs = qs.filter(premium_type__in=amo.ADDON_FREES, price=0)
    if 'device' in show:
        qs = qs.filter(device=forms.DEVICE_CHOICES_IDS[query['device']])
    if 'sort' in show:
        qs = qs.order_by(sorting[query['sort']])
    elif not query.get('q'):
        # Sort by a default if there was no query so results are predictable.
        qs = qs.order_by(sorting_default)

    return qs
Example #2
0
def _filter_search(qs,
                   query,
                   filters=None,
                   sorting=None,
                   sorting_default='-weekly_downloads'):
    """Filter an ES queryset based on a list of filters."""
    # Intersection of the form fields present and the filters we want to apply.
    filters = filters or DEFAULT_FILTERS
    sorting = sorting or DEFAULT_SORTING
    show = [f for f in filters if query.get(f)]

    if query.get('q'):
        qs = qs.query(or_=name_query(query['q']))
    if 'cat' in show:
        qs = qs.filter(category=query['cat'])
    if 'price' in show:
        if query['price'] == 'paid':
            qs = qs.filter(premium_type__in=amo.ADDON_PREMIUMS, price__gt=0)
        elif query['price'] == 'free':
            qs = qs.filter(premium_type=amo.ADDON_FREE, price=0)
    if 'device' in show:
        qs = qs.filter(device=forms.DEVICE_CHOICES_IDS[query['device']])
    if 'sort' in show:
        qs = qs.order_by(sorting[query['sort']])
    elif not query.get('q'):
        # Sort by a default if there was no query so results are predictable.
        qs = qs.order_by(sorting_default)

    return qs
Example #3
0
def _filter_search(request, qs, query, filters=None, sorting=None,
                   sorting_default='-popularity', region=None):
    """Filter an ES queryset based on a list of filters."""
    # Intersection of the form fields present and the filters we want to apply.
    filters = filters or DEFAULT_FILTERS
    sorting = sorting or DEFAULT_SORTING
    show = filter(query.get, filters)

    if query.get('q'):
        qs = qs.query(or_=name_query(query['q'].lower()))
    if 'cat' in show:
        qs = qs.filter(category=query['cat'])
    if 'price' in show:
        if query['price'] == 'paid':
            qs = qs.filter(premium_type__in=amo.ADDON_PREMIUMS)
        elif query['price'] == 'free':
            qs = qs.filter(premium_type__in=amo.ADDON_FREES, price=0)
    if 'device' in show:
        qs = qs.filter(device=forms.DEVICE_CHOICES_IDS[query['device']])
    if 'premium_types' in show:
        if query.get('premium_types'):
            qs = qs.filter(premium_type__in=query.get('premium_types'))
    if 'app_type' in query and query['app_type']:
        qs = qs.filter(app_type=query['app_type'])
    if 'sort' in show:
        sort_by = None
        if query['sort'] in sorting:
            sort_by = sorting[query['sort']]

        # For "Adolescent" regions popularity is global installs + reviews.

        if query['sort'] == 'popularity' and region and not region.adolescent:
            # For "Mature" regions popularity becomes installs + reviews
            # from only that region.
            sort_by = '-popularity_%s' % region.id

        if sort_by:
            qs = qs.order_by(sort_by)
    elif not query.get('q'):

        if (sorting_default == 'popularity' and region and
            not region.adolescent):
            # For "Mature" regions popularity becomes installs + reviews
            # from only that region.
            sorting_default = '-popularity_%s' % region.id

        # Sort by a default if there was no query so results are predictable.
        qs = qs.order_by(sorting_default)

    # Filter by adult/child flags.
    region_id = get_region_id()
    if region_id in regions.CHILD_EXCLUDED_IDS:
        qs = qs.filter(~F(flag_child=True))
    if region_id in regions.ADULT_EXCLUDED_IDS:
        qs = qs.filter(~F(flag_adult=True))

    return qs
Example #4
0
def _filter_search(request,
                   qs,
                   query,
                   filters=None,
                   sorting=None,
                   sorting_default='-popularity',
                   region=None):
    """Filter an ES queryset based on a list of filters."""
    # Intersection of the form fields present and the filters we want to apply.
    filters = filters or DEFAULT_FILTERS
    sorting = sorting or DEFAULT_SORTING
    show = filter(query.get, filters)

    if query.get('q'):
        qs = qs.query(or_=name_query(query['q'].lower()))
    if 'cat' in show:
        qs = qs.filter(category=query['cat'])
    if 'price' in show:
        if query['price'] == 'paid':
            qs = qs.filter(premium_type__in=amo.ADDON_PREMIUMS)
        elif query['price'] == 'free':
            qs = qs.filter(premium_type__in=amo.ADDON_FREES, price=0)
    if 'device' in show:
        qs = qs.filter(device=forms.DEVICE_CHOICES_IDS[query['device']])
    if 'premium_types' in show:
        if query.get('premium_types'):
            qs = qs.filter(premium_type__in=query.get('premium_types'))
    if 'app_type' in query and query['app_type']:
        qs = qs.filter(app_type=query['app_type'])
    if 'sort' in show:
        sort_by = None
        if query['sort'] in sorting:
            sort_by = sorting[query['sort']]

        # For "Adolescent" regions popularity is global installs + reviews.

        if query['sort'] == 'popularity' and region and not region.adolescent:
            # For "Mature" regions popularity becomes installs + reviews
            # from only that region.
            sort_by = '-popularity_%s' % region.id

        if sort_by:
            qs = qs.order_by(sort_by)
    elif not query.get('q'):

        if (sorting_default == 'popularity' and region
                and not region.adolescent):
            # For "Mature" regions popularity becomes installs + reviews
            # from only that region.
            sorting_default = '-popularity_%s' % region.id

        # Sort by a default if there was no query so results are predictable.
        qs = qs.order_by(sorting_default)

    return qs
Example #5
0
def _filter_search(qs, query, filters=None, sorting=None,
                   sorting_default='-popularity', region=None):
    """Filter an ES queryset based on a list of filters."""
    # Intersection of the form fields present and the filters we want to apply.
    filters = filters or DEFAULT_FILTERS
    sorting = sorting or DEFAULT_SORTING
    show = filter(query.get, filters)

    if query.get('q'):
        qs = qs.query(or_=name_query(query['q'].lower()))
    if 'cat' in show:
        qs = qs.filter(category=query['cat'])
    if waffle.switch_is_active('disabled-payments'):
        qs = qs.filter(premium_type__in=amo.ADDON_FREES, price=0)
    elif 'price' in show:
        if query['price'] == 'paid':
            qs = qs.filter(premium_type__in=amo.ADDON_PREMIUMS)
        elif query['price'] == 'free':
            qs = qs.filter(premium_type__in=amo.ADDON_FREES, price=0)
    if 'device' in show:
        qs = qs.filter(device=forms.DEVICE_CHOICES_IDS[query['device']])
    if 'sort' in show:
        sort_by = None
        if query['sort'] in sorting:
            sort_by = sorting[query['sort']]

        # For "Adolescent" regions popularity is global installs + reviews.

        if query['sort'] == 'popularity' and region and not region.adolescent:
            # For "Mature" regions popularity becomes installs + reviews
            # from only that region.
            sort_by = '-popularity_%s' % region.id

        if sort_by:
            qs = qs.order_by(sort_by)
    elif not query.get('q'):

        if (sorting_default == 'popularity' and region and
            not region.adolescent):
            # For "Mature" regions popularity becomes installs + reviews
            # from only that region.
            sorting_default = '-popularity_%s' % region.id

        # Sort by a default if there was no query so results are predictable.
        qs = qs.order_by(sorting_default)

    return qs
Example #6
0
def _filter_search(qs, query, filters=None, sorting=None, sorting_default="-popularity", region=None):
    """Filter an ES queryset based on a list of filters."""
    # Intersection of the form fields present and the filters we want to apply.
    filters = filters or DEFAULT_FILTERS
    sorting = sorting or DEFAULT_SORTING
    show = filter(query.get, filters)

    if query.get("q"):
        qs = qs.query(or_=name_query(query["q"].lower()))
    if "cat" in show:
        qs = qs.filter(category=query["cat"])
    if "price" in show:
        if query["price"] == "paid":
            qs = qs.filter(premium_type__in=amo.ADDON_PREMIUMS)
        elif query["price"] == "free":
            qs = qs.filter(premium_type__in=amo.ADDON_FREES, price=0)
    if "device" in show:
        qs = qs.filter(device=forms.DEVICE_CHOICES_IDS[query["device"]])
    if "sort" in show:
        sort_by = sorting[query["sort"]]

        # For "Adolescent" regions popularity is global installs + reviews.

        if query["sort"] == "popularity" and region and not region.adolescent:
            # For "Mature" regions popularity becomes installs + reviews
            # from only that region.
            sort_by = "-popularity_%s" % region.id

        qs = qs.order_by(sort_by)
    elif not query.get("q"):

        if sorting_default == "popularity" and region and not region.adolescent:
            # For "Mature" regions popularity becomes installs + reviews
            # from only that region.
            sorting_default = "-popularity_%s" % region.id

        # Sort by a default if there was no query so results are predictable.
        qs = qs.order_by(sorting_default)

    return qs
Example #7
0
def _filter_search(qs, query, filters, sorting, sorting_default="-weekly_downloads"):
    """Filter an ES queryset based on a list of filters."""
    # Intersection of the form fields present and the filters we want to apply.
    show = [f for f in filters if query.get(f)]

    if query.get("q"):
        qs = qs.query(or_=name_query(query["q"]))
    if "cat" in show:
        qs = qs.filter(category=query["cat"])
    if "price" in show:
        if query["price"] == "paid":
            qs = qs.filter(premium_type__in=amo.ADDON_PREMIUMS, price__gt=0)
        elif query["price"] == "free":
            qs = qs.filter(premium_type=amo.ADDON_FREE, price=0)
    if "device" in show:
        qs = qs.filter(device=forms.DEVICE_CHOICES_IDS[query["device"]])
    if "sort" in show:
        qs = qs.order_by(sorting[query["sort"]])
    elif not query.get("q"):
        # Sort by a default if there was no query so results are predictable.
        qs = qs.order_by(sorting_default)

    return qs