From ddaa49ecee5feb63db4c32cd6443a4f591ac9bb0 Mon Sep 17 00:00:00 2001 From: Matthew Somerville Date: Wed, 13 Mar 2024 16:10:31 +0000 Subject: [PATCH] Add optional external rate limit file. --- mapit/ratelimitcache.py | 5 +++++ project/settings.py | 8 ++++++++ 2 files changed, 13 insertions(+) diff --git a/mapit/ratelimitcache.py b/mapit/ratelimitcache.py index 03e68b645..93b61d795 100644 --- a/mapit/ratelimitcache.py +++ b/mapit/ratelimitcache.py @@ -14,6 +14,11 @@ } _sentinel = object() +CONFIG_FILE = getattr(settings, 'MAPIT_RATE_LIMIT_FILE', {}) +if isinstance(CONFIG_FILE, dict) and CONFIG_FILE.get('internal_ips'): + CONFIG['ips'] += CONFIG_FILE['internal_ips']['ipv4'] + CONFIG['ips'] += CONFIG_FILE['internal_ips']['ipv6'] + def ratelimit(_f=_sentinel, **kwargs): if _f is _sentinel: diff --git a/project/settings.py b/project/settings.py index 1b9dbb60d..528ae8965 100644 --- a/project/settings.py +++ b/project/settings.py @@ -37,6 +37,14 @@ # excluded from rate limiting. Optional. MAPIT_RATE_LIMIT = config.get('RATE_LIMIT', {}) +MAPIT_RATE_LIMIT_FILE = config.get('RATE_LIMIT_FILE') +if MAPIT_RATE_LIMIT_FILE: + try: + with open(MAPIT_RATE_LIMIT_FILE, 'r') as fp: + MAPIT_RATE_LIMIT_FILE = yaml.load(fp, Loader=yaml.SafeLoader) + except: + MAPIT_RATE_LIMIT_FILE = None + # A GA code for analytics GOOGLE_ANALYTICS = config.get('GOOGLE_ANALYTICS', '')