From 8a28da1047e14f378d8476b03d39e0bb91d0b310 Mon Sep 17 00:00:00 2001 From: Arsen Bespalov Date: Sun, 11 Aug 2019 17:52:37 +0800 Subject: [PATCH] Add more flexible rate attribute. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `rate` attribute allow custom seconds, example: `rate = "1/300"` — 1 request per 5 minutes. --- rest_framework/throttling.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/rest_framework/throttling.py b/rest_framework/throttling.py index 0ba2ba66b..2ead32b41 100644 --- a/rest_framework/throttling.py +++ b/rest_framework/throttling.py @@ -103,7 +103,10 @@ class SimpleRateThrottle(BaseThrottle): return (None, None) num, period = rate.split('/') num_requests = int(num) - duration = {'s': 1, 'm': 60, 'h': 3600, 'd': 86400}[period[0]] + if not period.isdigit(): + duration = {'s': 1, 'm': 60, 'h': 3600, 'd': 86400}[period[0]] + else: + duration = int(period) return (num_requests, duration) def allow_request(self, request, view):