Skip to content
This repository was archived by the owner on Mar 15, 2018. It is now read-only.

Commit f7843fc

Browse files
author
Rob Hudson
committed
Filtered the result of apps by adult/child (bug 852567)
1 parent 7cf0f08 commit f7843fc

File tree

4 files changed

+61
-25
lines changed

4 files changed

+61
-25
lines changed

mkt/api/resources.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import amo
1818
from addons.forms import CategoryFormSet
19-
from addons.models import Addon, AddonUser, Category, Preview
19+
from addons.models import Addon, AddonUser, Category, Preview, Webapp
2020
from amo.decorators import write
2121
from amo.utils import no_translation
2222
from constants.applications import DEVICE_TYPES
@@ -35,7 +35,7 @@
3535
from mkt.carriers import get_carrier_id
3636
from mkt.developers import tasks
3737
from mkt.developers.forms import NewManifestForm, PreviewForm
38-
from mkt.regions import get_region_id
38+
from mkt.regions import get_region_id, get_region, REGIONS_DICT
3939
from mkt.submit.forms import AppDetailsBasicForm
4040
from mkt.webapps.utils import app_to_dict
4141

@@ -110,7 +110,7 @@ class AppResource(CORSResource, MarketplaceModelResource):
110110
'previews', readonly=True)
111111

112112
class Meta(MarketplaceModelResource.Meta):
113-
queryset = Addon.objects.filter(type=amo.ADDON_WEBAPP)
113+
queryset = Webapp.objects.all() # Gets overriden in dispatch.
114114
fields = ['categories', 'description', 'device_types',
115115
'homepage', 'id', 'name', 'privacy_policy',
116116
'status', 'summary', 'support_email',
@@ -126,6 +126,15 @@ class Meta(MarketplaceModelResource.Meta):
126126
# Throttle users without Apps:APIUnthrottled at 10 POST requests/day.
127127
throttle = CacheThrottle(throttle_at=10, timeframe=60 * 60 * 24)
128128

129+
def dispatch(self, request_type, request, **kwargs):
130+
# Using `Webapp.objects.all()` here forces a new queryset, which for
131+
# now avoids bug 854505. We're also using this to filter by flagged
132+
# apps.
133+
self._meta.queryset = Webapp.objects.exclude(
134+
id__in=Webapp.get_excluded_in(REGIONS_DICT[get_region()]))
135+
return super(AppResource, self).dispatch(request_type, request,
136+
**kwargs)
137+
129138
@write
130139
@transaction.commit_on_success
131140
def obj_create(self, bundle, request, **kwargs):
@@ -188,13 +197,8 @@ def get_and_check_ownership(self, request, allow_anon=False, **kwargs):
188197
try:
189198
# Use queryset, not get_object_list to ensure a distinction
190199
# between a 404 and a 403.
191-
#
192-
# For reasons I haven't been able to figure out, accessing the
193-
# queryset at this point will get you the locale of the class
194-
# when it was initialized, which means that the locale gets
195-
# set and results in bug 854505.
196-
obj = Addon.objects.filter(type=amo.ADDON_WEBAPP).get(**kwargs)
197-
except Addon.DoesNotExist:
200+
obj = self._meta.queryset.get(**kwargs)
201+
except self._meta.object_class.DoesNotExist:
198202
raise ImmediateHttpResponse(response=http.HttpNotFound())
199203

200204
# If it's public, just return it.
@@ -250,8 +254,8 @@ def get_object_list(self, request):
250254
if not request.amo_user:
251255
log.info('Anonymous listing not allowed')
252256
raise ImmediateHttpResponse(response=http.HttpForbidden())
253-
return Addon.objects.filter(type=amo.ADDON_WEBAPP,
254-
authors=request.amo_user)
257+
return self._meta.queryset.filter(type=amo.ADDON_WEBAPP,
258+
authors=request.amo_user)
255259

256260

257261
class StatusResource(MarketplaceModelResource):

mkt/api/tests/test_handlers.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import amo
1010
from addons.models import (Addon, AddonUpsell, AddonDeviceType,
11-
AddonUser, Category, Preview)
11+
AddonUser, Category, Flag, Preview)
1212
from amo.tests import app_factory, AMOPaths
1313
from files.models import FileUpload
1414
from users.models import UserProfile
@@ -691,18 +691,29 @@ def test_public_waiting(self):
691691
class TestAppDetail(BaseOAuth, AMOPaths):
692692
fixtures = fixture('user_2519', 'webapp_337141')
693693

694-
def test_price(self):
694+
def setUp(self):
695+
super(TestAppDetail, self).setUp()
695696
self.get_url = get_url('app', pk=337141)
697+
698+
def test_price(self):
696699
res = self.client.get(self.get_url)
697700
data = json.loads(res.content)
698701
eq_(data['price'], None)
699702

