예제 #1
0
파일: stats.py 프로젝트: chop-dbhi/serrano
def get_count(request, model, refresh, processor, context):
    opts = model._meta

    # Build a queryset through the context which is toggled by
    # the parameter.
    processor = processor(context=context, tree=model)
    queryset = processor.get_queryset(request=request)

    # Get count from cache or database
    label = ':'.join([opts.app_label, opts.module_name, 'count'])
    key = cache_key(label, kwargs={'queryset': queryset})

    cache = get_cache(avocado_settings.DATA_CACHE)

    if refresh:
        count = None
    else:
        count = cache.get(key)

    if count is None:
        count = queryset.values('pk').distinct().count()
        cache.set(key, count, timeout=NEVER_EXPIRE)

    # Close the connection in the thread to prevent 'idle in transaction'
    # situtations.
    from django.db import connection
    connection.close()

    return count
예제 #2
0
    def get_paginator(self, queryset, limit):
        paginator = Paginator(queryset, per_page=limit)
        paginator.has_limit = bool(limit)

        # Cache count for paginator to prevent redundant calls between requests
        if settings.DATA_CACHE_ENABLED:
            key = cache_key('paginator', kwargs={
                'queryset': queryset
            })

            count = cache.get(key)

            if count is None:
                count = _count(queryset)
                cache.set(key, count)
        else:
            count = _count(queryset)

        paginator._count = count

        if not limit:
            # Prevent division by zero error in case count is zero
            paginator.per_page = max(count, 1)

        return paginator
예제 #3
0
def get_count(request, model, refresh, processor, context):
    opts = model._meta

    # Build a queryset through the context which is toggled by
    # the parameter.
    processor = processor(context=context, tree=model)
    queryset = processor.get_queryset(request=request)

    # Get count from cache or database
    label = ':'.join([opts.app_label, opts.module_name, 'count'])
    key = cache_key(label, kwargs={'queryset': queryset})

    cache = get_cache(avocado_settings.DATA_CACHE)

    if refresh:
        count = None
    else:
        count = cache.get(key)

    if count is None:
        count = queryset.values('pk').distinct().count()
        cache.set(key, count, timeout=NEVER_EXPIRE)

    # Close the connection in the thread to prevent 'idle in transaction'
    # situtations.
    from django.db import connection
    connection.close()

    return count
예제 #4
0
    def get_paginator(self, queryset, limit):
        paginator = Paginator(queryset, per_page=limit)
        paginator.has_limit = bool(limit)

        # Cache count for paginator to prevent redundant calls between requests
        if settings.DATA_CACHE_ENABLED:
            key = cache_key('paginator', kwargs={'queryset': queryset})

            count = cache.get(key)

            if count is None:
                count = _count(queryset)
                cache.set(key, count)
        else:
            count = _count(queryset)

        paginator._count = count

        if not limit:
            # Prevent division by zero error in case count is zero
            paginator.per_page = max(count, 1)

        return paginator