Пример #1
0
def test_cached():
    def some_function():
        some_function.call_count += 1
        return 'something'  # Needed for cached() to work.

    some_function.call_count = 0

    cached(some_function, 'my-key')
    cached(some_function, 'my-key')

    assert some_function.call_count == 1
Пример #2
0
def addon_bayesian_rating(*addons, **kw):
    def addon_aggregates():
        return Addon.objects.valid().aggregate(rating=Avg('average_rating'),
                                               reviews=Avg('total_ratings'))

    log.info('[%s@%s] Updating bayesian ratings.' %
             (len(addons), addon_bayesian_rating.rate_limit))

    avg = cached(addon_aggregates, 'task.bayes.avg', 60 * 60 * 60)
    # Rating can be NULL in the DB, so don't update it if it's not there.
    if avg['rating'] is None:
        return

    mc = avg['reviews'] * avg['rating']

    for addon in Addon.objects.no_cache().filter(id__in=addons):
        if addon.average_rating is None:
            # Ignoring addons with no average rating.
            continue

        # Update the addon bayesian_rating atomically using F objects (unless
        # it has no reviews, in which case directly set it to 0).
        qs = Addon.objects.filter(id=addon.id)
        if addon.total_ratings:
            num = mc + F('total_ratings') * F('average_rating')
            denom = avg['reviews'] + F('total_ratings')
            qs.update(bayesian_rating=num / denom)
        else:
            qs.update(bayesian_rating=0)
Пример #3
0
def version_detail(request, addon, version_num):
    qs = _version_list_qs(addon)

    def f():
        return _find_version_page(qs, addon, version_num)

    return cached(f, u'version-detail:{}:{}'.format(addon.id, version_num))
Пример #4
0
def addon_bayesian_rating(*addons, **kw):
    def addon_aggregates():
        return Addon.objects.valid().aggregate(rating=Avg('average_rating'),
                                               reviews=Avg('total_ratings'))

    log.info('[%s@%s] Updating bayesian ratings.' %
             (len(addons), addon_bayesian_rating.rate_limit))

    avg = cached(addon_aggregates, 'task.bayes.avg', 60 * 60 * 60)
    # Rating can be NULL in the DB, so don't update it if it's not there.
    if avg['rating'] is None:
        return

    mc = avg['reviews'] * avg['rating']

    for addon in Addon.objects.no_cache().filter(id__in=addons):
        if addon.average_rating is None:
            # Ignoring addons with no average rating.
            continue

        # Update the addon bayesian_rating atomically using F objects (unless
        # it has no reviews, in which case directly set it to 0).
        qs = Addon.objects.filter(id=addon.id)
        if addon.total_ratings:
            num = mc + F('total_ratings') * F('average_rating')
            denom = avg['reviews'] + F('total_ratings')
            qs.update(bayesian_rating=num / denom)
        else:
            qs.update(bayesian_rating=0)
Пример #5
0
def version_detail(request, addon, version_num):
    qs = _version_list_qs(addon)

    def f():
        return _find_version_page(qs, addon, version_num)

    return cached(f, u'version-detail:{}:{}'.format(addon.id, version_num))
Пример #6
0
def get_versions(order=('application', 'version_int')):
    def fetch_versions():
        apps = amo.APP_USAGE
        versions = dict((app.id, []) for app in apps)
        qs = list(AppVersion.objects.order_by(*order)
                  .filter(application__in=versions)
                  .values_list('application', 'version'))
        for app, version in qs:
            versions[app].append(version)
        return apps, versions
    return cached(fetch_versions, 'getv' + ''.join(order))
Пример #7
0
def get_versions(order=('application', 'version_int')):
    def fetch_versions():
        apps = amo.APP_USAGE
        versions = dict((app.id, []) for app in apps)
        qs = list(
            AppVersion.objects.order_by(*order).filter(
                application__in=versions).values_list('application',
                                                      'version'))
        for app, version in qs:
            versions[app].append(version)
        return apps, versions

    return cached(fetch_versions, 'getv' + ''.join(order))
Пример #8
0
    def get_files(self):
        """
        Returns an OrderedDict, ordered by the filename of all the files in the
        addon-file. Full of all the useful information you'll need to serve
        this file, build templates etc.
        """
        if self._files:
            return self._files

        if not self.is_extracted():
            extract_file(self)

        self._files = cached(self._get_files, self._cache_key())
        return self._files
Пример #9
0
    def blocked(cls, name):
        """
        Check to see if a given name is in the (cached) deny list.
        Return True if the name contains one of the denied terms.

        """
        name = name.lower()
        qs = cls.objects.all()

        def fetch_names():
            return [n.lower() for n in qs.values_list('name', flat=True)]

        blocked_list = cached(fetch_names, 'denied-name:blocked')
        return any(n in name for n in blocked_list)
Пример #10
0
    def blocked(cls, name):
        """
        Check to see if a given name is in the (cached) deny list.
        Return True if the name contains one of the denied terms.

        """
        name = name.lower()
        qs = cls.objects.all()

        def fetch_names():
            return [n.lower() for n in qs.values_list('name', flat=True)]

        blocked_list = cached(fetch_names, 'denied-name:blocked')
        return any(n in name for n in blocked_list)
Пример #11
0
def get_versions(order=('application', 'version_int')):
    def fetch_versions():
        if waffle.switch_is_active('disallow-thunderbird-and-seamonkey'):
            apps = amo.APP_USAGE_FIREFOXES_ONLY
        else:
            apps = amo.APP_USAGE
        versions = {app.id: [] for app in apps}
        qs = list(
            AppVersion.objects.order_by(*order).filter(
                application__in=versions).values_list('application',
                                                      'version'))
        for app, version in qs:
            versions[app].append(version)
        return apps, versions

    return cached(fetch_versions, 'getv' + ''.join(order))
Пример #12
0
    def get_addons(self):
        addons = self.collection.addons.public()
        kw = {
            'addon_type': 'ALL',
            'limit': self.limit,
            'app': self.request.APP,
            'platform': self.platform,
            'version': self.version,
            'compat_mode': self.compat_mode
        }

        def fetch_and_filter_addons():
            return addon_filter(addons, **kw)

        return cached(
            fetch_and_filter_addons,
            'collections-promo-get-addons:{}'.format(repr(kw)))
Пример #13
0
def site_nav(context):
    app = context['request'].APP.id
    return cached(lambda: _site_nav(context), 'site-nav-%s' % app)
Пример #14
0
def side_nav(context, addon_type, category=None):
    app = context['request'].APP.id
    cat = str(category.id) if category else 'all'
    return cached(
        lambda: _side_nav(context, addon_type, category),
        'side-nav-%s-%s-%s' % (app, addon_type, cat))
Пример #15
0
def _category_personas(qs, limit):
    def fetch_personas():
        return randslice(qs, limit=limit)

    key = 'cat-personas:' + qs.query_key()
    return cached(fetch_personas, key)
Пример #16
0
def side_nav(context, addon_type, category=None):
    app = context['request'].APP.id
    cat = str(category.id) if category else 'all'
    return cached(
        lambda: _side_nav(context, addon_type, category),
        'side-nav-%s-%s-%s' % (app, addon_type, cat))
Пример #17
0
def _category_personas(qs, limit):
    def fetch_personas():
        return randslice(qs, limit=limit)
    key = 'cat-personas:' + qs.query_key()
    return cached(fetch_personas, key)
Пример #18
0
def site_nav(context):
    app = context['request'].APP.id
    return cached(lambda: _site_nav(context), 'site-nav-%s' % app)