Skip to content

Commit

Permalink
core.ratelimit validate cache config
Browse files Browse the repository at this point in the history
  • Loading branch information
nitely committed May 19, 2016
1 parent 2fbff19 commit 6c7592c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
24 changes: 24 additions & 0 deletions spirit/core/utils/ratelimit/ratelimit.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,37 @@
'm': 60}


def validate_cache_config():
try:
cache = settings.CACHES[settings.ST_RATELIMIT_CACHE]
except KeyError:
# Django will raise later when using
# this cache so we do nothing
return

# Some third-party backend
# don't have a TIMEOUT option
if (not settings.ST_RATELIMIT_IGNORE_TIMEOUT_WARNING and
cache.get('TIMEOUT', 1) is not None):
# todo: raise ConfigurationError in next version
# todo: warn(
# 'settings.ST_RATELIMIT_CACHE cache's TIMEOUT '
# 'must be None (never expire) and it should '
# 'be other than the default. See spirit.settings. '
# 'To ignore this warning, set '
# 'settings.ST_RATELIMIT_IGNORE_TIMEOUT to True, '
# 'if you know what you are doing.')
pass


class RateLimitError(Exception):
""""""


class RateLimit:

def __init__(self, request, uid, method=None, field=None, rate='5/5m'):
validate_cache_config()
self.request = request
self.uid = uid
self.method = method or ['POST']
Expand Down
7 changes: 7 additions & 0 deletions spirit/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
ST_RATELIMIT_ENABLE = True
ST_RATELIMIT_CACHE_PREFIX = 'srl'
ST_RATELIMIT_CACHE = 'default'
ST_RATELIMIT_IGNORE_TIMEOUT_WARNING = False

ST_NOTIFICATIONS_PER_PAGE = 20

Expand Down Expand Up @@ -88,6 +89,12 @@
},
}

CACHES.update({
'st_rate_limit': {
'BACKEND': CACHES['default']['BACKEND'],
'LOCATION': CACHES['default']['LOCATION'],
'TIMEOUT': None}})

AUTHENTICATION_BACKENDS = [
'spirit.user.auth.backends.UsernameAuthBackend',
'spirit.user.auth.backends.EmailAuthBackend',
Expand Down

0 comments on commit 6c7592c

Please sign in to comment.