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, }
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
def get_current_compatible_version(self, addon): """ Return latest public version compatible with the app & appversion passed through the request, or fall back to addon.current_version if none is found. Only use on langpacks if the appversion parameter is present. """ request = self.context.get('request') try: # AddonAppVersionQueryParam.get_values() returns (app_id, min, max) # but we want {'min': min, 'max': max}. value = AddonAppVersionQueryParam(request).get_values() application = value[0] appversions = dict(zip(('min', 'max'), value[1:])) except ValueError as exc: raise exceptions.ParseError(str(exc)) version_qs = Version.objects.latest_public_compatible_with( application, appversions).filter(addon=addon) return version_qs.first() or addon.current_version