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

Commit

Permalink
fill empty spots with Worldwide-featured apps (bug 782335)
Browse files Browse the repository at this point in the history
  • Loading branch information
cvan committed Aug 13, 2012
1 parent 8bce649 commit 6465108
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
21 changes: 21 additions & 0 deletions mkt/home/tests/test_views.py
@@ -1,9 +1,11 @@
from nose.tools import eq_

from amo.tests import app_factory
from amo.urlresolvers import reverse

import mkt
from mkt.browse.tests.test_views import BrowseBase
from mkt.zadmin.models import FeaturedApp, FeaturedAppRegion


class TestHome(BrowseBase):
Expand All @@ -27,6 +29,25 @@ def test_featured(self):
eq_(self.get_pks('featured', self.url, {'region': region}),
[c.id] if region == 'us' else [])

def test_featured_fallback_to_worldwide(self):
a, b, c = self.setup_featured()

worldwide_apps = [app_factory().id for x in xrange(6)]
for app in worldwide_apps:
fa = FeaturedApp.objects.create(app_id=app, category=None)
FeaturedAppRegion.objects.create(featured_app=fa,
region=mkt.regions.WORLDWIDE.id)

# In US: 1 US-featured app + 5 Worldwide-featured app.
# Elsewhere: 6 Worldwide-featured apps.
for region in mkt.regions.REGIONS_DICT:
if region == 'us':
expected = [c.id] + worldwide_apps[:5]
else:
expected = worldwide_apps
eq_(self.get_pks('featured', self.url, {'region': region}),
expected)

def test_popular(self):
a, b = self.setup_popular()
# Check that these apps are shown.
Expand Down
40 changes: 36 additions & 4 deletions mkt/webapps/models.py
Expand Up @@ -346,15 +346,47 @@ def featured(cls, cat=None, region=None, limit=6):
.filter(app__status=amo.STATUS_PUBLIC,
app__disabled_by_user=False)
.order_by('-app__weekly_downloads'))

if isinstance(cat, list):
qs = qs.filter(category__in=cat)
else:
qs = qs.filter(category=cat)
qs = qs.filter(category=cat.id if cat else None)

locale_qs = FeaturedApp.objects.none()
worldwide_qs = FeaturedApp.objects.none()

if region:
qs = qs.filter(regions__region=region.id)
excluded = cls.get_excluded_in(region)
locale_qs = (qs.filter(regions__region=region.id)
.exclude(app__id__in=excluded))

# Fill the empty spots with Worldwide-featured apps.
if limit:
empty_spots = limit - locale_qs.count()
if empty_spots and region != mkt.regions.WORLDWIDE:
ww = mkt.regions.WORLDWIDE.id
worldwide_qs = (qs.filter(regions__region=ww)
.exclude(id__in=[x.id for x in locale_qs])
.exclude(app__id__in=excluded))[:limit]

if limit:
qs = qs[:limit]
return [fa.app for fa in qs]
locale_qs = locale_qs[:limit]

if worldwide_qs:
combined = ([fa.app for fa in locale_qs] +
[fa.app for fa in worldwide_qs])
return combined[:limit]

return [fa.app for fa in locale_qs]

@classmethod
def get_excluded_in(cls, region):
"""Return IDs of Webapp objects excluded from a particular region."""
# Worldwide is a subset of Future regions.
if region == mkt.regions.WORLDWIDE:
region = mkt.regions.FUTURE
return list(AddonExcludedRegion.objects.filter(region=region.id)
.values_list('addon', flat=True))

@classmethod
def from_search(cls):
Expand Down

0 comments on commit 6465108

Please sign in to comment.