def get_queryset(self): try: application_id = AddonAppFilterParam(self.request).get_value() except ValueError: raise ParseError('Invalid app parameter.') types = (amo.ADDON_DICT, amo.ADDON_LPAPP) return Addon.objects.public().filter( appsupport__app=application_id, type__in=types, target_locale__isnull=False).exclude(target_locale='')
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. # AddonCategoryFilterParam parses the request parameters for us to # determine the category. try: category = AddonCategoryFilterParam(self.request).get_value() except ValueError: raise ParseError( 'Invalid app, category and/or type parameter(s).') ids = 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 = AddonAppFilterParam( self.request).get_object_from_reverse_dict() type_ = None if 'type' in self.request.GET: type_ = AddonTypeFilterParam(self.request).get_value() except ValueError: raise ParseError( 'Invalid app, category and/or type parameter(s).') ids = get_featured_ids(app, lang=lang, type=type_) # 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 ParseError('Invalid page_size parameter') ids = ids[:page_size] return manual_order(queryset, ids, 'addons.id')