def personas(request, category=None): listing = personas_listing(request, category) # I guess this was a Complete Theme after all. if isinstance(listing, (HttpResponsePermanentRedirect, HttpResponseRedirect)): return listing categories, filter_, base, cat = listing count = cat.count if cat else base.count() addons = amo.utils.paginate(request, filter_.qs, PAGINATE_PERSONAS_BY, count=count) if 'sort' not in request.GET and count > MIN_COUNT_FOR_LANDING: template = 'browse/personas/category_landing.html' else: template = 'browse/personas/grid.html' if cat: ids = AddonCategory.creatured_random(cat, request.LANG) featured = manual_order(base, ids, pk_name="addons.id") else: ids = Addon.featured_random(request.APP, request.LANG) featured = manual_order(base, ids, pk_name="addons.id") ctx = {'categories': categories, 'category': cat, 'addons': addons, 'filter': filter_, 'sorting': filter_.field, 'sort_opts': filter_.opts, 'featured': featured, 'search_cat': 'themes', 'is_homepage': cat is None and 'sort' not in request.GET} return render(request, template, ctx)
def test_featured_ids(self): FeaturedCollection.objects.filter(collection__addons=3615)[0].delete() another = Addon.objects.get(id=1003) self.change_addon(another, 'en-US') items = Addon.featured_random(amo.FIREFOX, 'en-US') # The order should be random within those boundaries. assert [1003, 3481] == sorted(items[0:2]) assert [1001, 2464, 7661, 15679] == sorted(items[2:])
def test_homepage_filter(self): # Ensure that the base homepage filter is applied. res = self.client.get(reverse('home')) listed = [p.pk for p in (Addon.objects .listed(amo.FIREFOX) .exclude(type=amo.ADDON_PERSONA))] featured = Addon.featured_random(amo.FIREFOX, 'en-US') actual = [p.pk for p in res.context['featured']] assert sorted(actual) == sorted(set(listed) & set(featured))
def personas(request, category=None, template=None): listing = personas_listing(request, category) # I guess this was a Complete Theme after all. if isinstance(listing, (HttpResponsePermanentRedirect, HttpResponseRedirect)): return listing categories, filter_, base, cat = listing if filter_.field == 'up-and-coming': # Almost hardcoding the number of element because performing # `filter_.qs.count()` is a performance killer. We're still # verifying the `base.count()` for the template switch below. base_count = base.count() count = (base_count if base_count < MIN_COUNT_FOR_LANDING else PAGINATE_PERSONAS_BY * settings.PERSONA_DEFAULT_PAGES) else: # Pass the count from base instead of letting it come from # filter_.qs.count() since that would join against personas. count = cat.count if cat else base.count() addons = amo.utils.paginate(request, filter_.qs, PAGINATE_PERSONAS_BY, count=count) if ('sort' not in request.GET and ((request.MOBILE and not cat) or (not request.MOBILE and count > MIN_COUNT_FOR_LANDING))): template += 'category_landing.html' else: template += 'grid.html' if cat: ids = AddonCategory.creatured_random(cat, request.LANG) featured = manual_order(base, ids, pk_name="addons.id") else: ids = Addon.featured_random(request.APP, request.LANG) featured = manual_order(base, ids, pk_name="addons.id") ctx = { 'categories': categories, 'category': cat, 'addons': addons, 'filter': filter_, 'sorting': filter_.field, 'sort_opts': filter_.opts, 'featured': featured, 'search_cat': 'themes', 'is_homepage': cat is None and 'sort' not in request.GET } return render(request, template, ctx)
def personas(request, category=None, template=None): listing = personas_listing(request, category) # I guess this was a Complete Theme after all. if isinstance(listing, (HttpResponsePermanentRedirect, HttpResponseRedirect)): return listing categories, filter_, base, cat = listing if filter_.field == 'up-and-coming': # Almost hardcoding the number of element because performing # `filter_.qs.count()` is a performance killer. We're still # verifying the `base.count()` for the template switch below. base_count = base.count() count = (base_count if base_count < MIN_COUNT_FOR_LANDING else PAGINATE_PERSONAS_BY * settings.PERSONA_DEFAULT_PAGES) else: # Pass the count from base instead of letting it come from # filter_.qs.count() since that would join against personas. count = cat.count if cat else base.count() addons = amo.utils.paginate(request, filter_.qs, PAGINATE_PERSONAS_BY, count=count) if ('sort' not in request.GET and ( (request.MOBILE and not cat) or (not request.MOBILE and count > MIN_COUNT_FOR_LANDING))): template += 'category_landing.html' else: template += 'grid.html' if cat: ids = AddonCategory.creatured_random(cat, request.LANG) featured = manual_order(base, ids, pk_name="addons.id") else: ids = Addon.featured_random(request.APP, request.LANG) featured = manual_order(base, ids, pk_name="addons.id") ctx = {'categories': categories, 'category': cat, 'addons': addons, 'filter': filter_, 'sorting': filter_.field, 'sort_opts': filter_.opts, 'featured': featured, 'search_cat': 'themes', 'is_homepage': cat is None and 'sort' not in request.GET} return render(request, template, ctx)
def process_request(self, list_type='recommended', addon_type='ALL', limit=10, platform='ALL', version=None, compat_mode='strict'): """ Find a list of new or featured add-ons. Filtering is done in Python for cache-friendliness and to avoid heavy queries. """ limit = min(MAX_LIMIT, int(limit)) APP, platform = self.request.APP, platform.lower() qs = Addon.objects.listed(APP) shuffle = True if list_type in ('by_adu', 'featured'): qs = qs.exclude(type=amo.ADDON_PERSONA) if list_type == 'newest': new = date.today() - timedelta(days=NEW_DAYS) addons = (qs.filter( created__gte=new).order_by('-created'))[:limit + BUFFER] elif list_type == 'by_adu': addons = qs.order_by('-average_daily_users')[:limit + BUFFER] shuffle = False # By_adu is an ordered list. elif list_type == 'hotness': # Filter to type=1 so we hit visible_idx. Only extensions have a # hotness index right now so this is not incorrect. addons = (qs.filter( type=amo.ADDON_EXTENSION).order_by('-hotness'))[:limit + BUFFER] shuffle = False else: ids = Addon.featured_random(APP, self.request.LANG) addons = manual_order(qs, ids[:limit + BUFFER], 'addons.id') shuffle = False args = (addon_type, limit, APP, platform, version, compat_mode, shuffle) def f(): return self._process(addons, *args) return cached_with(addons, f, map(smart_str, args))
def process_request(self, list_type='recommended', addon_type='ALL', limit=10, platform='ALL', version=None, compat_mode='strict'): """ Find a list of new or featured add-ons. Filtering is done in Python for cache-friendliness and to avoid heavy queries. """ limit = min(MAX_LIMIT, int(limit)) APP, platform = self.request.APP, platform.lower() qs = Addon.objects.listed(APP) shuffle = True if list_type in ('by_adu', 'featured'): qs = qs.exclude(type=amo.ADDON_PERSONA) if list_type == 'newest': new = date.today() - timedelta(days=NEW_DAYS) addons = (qs.filter(created__gte=new) .order_by('-created'))[:limit + BUFFER] elif list_type == 'by_adu': addons = qs.order_by('-average_daily_users')[:limit + BUFFER] shuffle = False # By_adu is an ordered list. elif list_type == 'hotness': # Filter to type=1 so we hit visible_idx. Only extensions have a # hotness index right now so this is not incorrect. addons = (qs.filter(type=amo.ADDON_EXTENSION) .order_by('-hotness'))[:limit + BUFFER] shuffle = False else: ids = Addon.featured_random(APP, self.request.LANG) addons = manual_order(qs, ids[:limit + BUFFER], 'addons.id') shuffle = False args = (addon_type, limit, APP, platform, version, compat_mode, shuffle) def f(): return self._process(addons, *args) return cached_with(addons, f, map(force_bytes, args))