def c(x, y): x = convert_version(x) y = convert_version(y) if (x > y): return 1 elif (x < y): return - 1 return 0
def c(x, y): x = convert_version(x) y = convert_version(y) if (x > y): return 1 elif (x < y): return -1 return 0
def addon_filter(addons, addon_type, limit, app, platform, version, compat_mode='strict', shuffle=True): """ Filter addons by type, application, app version, and platform. Add-ons that support the current locale will be sorted to front of list. Shuffling will be applied to the add-ons supporting the locale and the others separately. Doing this in the database takes too long, so we in code and wrap it in generous caching. """ APP = app if addon_type.upper() != 'ALL': try: addon_type = int(addon_type) if addon_type: addons = [a for a in addons if a.type == addon_type] except ValueError: # `addon_type` is ALL or a type id. Otherwise we ignore it. pass # Take out personas since they don't have versions. groups = dict(partition(addons, lambda x: x.type == amo.ADDON_PERSONA)) personas, addons = groups.get(True, []), groups.get(False, []) platform = platform.lower() if platform != 'all' and platform in amo.PLATFORM_DICT: pid = amo.PLATFORM_DICT[platform] f = lambda ps: pid in ps or amo.PLATFORM_ALL in ps addons = [a for a in addons if f(a.current_version.supported_platforms)] if version is not None: v = search_utils.convert_version(version) f = lambda app: app.min.version_int <= v <= app.max.version_int xs = [(a, a.compatible_apps) for a in addons] addons = [a for a, apps in xs if apps.get(APP) and f(apps[APP])] # Put personas back in. addons.extend(personas) # We prefer add-ons that support the current locale. lang = translation.get_language() partitioner = lambda x: (x.description and (x.description.locale == lang)) groups = dict(partition(addons, partitioner)) good, others = groups.get(True, []), groups.get(False, []) if shuffle: random.shuffle(good) random.shuffle(others) if len(good) < limit: good.extend(others[:limit - len(good)]) return good[:limit]
def addon_filter(addons, addon_type, limit, app, platform, version, shuffle=True): """ Filter addons by type, application, app version, and platform. Add-ons that support the current locale will be sorted to front of list. Shuffling will be applied to the add-ons supporting the locale and the others separately. Doing this in the database takes too long, so we in code and wrap it in generous caching. """ APP = app if addon_type.upper() != 'ALL': try: addon_type = int(addon_type) if addon_type: addons = [a for a in addons if a.type == addon_type] except ValueError: # `addon_type` is ALL or a type id. Otherwise we ignore it. pass # Take out personas since they don't have versions. groups = dict(partition(addons, lambda x: x.type == amo.ADDON_PERSONA)) personas, addons = groups.get(True, []), groups.get(False, []) platform = platform.lower() if platform != 'all' and platform in amo.PLATFORM_DICT: pid = amo.PLATFORM_DICT[platform] f = lambda ps: pid in ps or amo.PLATFORM_ALL in ps addons = [a for a in addons if f(a.current_version.supported_platforms)] if version is not None: v = search_utils.convert_version(version) f = lambda app: app.min.version_int <= v <= app.max.version_int xs = [(a, a.compatible_apps) for a in addons] addons = [a for a, apps in xs if apps.get(APP) and f(apps[APP])] # Put personas back in. addons.extend(personas) # We prefer add-ons that support the current locale. lang = translation.get_language() partitioner = lambda x: (x.description and (x.description.locale == lang)) groups = dict(partition(addons, partitioner)) good, others = groups.get(True, []), groups.get(False, []) if shuffle: random.shuffle(good) random.shuffle(others) if len(good) < limit: good.extend(others[:limit - len(good)]) return good[:limit]
def compatible_with_app(self, app, version=None): """ Returns addons compatible with specific applcications and optionally specific versions of said application. E.g. amo.FIREFOX and '3.5' """ qs = self.filter( versions__applicationsversions__min__application=app.id) if version is not None: version_int = search_utils.convert_version(version) qs = qs.filter( versions__applicationsversions__min__version_int__lte= version_int, versions__applicationsversions__max__version_int__gte= version_int).distinct() return qs.distinct()