Skip to content

Commit

Permalink
- Renamed MAINTENANCE_MODE_GET_TEMPLATE_CONTEXT setting to `MAINTEN…
Browse files Browse the repository at this point in the history
…ANCE_MODE_GET_CONTEXT`.
  • Loading branch information
fabiocaccamo committed Dec 11, 2023
1 parent ab1fcf6 commit cccaabf
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 32 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ Retrieve user's real IP address using [`django-ipware`](https://github.com/un33k
MAINTENANCE_MODE_GET_CLIENT_IP_ADDRESS = "ipware.ip.get_ip"
```

```python
# the path of the function that will return the response context -> 'myapp.mymodule.myfunction'
MAINTENANCE_MODE_GET_CONTEXT = None
```

```python
# list of urls that will not be affected by the maintenance-mode
# urls will be used to compile regular expressions objects
Expand Down Expand Up @@ -145,11 +150,6 @@ MAINTENANCE_MODE_RESPONSE_TYPE = "html"
MAINTENANCE_MODE_TEMPLATE = "503.html"
```

```python
# the path of the function that will return the template context -> 'myapp.mymodule.myfunction'
MAINTENANCE_MODE_GET_TEMPLATE_CONTEXT = None
```

```python
# the HTTP status code to send
MAINTENANCE_MODE_STATUS_CODE = 503
Expand Down
7 changes: 3 additions & 4 deletions maintenance_mode/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@

def get_maintenance_response_context(request):
context = {}
if settings.MAINTENANCE_MODE_GET_TEMPLATE_CONTEXT:
if settings.MAINTENANCE_MODE_GET_CONTEXT:
try:
get_request_context_func = import_string(
settings.MAINTENANCE_MODE_GET_TEMPLATE_CONTEXT
settings.MAINTENANCE_MODE_GET_CONTEXT
)
except ImportError as error:
raise ImproperlyConfigured(
"settings.MAINTENANCE_MODE_GET_TEMPLATE_CONTEXT "
"is not a valid function path."
"settings.MAINTENANCE_MODE_GET_CONTEXT is not a valid function path."
) from error

context = get_request_context_func(request=request)
Expand Down
36 changes: 13 additions & 23 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def get_client_ip_address(request):


def get_template_context(request):
return {"TEST_MAINTENANCE_MODE_GET_TEMPLATE_CONTEXT": True}
return {"TEST_MAINTENANCE_MODE_GET_CONTEXT": True}


@override_settings(
Expand Down Expand Up @@ -961,44 +961,38 @@ def test_middleware_get_template_context(self):

settings.MAINTENANCE_MODE = True

settings.MAINTENANCE_MODE_GET_TEMPLATE_CONTEXT = (
"tests.tests.get_template_context"
)
settings.MAINTENANCE_MODE_GET_CONTEXT = "tests.tests.get_template_context"
response = self.client.get("/")
val = response.context.get("TEST_MAINTENANCE_MODE_GET_TEMPLATE_CONTEXT", False)
val = response.context.get("TEST_MAINTENANCE_MODE_GET_CONTEXT", False)
self.assertTrue(val)

get_template_context_error = False
try:
settings.MAINTENANCE_MODE_GET_TEMPLATE_CONTEXT = (
settings.MAINTENANCE_MODE_GET_CONTEXT = (
"tests.tests_invalid.get_template_context_invalid"
)
response = self.client.get("/")
val = response.context.get(
"TEST_MAINTENANCE_MODE_GET_TEMPLATE_CONTEXT", False
)
val = response.context.get("TEST_MAINTENANCE_MODE_GET_CONTEXT", False)
self.assertFalse(val)
except ImproperlyConfigured:
get_template_context_error = True
self.assertTrue(get_template_context_error)

get_template_context_error = False
try:
settings.MAINTENANCE_MODE_GET_TEMPLATE_CONTEXT = (
settings.MAINTENANCE_MODE_GET_CONTEXT = (
"tests.tests.get_template_context_invalid"
)
response = self.client.get("/")
val = response.context.get(
"TEST_MAINTENANCE_MODE_GET_TEMPLATE_CONTEXT", False
)
val = response.context.get("TEST_MAINTENANCE_MODE_GET_CONTEXT", False)
self.assertFalse(val)
except ImproperlyConfigured:
get_template_context_error = True
self.assertTrue(get_template_context_error)

settings.MAINTENANCE_MODE_GET_TEMPLATE_CONTEXT = None
settings.MAINTENANCE_MODE_GET_CONTEXT = None
response = self.client.get("/")
val = response.context.get("TEST_MAINTENANCE_MODE_GET_TEMPLATE_CONTEXT", False)
val = response.context.get("TEST_MAINTENANCE_MODE_GET_CONTEXT", False)
self.assertFalse(val)

def test_middleware_response_type(self):
Expand Down Expand Up @@ -1084,7 +1078,7 @@ def tearDown(self):
# Clean up settings
settings.MAINTENANCE_MODE_REDIRECT_URL = None
settings.MAINTENANCE_MODE_TEMPLATE = "503.html"
settings.MAINTENANCE_MODE_GET_TEMPLATE_CONTEXT = None
settings.MAINTENANCE_MODE_GET_CONTEXT = None

def test_redirect(self):
settings.MAINTENANCE_MODE_REDIRECT_URL = "http://redirect.example.cz/"
Expand All @@ -1096,7 +1090,7 @@ def test_redirect(self):

def test_no_context(self):
settings.MAINTENANCE_MODE_TEMPLATE = "503.html"
settings.MAINTENANCE_MODE_GET_TEMPLATE_CONTEXT = None
settings.MAINTENANCE_MODE_GET_CONTEXT = None

response = http.get_maintenance_response(RequestFactory().get("/dummy/"))

Expand All @@ -1109,9 +1103,7 @@ def test_no_context(self):

def test_custom_context(self):
settings.MAINTENANCE_MODE_TEMPLATE = "503.html"
settings.MAINTENANCE_MODE_GET_TEMPLATE_CONTEXT = (
"tests.tests.get_template_context"
)
settings.MAINTENANCE_MODE_GET_CONTEXT = "tests.tests.get_template_context"

response = http.get_maintenance_response(RequestFactory().get("/dummy/"))

Expand All @@ -1123,9 +1115,7 @@ def test_custom_context(self):
self.assertIn("max-age=0", response["Cache-Control"])

def test_invalid_context_function(self):
settings.MAINTENANCE_MODE_GET_TEMPLATE_CONTEXT = (
"invalid.invalid-context-function"
)
settings.MAINTENANCE_MODE_GET_CONTEXT = "invalid.invalid-context-function"

self.assertRaisesMessage(
ImproperlyConfigured,
Expand Down

0 comments on commit cccaabf

Please sign in to comment.