700703
def test_price_other_region(self):
701-
self.get_url = get_url('app', pk=337141)
702704
res = self.client.get(self.get_url, {'lang': 'fr'})
703705
data = json.loads(res.content)
704706
eq_(data['price'], None)
705707

708+
def test_flagged(self):
709+
# Acess a flagged app via API should 404.
710+
Flag.objects.create(addon_id=337141, adult_content=True)
711+
Webapp.objects.get(pk=337141).save()
712+
res = self.client.get(
713+
self.get_url,
714+
data={'region': list(regions.ADULT_EXCLUDED)[0].slug})
715+
eq_(res.status_code, 404)
716+
706717

707718
class TestCategoryHandler(BaseOAuth):
708719

mkt/webapps/models.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import amo.models
2424
from access.acl import action_allowed, check_reviewer
2525
from addons import query
26-
from addons.models import (Addon, AddonDeviceType, Category,
26+
from addons.models import (Addon, AddonDeviceType, Category, Flag,
2727
update_search_index)
2828
from addons.signals import version_changed
2929
from amo.decorators import skip_cache
@@ -40,6 +40,7 @@
4040
from mkt.zadmin.models import FeaturedApp
4141

4242
import mkt
43+
from mkt import regions
4344
from mkt.constants import apps
4445
from mkt.constants import APP_IMAGE_SIZES
4546
from mkt.webapps.utils import get_locale_properties
@@ -102,9 +103,9 @@ def by_identifier(self, identifier):
102103
Look up a single app by its `id` or `app_slug`.
103104
104105
If the identifier is coercable into an integer, we first check for an
105-
ID match, falling back to a slug check (probably not necessary, as there
106-
is validation preventing numeric slugs). Otherwise, we only look for a
107-
slug match.
106+
ID match, falling back to a slug check (probably not necessary, as
107+
there is validation preventing numeric slugs). Otherwise, we only look
108+
for a slug match.
108109
"""
109110
try:
110111
return self.get(id=identifier)
@@ -517,9 +518,9 @@ def get_regions(self):
517518
<class 'mkt.constants.regions.US'>,
518519
<class 'mkt.constants.regions.WORLDWIDE'>]
519520
"""
520-
regions = map(mkt.regions.REGIONS_CHOICES_ID_DICT.get,
521-
self.get_region_ids(worldwide=True))
522-
return sorted(regions, key=lambda x: x.slug)
521+
_regions = map(mkt.regions.REGIONS_CHOICES_ID_DICT.get,
522+
self.get_region_ids(worldwide=True))
523+
return sorted(_regions, key=lambda x: x.slug)
523524

524525
def listed_in(self, region=None, category=None):
525526
listed = []
@@ -559,8 +560,18 @@ def featured(cls, cat=None, region=None, limit=9, mobile=False,
559560
@classmethod
560561
def get_excluded_in(cls, region):
561562
"""Return IDs of Webapp objects excluded from a particular region."""
562-
return list(AddonExcludedRegion.objects.filter(region=region.id)
563-
.values_list('addon', flat=True))
563+
excluded = list(AddonExcludedRegion.objects.filter(region=region.id)
564+
.values_list('addon', flat=True))
565+
q = models.Q()
566+
if region.id in regions.ADULT_EXCLUDED_IDS:
567+
q |= models.Q(adult_content=True)
568+
if region.id in regions.CHILD_EXCLUDED_IDS:
569+
q |= models.Q(child_content=True)
570+
571+
excluded += list(Flag.objects.filter(q)
572+
.values_list('addon', flat=True))
573+
574+
return list(set(excluded))
564575

565576
@classmethod
566577
def from_search(cls, cat=None, region=None, gaia=False, mobile=False,

mkt/webapps/tests/test_models.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919

2020
import amo
2121
from addons.models import (Addon, AddonCategory, AddonDeviceType,
22-
BlacklistedSlug, Category, Preview, version_changed)
22+
BlacklistedSlug, Category, Flag, Preview,
23+
version_changed)
2324
from addons.signals import version_changed as version_changed_signal
2425
from amo.helpers import absolutify
2526
from amo.tests import app_factory, version_factory
@@ -415,6 +416,15 @@ def test_nomination_pkg_public_new_version(self):
415416
v = Version.objects.create(addon=app, version='1.9')
416417
self.assertCloseToNow(v.nomination)
417418

419+
def test_excluded_in(self):
420+
app1 = app_factory()
421+
app2 = app_factory()
422+
region = list(mkt.regions.ADULT_EXCLUDED)[0]
423+
AddonExcludedRegion.objects.create(addon=app1, region=region.id)
424+
Flag.objects.create(addon=app1, adult_content=True)
425+
Flag.objects.create(addon=app2, adult_content=True)
426+
eq_(Webapp.get_excluded_in(region), [app1.id, app2.id])
427+
418428

419429
class TestPackagedAppManifestUpdates(amo.tests.TestCase):
420430
# Note: More extensive tests for `Addon.update_names` are in the Addon

0 commit comments

Comments
 (0)