def dispatch(self, request_type, request, **kwargs): # Using `Webapp.objects.all()` here forces a new queryset, which for # now avoids bug 854505. We're also using this to filter by flagged # apps. self._meta.queryset_base = Webapp.objects.all() self._meta.queryset = self._meta.queryset_base.exclude(id__in=get_excluded_in(REGIONS_DICT[get_region()].id)) return super(AppResource, self).dispatch(request_type, request, **kwargs)
def dispatch(self, request_type, request, **kwargs): # Using `Webapp.objects.all()` here forces a new queryset, which for # now avoids bug 854505. We're also using this to filter by flagged # apps. self._meta.queryset_base = Webapp.objects.all() self._meta.queryset = self._meta.queryset_base.exclude( id__in=get_excluded_in(REGIONS_DICT[get_region()].id)) return super(AppResource, self).dispatch(request_type, request, **kwargs)
def featured(self, cat=None, region=None, limit=9, mobile=False, gaia=False, tablet=False): """ Filters the QuerySet, removing FeaturedApp instances that should not be featured based on the passed criteria. If a region is defined and there are fewer than `limit` items remaining in the resultant queryset, the difference is populated by additional apps that are featured worldwide. """ qs = self.active() Webapp = get_model('webapps', 'Webapp') excluded = Webapp.objects.none() if region: from mkt.webapps.models import get_excluded_in excluded = get_excluded_in(region.id) if waffle.switch_is_active('disabled-payments') or not gaia: qs = qs.filter(app__premium_type__in=amo.ADDON_FREES) qs = qs.for_category(cat) carrier = get_carrier() if carrier: qs = qs.for_carrier(carrier) if gaia: qs = qs.gaia() elif mobile: qs = qs.mobile() if tablet: qs = qs.tablet() qs_pre_region = qs._clone() if region: qs = qs.for_region(region).exclude(app__id__in=excluded) # Fill the empty spots with Worldwide-featured apps. if limit: empty_spots = limit - qs.count() if empty_spots > 0 and region != mkt.regions.WORLDWIDE: qs |= (qs_pre_region.worldwide() .exclude(id__in=[x.id for x in qs]) .exclude(app__id__in=excluded)) if limit: qs = qs[:limit] return qs
def queryset(self): """Get items based on ID or search by name.""" res = Addon.objects.none() q = self.request.GET.get(self.key) if q: pk = None try: pk = int(q) except ValueError: pass qs = None if pk: qs = Addon.objects.filter(id=int(q), disabled_by_user=False) elif len(q) > 2: qs = (S(Webapp).query(or_=name_only_query(q.lower())) .filter(is_disabled=False)) if qs: res = qs.filter(type__in=self.types, status__in=amo.REVIEWED_STATUSES) if self.category: res = res.filter(category__in=[self.category]) if (self.mobile or self.tablet) and not self.gaia: if isinstance(res, S): res = res.filter(~F(premium_type__in=amo.ADDON_PREMIUMS, price__gt=0)) else: res.exclude(premium_type__in=amo.ADDON_PREMIUMS, price__gt=0) region = getattr(self.request, 'REGION', mkt.regions.WORLDWIDE) if region: excluded = get_excluded_in(region.id) if excluded: if isinstance(res, S): # ES? Do fanciness. res = res.filter(~F(id__in=excluded)) else: # Django ORM? Do an `exclude`. res = res.exclude(id__in=excluded) if self.gaia: res = res.filter(device=amo.DEVICE_GAIA.id, uses_flash=False) elif self.mobile: res = res.filter(device=amo.DEVICE_MOBILE.id, uses_flash=False) return res
def queryset(self): """Get items based on ID or search by name.""" res = Addon.objects.none() q = self.request.GET.get(self.key) if q: pk = None try: pk = int(q) except ValueError: pass qs = None if pk: qs = Addon.objects.filter(id=int(q), disabled_by_user=False) elif len(q) > 2: qs = (S(Webapp).query(or_=name_only_query(q.lower())).filter( is_disabled=False)) if qs: res = qs.filter(type__in=self.types, status__in=amo.REVIEWED_STATUSES) if self.category: res = res.filter(category__in=[self.category]) if (self.mobile or self.tablet) and not self.gaia: if isinstance(res, S): res = res.filter( ~F(premium_type__in=amo.ADDON_PREMIUMS, price__gt=0)) else: res.exclude(premium_type__in=amo.ADDON_PREMIUMS, price__gt=0) region = getattr(self.request, 'REGION', mkt.regions.WORLDWIDE) if region: excluded = get_excluded_in(region.id) if excluded: if isinstance(res, S): # ES? Do fanciness. res = res.filter(~F(id__in=excluded)) else: # Django ORM? Do an `exclude`. res = res.exclude(id__in=excluded) if self.gaia: res = res.filter(device=amo.DEVICE_GAIA.id, uses_flash=False) elif self.mobile: res = res.filter(device=amo.DEVICE_MOBILE.id, uses_flash=False) return res
def get_queryset(self): return Webapp.objects.all().exclude(id__in=get_excluded_in(get_region().id))
def get_queryset(self): return Webapp.objects.all().exclude( id__in=get_excluded_in(get_region().id))
def featured(self, cat=None, region=None, limit=9, mobile=False, gaia=False, tablet=False, profile=None): """ Filters the QuerySet, removing FeaturedApp instances that should not be featured based on the passed criteria. If a region is defined and there are fewer than `limit` items remaining in the resultant queryset, the difference is populated by additional apps that are featured worldwide. If `profile` (a FeatureProfile object) is provided we filter by the profile. If you don't want to filter by these don't pass it. I.e. do the device detection for when this happens elsewhere. """ qs = self.active() Webapp = get_model('webapps', 'Webapp') excluded = Webapp.objects.none() if region: from mkt.webapps.models import get_excluded_in excluded = get_excluded_in(region.id) if waffle.switch_is_active('disabled-payments') or not gaia: qs = qs.filter(app__premium_type__in=amo.ADDON_FREES) qs = qs.for_category(cat) carrier = get_carrier() if carrier: qs = qs.for_carrier(carrier) if gaia: qs = qs.gaia() elif mobile: qs = qs.mobile() if tablet: qs = qs.tablet() if profile and waffle.switch_is_active('buchets'): # Exclude apps that require any features we don't support. qs = qs.filter(**profile.to_kwargs( prefix='app___current_version__features__has_')) qs_pre_region = qs._clone() if region: qs = qs.for_region(region).exclude(app__id__in=excluded) # Fill the empty spots with Worldwide-featured apps. if limit: empty_spots = limit - qs.count() if empty_spots > 0 and region != mkt.regions.WORLDWIDE: qs |= (qs_pre_region.worldwide().exclude( id__in=[x.id for x in qs]).exclude(app__id__in=excluded)) if limit: qs = qs[:limit] return qs
def get_queryset(self): return Webapp.objects.all().exclude( id__in=get_excluded_in(REGIONS_DICT[get_region()].id))
def featured(self, cat=None, region=None, limit=9, mobile=False, gaia=False, tablet=False, profile=None): """ Filters the QuerySet, removing FeaturedApp instances that should not be featured based on the passed criteria. If a region is defined and there are fewer than `limit` items remaining in the resultant queryset, the difference is populated by additional apps that are featured worldwide. If `profile` (a FeatureProfile object) is provided we filter by the profile. If you don't want to filter by these don't pass it. I.e. do the device detection for when this happens elsewhere. """ qs = self.active() Webapp = get_model('webapps', 'Webapp') excluded = Webapp.objects.none() if region: from mkt.webapps.models import get_excluded_in excluded = get_excluded_in(region.id) if waffle.switch_is_active('disabled-payments') or not gaia: qs = qs.filter(app__premium_type__in=amo.ADDON_FREES) qs = qs.for_category(cat) carrier = get_carrier() if carrier: qs = qs.for_carrier(carrier) if gaia: qs = qs.gaia() elif mobile: qs = qs.mobile() if tablet: qs = qs.tablet() if profile and waffle.switch_is_active('buchets'): # Exclude apps that require any features we don't support. qs = qs.filter(**profile.to_kwargs( prefix='app___current_version__features__has_')) qs_pre_region = qs._clone() if region: qs = qs.for_region(region).exclude(app__id__in=excluded) # Fill the empty spots with Worldwide-featured apps. if limit: empty_spots = limit - qs.count() if empty_spots > 0 and region != mkt.regions.WORLDWIDE: qs |= (qs_pre_region.worldwide() .exclude(id__in=[x.id for x in qs]) .exclude(app__id__in=excluded)) if limit: qs = qs[:limit] return qs
def test_excluded_in(self): app1 = app_factory() region = mkt.regions.BR AddonExcludedRegion.objects.create(addon=app1, region=region.id) eq_(get_excluded_in(region.id), [app1.id])