def test_redirect(self): settings.MAINTENANCE_MODE_REDIRECT_URL = 'http://redirect.example.cz/' response = http.get_maintenance_response(RequestFactory().get('/dummy/')) self.assertEqual(response.status_code, 302) self.assertEqual(response['Location'], 'http://redirect.example.cz/')
def test_redirect(self): settings.MAINTENANCE_MODE_REDIRECT_URL = 'http://redirect.example.cz/' response = http.get_maintenance_response( RequestFactory().get('/dummy/')) self.assertEqual(response.status_code, 302) self.assertEqual(response['Location'], 'http://redirect.example.cz/')
def test_no_context(self): settings.MAINTENANCE_MODE_TEMPLATE = '503.html' settings.MAINTENANCE_MODE_GET_TEMPLATE_CONTEXT = None response = http.get_maintenance_response( RequestFactory().get('/dummy/')) self.assertContains( response, 'django-maintenance-mode', status_code=settings.MAINTENANCE_MODE_STATUS_CODE) self.assertIn('max-age=0', response['Cache-Control'])
def test_no_context(self): settings.MAINTENANCE_MODE_TEMPLATE = '503.html' settings.MAINTENANCE_MODE_GET_TEMPLATE_CONTEXT = None response = http.get_maintenance_response( RequestFactory().get('/dummy/')) self.assertContains(response, 'django-maintenance-mode', status_code=settings.MAINTENANCE_MODE_STATUS_CODE) self.assertIn('max-age=0', response['Cache-Control'])
def test_custom_context(self): settings.MAINTENANCE_MODE_TEMPLATE = '503.html' settings.MAINTENANCE_MODE_GET_TEMPLATE_CONTEXT = \ 'tests.tests.get_template_context' response = http.get_maintenance_response( RequestFactory().get('/dummy/')) self.assertContains( response, 'django-maintenance-mode', status_code=503) self.assertEqual(response['Content-Type'], 'text/html') self.assertIn('max-age=0', response['Cache-Control'])
def wrapper(request, *args, **kwargs): return get_maintenance_response(request)
def process_request(self, request): if need_maintenance_response(request): return get_maintenance_response(request) else: return None
def process_request(self, request): if not core.get_maintenance_mode(): return None try: url_off = reverse('maintenance_mode_off') resolve(url_off) if url_off == request.path_info: return None except NoReverseMatch: # maintenance_mode.urls not added pass if hasattr(request, 'user'): if settings.MAINTENANCE_MODE_IGNORE_STAFF \ and request.user.is_staff: return None if settings.MAINTENANCE_MODE_IGNORE_SUPERUSER \ and request.user.is_superuser: return None if settings.MAINTENANCE_MODE_IGNORE_TESTS: is_testing = False if (len(sys.argv) > 0 and 'runtests' in sys.argv[0]) \ or (len(sys.argv) > 1 and sys.argv[1] == 'test'): # python runtests.py | python manage.py test | python # setup.py test | django-admin.py test is_testing = True if is_testing: return None if settings.MAINTENANCE_MODE_IGNORE_IP_ADDRESSES: if settings.MAINTENANCE_MODE_GET_CLIENT_IP_ADDRESS: try: get_client_ip_address_func = import_string( settings.MAINTENANCE_MODE_GET_CLIENT_IP_ADDRESS) except ImportError: raise ImproperlyConfigured( 'settings.MAINTENANCE_MODE_GET_CLIENT_IP_ADDRESS ' 'is not a valid function path.') else: client_ip_address = get_client_ip_address_func(request) else: client_ip_address = utils.get_client_ip_address(request) for ip_address in settings.MAINTENANCE_MODE_IGNORE_IP_ADDRESSES: ip_address_re = re.compile(ip_address) if ip_address_re.match(client_ip_address): return None if settings.MAINTENANCE_MODE_IGNORE_URLS: for url in settings.MAINTENANCE_MODE_IGNORE_URLS: url_re = re.compile(url) if url_re.match(request.path_info): return None if settings.MAINTENANCE_MODE_REDIRECT_URL: redirect_url_re = re.compile( settings.MAINTENANCE_MODE_REDIRECT_URL) if redirect_url_re.match(request.path_info): return None return get_maintenance_response(request)
def wrap(request, *args, **kwargs): return get_maintenance_response(request)