0002-middleware-add-a-maintenance-middleware-65447.patch
tests/test_maintenance.py | ||
---|---|---|
1 |
import os |
|
2 | ||
3 |
from .utilities import create_temporary_pub, get_app |
|
4 | ||
5 | ||
6 |
def test_maintenance_page(settings): |
|
7 |
pub = create_temporary_pub() |
|
8 |
app = get_app(pub) |
|
9 |
resp = app.get('/') |
|
10 |
assert resp.status_code == 200 |
|
11 | ||
12 |
site_options_path = os.path.join(pub.app_dir, 'site-options.cfg') |
|
13 |
with open(site_options_path, 'w') as fd: |
|
14 |
fd.write( |
|
15 |
'''\ |
|
16 |
[variables] |
|
17 |
maintenance_page = True |
|
18 |
''' |
|
19 |
) |
|
20 |
resp = app.get('/', status=503) |
|
21 |
assert 'This site is currently unavailable.' in resp.text |
|
22 | ||
23 |
with open(site_options_path, 'w') as fd: |
|
24 |
fd.write( |
|
25 |
'''\ |
|
26 |
[variables] |
|
27 |
maintenance_page = True |
|
28 |
maintenance_page_message = foo bar |
|
29 |
''' |
|
30 |
) |
|
31 |
resp = app.get('/', status=503) |
|
32 |
assert 'This site is currently unavailable.' in resp.text |
|
33 |
assert 'foo bar' in resp.text |
|
34 | ||
35 |
settings.MAINTENANCE_PASS_THROUGH_IPS = ['127.0.0.1'] |
|
36 |
resp = app.get('/') |
|
37 |
assert resp.status_code == 200 |
|
38 | ||
39 |
settings.MAINTENANCE_PASS_THROUGH_IPS = [] |
|
40 |
resp = app.get('/', status=503) |
|
41 | ||
42 |
with open(site_options_path, 'w') as fd: |
|
43 |
fd.write( |
|
44 |
'''\ |
|
45 |
[variables] |
|
46 |
maintenance_page = True |
|
47 |
maintenance_pass_through_header = X-Entrouvert |
|
48 |
''' |
|
49 |
) |
|
50 |
resp = app.get('/', headers={'X-Entrouvert': 'yes'}) |
|
51 |
assert resp.status_code == 200 |
wcs/middleware.py | ||
---|---|---|
19 | 19 |
import time |
20 | 20 |
import urllib.parse |
21 | 21 | |
22 |
from django.conf import settings |
|
22 | 23 |
from django.http import HttpResponseBadRequest, HttpResponseRedirect |
24 |
from django.template.response import TemplateResponse |
|
23 | 25 |
from django.utils.deprecation import MiddlewareMixin |
24 | 26 |
from quixote import get_publisher |
25 | 27 |
from quixote.errors import RequestError |
... | ... | |
114 | 116 |
else: |
115 | 117 |
http_response.process_after_jobs() |
116 | 118 |
return response |
119 | ||
120 | ||
121 |
def pass_through(request, pub): |
|
122 |
remote_addr = request.META.get('REMOTE_ADDR') |
|
123 |
if remote_addr: |
|
124 |
pass_through_ips = getattr(settings, 'MAINTENANCE_PASS_THROUGH_IPS', []) |
|
125 |
if remote_addr in pass_through_ips: |
|
126 |
return True |
|
127 |
pass_through_header = pub.get_site_option('maintenance_pass_through_header', 'variables') |
|
128 |
if pass_through_header and pass_through_header in request.headers: |
|
129 |
return True |
|
130 |
return False |
|
131 | ||
132 | ||
133 |
class MaintenanceMiddleware: |
|
134 |
def __init__(self, get_response): |
|
135 |
self.get_response = get_response |
|
136 | ||
137 |
def __call__(self, request): |
|
138 |
pub = get_publisher() |
|
139 |
maintenance_mode = pub.get_site_option('maintenance_page', 'variables') |
|
140 |
if maintenance_mode and not pass_through(request, pub): |
|
141 |
maintenance_message = pub.get_site_option('maintenance_page_message', 'variables') |
|
142 |
context = {'maintenance_message': maintenance_message or ''} |
|
143 |
return TemplateResponse( |
|
144 |
request, 'hobo/maintenance/maintenance_page.html', context=context, status=503 |
|
145 |
).render() |
|
146 |
return self.get_response(request) |
wcs/settings.py | ||
---|---|---|
115 | 115 |
'wcs.middleware.PublisherInitialisationMiddleware', |
116 | 116 |
'django.middleware.clickjacking.XFrameOptionsMiddleware', |
117 | 117 |
'wcs.compat.PublishErrorMiddleware', |
118 |
'wcs.middleware.MaintenanceMiddleware', |
|
118 | 119 |
'wcs.middleware.AfterJobsMiddleware', |
119 | 120 |
) |
120 | 121 |
wcs/templates/hobo/maintenance/maintenance_page.html | ||
---|---|---|
1 |
{% load i18n %} |
|
2 | ||
3 |
<html> |
|
4 |
<body> |
|
5 |
<h1>{% trans "This site is currently unavailable." %}</h1> |
|
6 |
<p>{{ maintenance_message|default:"" }}</p> |
|
7 |
</body> |
|
8 |
</html> |
|
0 |
- |