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 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_ordering(self): """Given a specific set of primary keys, assure that we return addons in that order.""" semi_arbitrary_order = [40, 5299, 3615] addons = amo_models.manual_order( Addon.objects.all(), semi_arbitrary_order) assert semi_arbitrary_order == [addon.id for addon in addons]
def test_ordering(self): """Given a specific set of primary keys, assure that we return addons in that order.""" semi_arbitrary_order = [40, 5299, 3615] addons = amo_models.manual_order(Addon.objects.all(), semi_arbitrary_order) assert semi_arbitrary_order == [addon.id for addon in addons]
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 creatured(request, category): TYPE = amo.ADDON_EXTENSION q = Category.objects.filter(application=request.APP.id, type=TYPE) category = get_object_or_404(q, slug=category) ids = AddonCategory.creatured_random(category, request.LANG) addons = manual_order(Addon.objects.public(), ids, pk_name='addons.id') return render(request, 'browse/creatured.html', {'addons': addons, 'category': category, 'sorting': 'featured'})
def filter_featured(self): # Featured search add-ons in all locales: APP, LANG = self.request.APP, self.request.LANG ids = get_featured_ids(APP, LANG, amo.ADDON_SEARCH) try: search_cat = Category.objects.get(slug='search-tools', application=APP.id) others = get_creatured_ids(search_cat, LANG) ids.extend(o for o in others if o not in ids) except Category.DoesNotExist: pass return manual_order(Addon.objects.valid(), ids, 'addons.id')
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 filter_queryset(self, queryset): # We can pass the optional lang parameter to either get_creatured_ids() # or get_featured_ids() below to get locale-specific results in # addition to the generic ones. lang = self.request.GET.get('lang') if 'category' in self.request.GET: # If a category is passed then the app and type parameters are # mandatory because we need to find a category in the constants to # pass to get_creatured_ids(), and category slugs are not unique. # AddonCategoryQueryParam parses the request parameters for us to # determine the category. try: categories = AddonCategoryQueryParam(self.request).get_value() except ValueError: raise exceptions.ParseError( 'Invalid app, category and/or type parameter(s).') ids = [] for category in categories: ids.extend(get_creatured_ids(category, lang)) else: # If no category is passed, only the app parameter is mandatory, # because get_featured_ids() needs it to find the right collection # to pick addons from. It can optionally filter by type, so we # parse request for that as well. try: app = AddonAppQueryParam( self.request).get_object_from_reverse_dict() types = None if 'type' in self.request.GET: types = AddonTypeQueryParam(self.request).get_value() except ValueError: raise exceptions.ParseError( 'Invalid app, category and/or type parameter(s).') ids = get_featured_ids(app, lang=lang, types=types) # ids is going to be a random list of ids, we just slice it to get # the number of add-ons that was requested. We do it before calling # manual_order(), since it'll use the ids as part of a id__in filter. try: page_size = int( self.request.GET.get('page_size', api_settings.PAGE_SIZE)) except ValueError: raise exceptions.ParseError('Invalid page_size parameter') ids = ids[:page_size] return manual_order(queryset, ids, 'addons.id')
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))
def get_featured_personas(request, category=None, num_personas=6): categories, filter, base, category = personas_listing(request, category) ids = get_featured_ids(request.APP, request.LANG, type=amo.ADDON_PERSONA) return manual_order(base, ids, 'addons.id')[:num_personas]
def filter_featured(self): ids = self.model.featured_random(self.request.APP, self.request.LANG) return manual_order(self.base_queryset, ids, 'addons.id')
def filter_featured(self): qs = self.base_queryset.all() return manual_order(qs, self.ids, pk_name='addons.id')