Skip to content

Commit

Permalink
Don't save settings values at module load time
Browse files Browse the repository at this point in the history
Previously, we'd "cache" the RATELIMIT_ENABLE and CACHE_PREFIX settings
at module load time. That makes it harder to create tests that change
these values.

This fixes that by changing the code so we're not caching the settings
values.

Fixes #38
  • Loading branch information
willkg committed Mar 19, 2014
1 parent fa8d543 commit fab72f9
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions ratelimit/helpers.py
Expand Up @@ -7,9 +7,6 @@

__all__ = ['is_ratelimited']

RATELIMIT_ENABLE = getattr(settings, 'RATELIMIT_ENABLE', True)
CACHE_PREFIX = getattr(settings, 'RATELIMIT_CACHE_PREFIX', 'rl:')

_PERIODS = {
's': 1,
'm': 60,
Expand Down Expand Up @@ -53,7 +50,8 @@ def _get_keys(request, ip=True, field=None, keyfuncs=None):
keyfuncs = [keyfuncs]
for k in keyfuncs:
keys.append(k(request))
return [CACHE_PREFIX + k for k in keys]
prefix = getattr(settings, 'CACHE_PREFIX', 'rl:')
return [prefix + k for k in keys]


def _incr(cache, keys, timeout=60):
Expand Down Expand Up @@ -86,8 +84,9 @@ def is_ratelimited(request, increment=False, ip=True, method=['POST'],
cache = get_cache(cache)

request.limited = getattr(request, 'limited', False)
if (not request.limited and RATELIMIT_ENABLE and
_method_match(request, method)):
if (not request.limited
and getattr(settings, 'RATELIMIT_ENABLE', True)
and _method_match(request, method)):
_keys = _get_keys(request, ip, field, keys)
if increment:
counts = _incr(cache, _keys, period)
Expand Down

0 comments on commit fab72f9

Please sign in to comment.