def personas(request, category=None, template=None): categories, filter, base, category = personas_listing(request, category) # Pass the count from base instead of letting it come from # filter.qs.count() since that would join against personas. count = category.count if category else base.count() if ('sort' not in request.GET and ((request.MOBILE and not category) or (not request.MOBILE and count > 4))): template += 'category_landing.html' else: template += 'grid.html' addons = amo.utils.paginate(request, filter.qs, 30, count=count) if category: ids = AddonCategory.creatured_random(category, 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': category, 'addons': addons, 'filter': filter, 'sorting': filter.field, 'sort_opts': filter.opts, 'featured': featured, 'search_cat': 'personas', 'is_homepage': category is None and 'sort' not in request.GET } return jingo.render(request, template, ctx)
def process_request(self, list_type='recommended', addon_type='ALL', limit=10, platform='ALL', version=None): """ 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, shuffle) f = lambda: self._process(addons, *args) return cached_with(addons, f, map(encoding.smart_str, args))
def personas(request, category=None): categories, filter, base, category = personas_listing(request, category) # Pass the count from base instead of letting it come from # filter.qs.count() since that would join against personas. count = category.count if category else base.count() if 'sort' in request.GET or count < 5: template = 'grid.html' else: template = 'category_landing.html' addons = amo.utils.paginate(request, filter.qs, 30, count=count) if category: ids = AddonCategory.creatured_random(category, 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") is_homepage = category is None and 'sort' not in request.GET return jingo.render(request, 'browse/personas/' + template, {'categories': categories, 'category': category, 'filter': filter, 'addons': addons, 'featured': featured, 'is_homepage': is_homepage, 'search_cat': 'personas'})
def personas(request, category=None, template=None): categories, filter, base, category = personas_listing(request, category) # Pass the count from base instead of letting it come from # filter.qs.count() since that would join against personas. count = category.count if category else base.count() if "sort" not in request.GET and ((request.MOBILE and not category) or (not request.MOBILE and count > 4)): template += "category_landing.html" else: template += "grid.html" addons = amo.utils.paginate(request, filter.qs, 30, count=count) if category: ids = AddonCategory.creatured_random(category, 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": category, "addons": addons, "filter": filter, "sorting": filter.field, "sort_opts": filter.opts, "featured": featured, "search_cat": "personas", "is_homepage": category is None and "sort" not in request.GET, } return jingo.render(request, template, ctx)
def personas(request, category=None, template=None): categories, filter, base, category = personas_listing(request, category) # Pass the count from base instead of letting it come from # filter.qs.count() since that would join against personas. count = category.count if category else base.count() if ('sort' not in request.GET and ((request.MOBILE and not category) or (not request.MOBILE and count > 4))): template += 'category_landing.html' else: template += 'grid.html' addons = amo.utils.paginate(request, filter.qs, 30, count=count) if category: ids = AddonCategory.creatured_random(category, 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': category, 'addons': addons, 'filter': filter, 'sorting': filter.field, 'sort_opts': filter.opts, 'featured': featured, 'search_cat': 'personas', 'is_homepage': category is None and 'sort' not in request.GET} return jingo.render(request, template, ctx)
def test_featured_duplicated(self): another = Addon.objects.get(id=1003) self.change_addon(another, 'en-US') another.feature_set.create(application_id=amo.FIREFOX.id, locale=None, start=datetime.today(), end=datetime.today()) eq_(Addon.featured_random(amo.FIREFOX, 'en-US').count(1003), 1)
def test_featured_duplicated(self): another = Addon.objects.get(id=1003) self.change_addon(another, 'en-US') another.feature_set.create(application_id=amo.FIREFOX.id, locale=None, start=datetime.datetime.today(), end=datetime.datetime.today()) eq_(Addon.featured_random(amo.FIREFOX, 'en-US').count(1003), 1)
def test_featured_ids(self): 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. eq_([1003, 3481], sorted(items[0:2])) eq_([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['filter'].filter('featured')] eq_(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 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['filter'].filter('featured')] eq_(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 get(self, request, api_version, list_type='recommended', addon_type='ALL', limit=10, platform='ALL', version=None, compat_mode='strict', format=None): """ 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).exclude(type=amo.ADDON_WEBAPP) 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) response = self.create_response(addon_filter(addons, *args)) # Storing addons within the response to be able to use it later # in `self.finalize_response()` as a cache key. response.addons = addons return response
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(encoding.smart_str, args))
def get(self, request, api_version, list_type='recommended', addon_type='ALL', limit=10, platform='ALL', version=None, compat_mode='strict', format=None): """ 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).exclude(type=amo.ADDON_WEBAPP) 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) response = self.create_response(addon_filter(addons, *args)) # Storing addons within the response to be able to use it later # in `self.finalize_response()` as a cache key. response.addons = addons return response
def featured(request, category=None, template=None): ids = Addon.featured_random(request.APP, request.LANG) addons = manual_order(Addon.objects.exclude(type=amo.ADDON_PERSONA), ids) return jingo.render(request, template, {'addons': addons})