Ejemplo n.º 1
0
def logger():
    """
    :return: simple logger object.
    """
    global DebugLogger
    if not DebugLogger:
        proj = get_setting('LOG_PROJECT_CODE')
        logfile = get_setting('LOG_FILE')
        logfmt = get_setting('LOG_FORMAT')
        DebugLogger = create_log(logname=proj, logfile=logfile, format=logfmt)
    return DebugLogger
Ejemplo n.º 2
0
def logger():
    """
    :return: simple logger object.
    """
    from garage import get_setting
    global DebugLogger
    if not DebugLogger:
        proj = get_setting('LOG_PROJECT_CODE')
        logfile = get_setting('LOG_FILE')
        logfmt = get_setting('LOG_FORMAT')
        DebugLogger = create_log(logname=proj, logfile=logfile, format=logfmt)
    return DebugLogger
Ejemplo n.º 3
0
def cache_data(cache_key='', timeout=get_setting('OBJECT_CACHE_TIMEOUT')):
    """
    Decorator to cache objects.
    """
    def decorator(f):
        def _cache_controller(*args, **kwargs):
            if not get_setting('USE_MINI_CACHE'):
                return f(*args, **kwargs)
            if isinstance(cache_key, basestring):
                k = cache_key % locals()
            elif callable(cache_key):
                k = cache_key(*args, **kwargs)
            result = cache.get(k)
            if result is None:
                result = f(*args, **kwargs)
                cache.set(k, result, timeout)
                if get_setting('CACHE_DEBUG'):
                    logger().debug('Cached data: %s' % k)
            else:
                if get_setting('CACHE_DEBUG'):
                    logger().debug('Return cached data: %s' % k)
            return result

        return _cache_controller

    return decorator
Ejemplo n.º 4
0
def get_slug_iteration_separator():
    """
    The slug iteration separator is used to mark entries with the same slug base.

    e.g. article (first entry)
         article--2 (second entry)
    """
    return get_setting('SLUG_ITERATION_SEPARATOR', SLUG_ITERATION_SEPARATOR)
Ejemplo n.º 5
0
def delete_cache(cache_key):
    """
    Delete cached object.
    """
    if not get_setting('USE_MINI_CACHE'):
        return False
    if cache.get(cache_key):
        cache.set(cache_key, None, 0)
        result = True
    else:
        result = False
    if get_setting('CACHE_DEBUG'):
        if result is True:
            logger().debug('delete_cache: Deleting cached data: %s' % cache_key)
        else:
            logger().debug('delete_cache: Unable to get cached data: %s' % cache_key)
    return result
Ejemplo n.º 6
0
 def _cache_controller(*args, **kwargs):
     if not get_setting('USE_MINI_CACHE'):
         return f(*args, **kwargs)
     if isinstance(cache_key, basestring):
         k = cache_key % locals()
     elif callable(cache_key):
         k = cache_key(*args, **kwargs)
     result = cache.get(k)
     if result is None:
         result = f(*args, **kwargs)
         cache.set(k, result, timeout)
         if get_setting('CACHE_DEBUG'):
             logger().debug('Cached data: %s' % k)
     else:
         if get_setting('CACHE_DEBUG'):
             logger().debug('Return cached data: %s' % k)
     return result
Ejemplo n.º 7
0
def get_slug_iteration_separator():
    """
    The slug iteration separator is used to mark entries with the same slug base.

    e.g. article (first entry)
         article--2 (second entry)
    """
    from garage import get_setting
    return get_setting('SLUG_ITERATION_SEPARATOR', SLUG_ITERATION_SEPARATOR)
Ejemplo n.º 8
0
def cache_key(s, *args):
    try:
        from django.contrib.sites.models import Site
        current_site = Site.objects.get_current()
        site_id = current_site.id
    except (ImportError, AttributeError):
        site_id = get_setting('SITE_ID', 1)
    s = [s]
    s.extend([a for a in args])
    return create_cache_key('_'.join(s), prefix='%s_' % str(site_id))
Ejemplo n.º 9
0
def get_local_tz():
    """
    Get local timezone.

    :returns: local timezone object
    """
    from django.utils.timezone import get_current_timezone_name
    from garage import get_setting
    local_timezone = get_setting('TIME_ZONE')
    if not local_timezone:
        local_timezone = get_current_timezone_name()
    local_tz = pytz.timezone(local_timezone)
    return local_tz
Ejemplo n.º 10
0
def batch_qs(qs, batch_size=get_setting('QS_BATCH_SIZE', QS_BATCH_SIZE)):
    """
    Returns a (start, end, total, queryset) tuple for each batch in the given
    queryset.

    Usage:
    # Make sure to order your querset
    article_qs = Article.objects.order_by('id')
    for start, end, total, qs in batch_qs(article_qs):
    print "Now processing %s - %s of %s" % (start + 1, end, total)
    for article in qs:
    print article.body
    """
    total = qs.count()
    for start in range(0, total, batch_size):
        end = min(start + batch_size, total)
        yield (start, end, total, qs[start:end])
Ejemplo n.º 11
0
def get_slug_separator():
    return get_setting('SLUG_SEPARATOR', SLUG_SEPARATOR)
Ejemplo n.º 12
0
def get_slug_separator():
    from garage import get_setting
    return get_setting('SLUG_SEPARATOR', SLUG_SEPARATOR)