Пример #1
0
    def get_query_params(self):
        """
        Parse query parameters that this API supports:
        - app (mandatory)
        - type (optional)
        - appversion (optional, makes type mandatory)
        - author (optional)

        Can raise ParseError() in case a mandatory parameter is missing or a
        parameter is invalid.

        Returns a dict containing application (int), types (tuple or None),
        appversions (dict or None) and author (string or None).
        """
        # app parameter is mandatory when calling this API.
        try:
            application = AddonAppQueryParam(self.request.GET).get_value()
        except ValueError:
            raise exceptions.ParseError('Invalid or missing app parameter.')

        # appversion parameter is optional.
        if AddonAppVersionQueryParam.query_param in self.request.GET:
            try:
                value = AddonAppVersionQueryParam(
                    self.request.GET).get_values()
                appversions = {'min': value[1], 'max': value[2]}
            except ValueError:
                raise exceptions.ParseError('Invalid appversion parameter.')
        else:
            appversions = None

        # type is optional, unless appversion is set. That's because the way
        # dicts and language packs have their compatibility info set in the
        # database differs, so to make things simpler for us we force clients
        # to filter by type if they want appversion filtering.
        if AddonTypeQueryParam.query_param in self.request.GET or appversions:
            try:
                addon_types = tuple(
                    AddonTypeQueryParam(self.request.GET).get_values())
            except ValueError:
                raise exceptions.ParseError(
                    'Invalid or missing type parameter while appversion '
                    'parameter is set.')
        else:
            addon_types = (amo.ADDON_LPAPP, amo.ADDON_DICT)

        # author is optional. It's a string representing the username(s) we're
        # filtering on.
        if AddonAuthorQueryParam.query_param in self.request.GET:
            authors = AddonAuthorQueryParam(self.request.GET).get_values()
        else:
            authors = None

        return {
            'application': application,
            'types': addon_types,
            'appversions': appversions,
            'authors': authors,
        }
Пример #2
0
 def get_application_id(self):
     if not hasattr(self, 'application_id'):
         try:
             self.application_id = AddonAppQueryParam(
                 self.request).get_value()
         except ValueError:
             raise exceptions.ParseError('Invalid app parameter.')
     return self.application_id
Пример #3
0
    def get_queryset(self):
        try:
            application_id = AddonAppQueryParam(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='')
Пример #4
0
    def get_query_params(self):
        """
        Parse query parameters that this API supports:
        - app (mandatory)
        - type (optional)
        - appversion (optional, makes type mandatory)

        Can raise ParseError() in case a mandatory parameter is missing or a
        parameter is invalid.

        Returns a tuple with application, addon_types tuple (or None), and
        appversions dict (or None) ready to be consumed by the get_queryset_*()
        methods.
        """
        # app parameter is mandatory when calling this API.
        try:
            application = AddonAppQueryParam(self.request).get_value()
        except ValueError:
            raise exceptions.ParseError('Invalid or missing app parameter.')

        # appversion parameter is optional.
        if AddonAppVersionQueryParam.query_param in self.request.GET:
            try:
                value = AddonAppVersionQueryParam(self.request).get_values()
                appversions = {'min': value[1], 'max': value[2]}
            except ValueError:
                raise exceptions.ParseError('Invalid appversion parameter.')
        else:
            appversions = None

        # type is optional, unless appversion is set. That's because the way
        # dicts and language packs have their compatibility info set in the
        # database differs, so to make things simpler for us we force clients
        # to filter by type if they want appversion filtering.
        if AddonTypeQueryParam.query_param in self.request.GET or appversions:
            try:
                addon_types = tuple(
                    AddonTypeQueryParam(self.request).get_value())
            except ValueError:
                raise exceptions.ParseError(
                    'Invalid or missing type parameter while appversion '
                    'parameter is set.')
        else:
            addon_types = (amo.ADDON_LPAPP, amo.ADDON_DICT)
        return application, addon_types, appversions
Пример #5
0
 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')