nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: jazzband/django-axes
Pull request #770 (2021-08-17 18:22:33)

Add ability to change status code
Am adding the ability to change the http status code returned when the retry limit is reached. Have left the default as `403` and added a test and documentation for the change.

Use case would be something like wanting to return `429` instead.


Patch:
diff --git a/axes/conf.py b/axes/conf.py
index 4747f1b..67dc2d0 100644
--- a/axes/conf.py
+++ b/axes/conf.py
@@ -129,3 +129,6 @@ settings.AXES_SENSITIVE_PARAMETERS = getattr(
 # set the callable for the readable string that can be used in
 # e.g. logging to distinguish client requests
 settings.AXES_CLIENT_STR_CALLABLE = getattr(settings, "AXES_CLIENT_STR_CALLABLE", None)
+
+# set the HTTP response code given by too many requests
+settings.AXES_HTTP_RESPONSE_CODE = getattr(settings, "AXES_HTTP_RESPONSE_CODE", 403)
diff --git a/axes/helpers.py b/axes/helpers.py
index dff7f18..4fa0154 100644
--- a/axes/helpers.py
+++ b/axes/helpers.py
@@ -362,7 +362,7 @@ def get_lockout_response(request, credentials: dict = None) -> HttpResponse:
             "settings.AXES_LOCKOUT_CALLABLE needs to be a string, callable, or None."
         )
 
-    status = 403
+    status = settings.AXES_HTTP_RESPONSE_CODE
     context = {
         "failure_limit": get_failure_limit(request, credentials),
         "username": get_client_username(request, credentials) or "",
diff --git a/docs/4_configuration.rst b/docs/4_configuration.rst
index 01e53c1..ef7cfa3 100644
--- a/docs/4_configuration.rst
+++ b/docs/4_configuration.rst
@@ -114,6 +114,10 @@ The following ``settings.py`` options are available for customizing Axes behavio
   Default: ``False``
 * ``AXES_ALLOWED_CORS_ORIGINS``: Configures lockout response CORS headers for XHR requests.
   Default: ``*``
+* ``AXES_HTTP_RESPONSE_CODE``: Sets the http response code returned when ``AXES_FAILURE_LIMIT`` is
+  reached.
+  For example: ``AXES_HTTP_RESPONSE_CODE = 429``
+  Default: ``403``
 
 The configuration option precedences for the access attempt monitoring are:
 
diff --git a/requirements.txt b/requirements.txt
index 8929a44..1429b2f 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -2,4 +2,4 @@
 -r requirements-qa.txt
 -r requirements-test.txt
 sphinx_rtd_theme==0.5.2
-tox==3.24.1
+tox==3.24.2


Test patch:
diff --git a/tests/test_helpers.py b/tests/test_helpers.py
index d2f02af..ffac679 100644
--- a/tests/test_helpers.py
+++ b/tests/test_helpers.py
@@ -677,6 +677,11 @@ class AxesLockoutTestCase(AxesTestCase):
         response = get_lockout_response(self.request, self.credentials)
         self.assertEqual(403, response.status_code)
 
+    @override_settings(AXES_HTTP_RESPONSE_CODE=429)
+    def test_get_lockout_response_with_custom_http_response_code(self):
+        response = get_lockout_response(self.request, self.credentials)
+        self.assertEqual(429, response.status_code)
+
     @override_settings(AXES_LOCKOUT_CALLABLE=mock_get_lockout_response)
     def test_get_lockout_response_override_callable(self):
         response = get_lockout_response(self.request, self.credentials)