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
def get_queryset(self): qs = Category.objects.filter(type=amo.ADDON_WEBAPP, weight__gte=0) if self.action == "list": qs = qs.filter(Q(region__isnull=True) | Q(region=get_region_id())) # Check carrier. carrier = get_carrier_id() carrier_f = Q(carrier__isnull=True) if carrier: carrier_f |= Q(carrier=carrier) qs = qs.filter(carrier_f) return qs.order_by("-carrier", "-region", "-weight")
def get_queryset(self): qs = Category.objects.filter(type=amo.ADDON_WEBAPP, weight__gte=0) if self.action == 'list': qs = qs.filter(Q(region__isnull=True) | Q(region=get_region_id())) # Check carrier. carrier = get_carrier_id() carrier_f = Q(carrier__isnull=True) if carrier: carrier_f |= Q(carrier=carrier) qs = qs.filter(carrier_f) return qs.order_by('-carrier', '-region', '-weight')
def obj_get_list(self, request=None, **kwargs): objs = super(CategoryResource, self).obj_get_list(request, **kwargs) # Filter by region or worldwide. objs = objs.filter(Q(region__isnull=True) | Q(region=get_region_id())) # Check carrier. carrier = get_carrier_id() carrier_f = Q(carrier__isnull=True) if carrier: carrier_f |= Q(carrier=carrier) objs = objs.filter(carrier_f) return objs
def test_get_region_empty(): eq_(get_region(), regions.RESTOFWORLD.slug) eq_(get_region_id(), regions.RESTOFWORLD.id)
def test_get_region_not_empty(local): local.region = 'us' eq_(get_region(), 'us') eq_(get_region_id(), regions.US.id)
def test_get_region_empty(): eq_(get_region(), regions.WORLDWIDE.slug) eq_(get_region_id(), regions.WORLDWIDE.id)