diff --git a/apps/addons/helpers.py b/apps/addons/helpers.py index fa6be87e18..ca736436fe 100644 --- a/apps/addons/helpers.py +++ b/apps/addons/helpers.py @@ -1,3 +1,5 @@ +import random + import jinja2 from jingo import register, env @@ -101,6 +103,17 @@ def tags_box(context, addon, tags=None): return c +@register.filter +@jinja2.contextfilter +def shuffle_boundary(context, addons, boundary=None): + if not boundary: + return addons + split = addons[:boundary], addons[boundary:] + for s in split: + random.shuffle(s) + return split[0] + split[1] + + @register.inclusion_tag('addons/listing/items.html') @jinja2.contextfunction def addon_listing_items(context, addons, show_date=False, src=None, @@ -110,8 +123,10 @@ def addon_listing_items(context, addons, show_date=False, src=None, @register.inclusion_tag('addons/listing/items_compact.html') @jinja2.contextfunction -def addon_listing_items_compact(context, addons, show_date=False, - src=None): +def addon_listing_items_compact(context, addons, boundary=0, + show_date=False, src=None): + if show_date == 'featured' and boundary: + addons = shuffle_boundary(context, list(addons), boundary) return new_context(**locals()) diff --git a/apps/addons/models.py b/apps/addons/models.py index 6dd1c2fac8..3b082a3253 100644 --- a/apps/addons/models.py +++ b/apps/addons/models.py @@ -63,15 +63,50 @@ class AddonManager(amo.models.ManagerBase): """Get valid, enabled add-ons only""" return self.filter(self.valid_q(amo.LISTED_STATUSES)) - def featured(self, app): + def featured(self, app, by_locale=False): """ - Filter for all featured add-ons for an application in all locales. + Filter for all featured add-ons for an application. Specify + by_locale to have it filtered by the current locale. """ - return self.valid().filter(feature__application=app.id) + qs = Q(feature__application=app.id) + if by_locale: + locales = [] + for locale in by_locale(): + locale = to_language(locale) if locale else locale + locales.append(Q(feature__locale=locale)) - def category_featured(self): - """Get all category-featured add-ons for ``app`` in all locales.""" - return self.filter(addoncategory__feature=True) + qs = qs & Q(feature__isnull=False) & reduce(operator.or_, locales) + return (self.valid().filter(qs).order_by('-feature__locale')) + + return self.valid().filter(qs) + + def category_featured(self, category=None, by_locale=False): + """ + Filter for all featured addons for a category. Specify + by_locale to have it filtered by the current locale. + """ + qs = Q(addoncategory__feature=True) + + if by_locale: + locales = [] + for locale in by_locale(): + prefix = 'addoncategory__feature_locales' + if locale: + locale = to_language(locale) + prefix += '__contains' + else: + # Sadly looks like theres '' and NULL in the column, + # hopefully the new admin tools will clean this out. + locales.append(Q(**{prefix: ''})) + locales.append(Q(**{prefix: locale})) + + qs = (qs & Q(addoncategory__category=category) & + reduce(operator.or_, locales)) + return self.filter(qs).order_by('-addoncategory__feature_locales') + + if category: + qs = qs & Q(addoncategory__category=category) + return self.filter(qs) def listed(self, app, *status): """ diff --git a/apps/addons/templates/addons/home.html b/apps/addons/templates/addons/home.html index 04056a75fe..20fc444e5c 100644 --- a/apps/addons/templates/addons/home.html +++ b/apps/addons/templates/addons/home.html @@ -80,8 +80,8 @@
{% for key, addons in addon_sets.items() %}
- {{ addon_listing_items_compact(addons, show_date=key, - src='homepagebrowse') }} + {{ addon_listing_items_compact(addons, boundary=boundary, + show_date=key, src='homepagebrowse') }}