コード例 #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
ファイル: tasks.py プロジェクト: piyushmittal25/addons-server
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
ファイル: views.py プロジェクト: vparfonov/addons-server
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
ファイル: tasks.py プロジェクト: vparfonov/addons-server
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
ファイル: views.py プロジェクト: piyushmittal25/addons-server
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
ファイル: views.py プロジェクト: piyushmittal25/addons-server
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
ファイル: views.py プロジェクト: vparfonov/addons-server
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
ファイル: models.py プロジェクト: rebmullin/addons-server
    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
ファイル: modules.py プロジェクト: vparfonov/addons-server
    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
ファイル: views.py プロジェクト: piyushmittal25/addons-server
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)