def test_management_commands_verbosity(self): self.__reset_state() sys_stdin = sys.stdin confirm_answer_file = StringIO('y') sys.stdin = confirm_answer_file call_command('maintenance_mode', 'on', verbosity=3) val = core.get_maintenance_mode() self.assertTrue(val) confirm_answer_file.close() confirm_answer_file = StringIO('y') sys.stdin = confirm_answer_file call_command('maintenance_mode', 'off', verbosity=3) val = core.get_maintenance_mode() self.assertFalse(val) confirm_answer_file.close() confirm_answer_file = StringIO('n') sys.stdin = confirm_answer_file call_command('maintenance_mode', 'on', verbosity=3) val = core.get_maintenance_mode() self.assertFalse(val) confirm_answer_file.close() sys.stdin = sys_stdin
def test_context_manager_off(self): with self.settings(MAINTENANCE_MODE_STATE_FILE_PATH=self.tmp_dir): for value in [True, False]: core.set_maintenance_mode(value) with core.maintenance_mode_off(): self.assertEqual(core.get_maintenance_mode(), False) self.assertEqual(core.get_maintenance_mode(), value)
def test_context_manager_override(self): with self.settings(MAINTENANCE_MODE_STATE_FILE_PATH=self.tmp_dir): for environ, override, result in self.override_cases: core.set_maintenance_mode(environ) with core.override_maintenance_mode(override): self.assertEqual(core.get_maintenance_mode(), result) self.assertEqual(core.get_maintenance_mode(), environ)
def handle(self, *args, **options): verbosity = int(options['verbosity']) verbose = True if verbosity == 3 else False if django.VERSION < (1, 8): if len(args) != 1: raise CommandError('Error: expected 1 argument: %s' % (self.args, )) state = args[0] else: state = options['state'] state = state.lower() value = core.get_maintenance_mode() if state in ['on', 'yes', 'true', '1']: if value: if verbose: self.stdout.write('maintenance mode is already on') return if verbose: if self.confirm('maintenance mode on? (y/N) '): core.set_maintenance_mode(True) else: core.set_maintenance_mode(True) elif state in ['off', 'no', 'false', '0']: if not value: if verbose: self.stdout.write('maintenance mode is already off') return if verbose: if self.confirm('maintenance mode off? (y/N) '): core.set_maintenance_mode(False) else: core.set_maintenance_mode(False) else: raise CommandError('Error: invalid argument: \'%s\' ' 'expected %s' % ( state, self.args, )) if verbose: output = 'maintenance mode: %s' % ( 'on' if core.get_maintenance_mode() else 'off', ) self.stdout.write(output) return
def test_core(self): self.__reset_state() core.set_maintenance_mode(True) val = core.get_maintenance_mode() self.assertTrue(val) core.set_maintenance_mode(False) val = core.get_maintenance_mode() self.assertFalse(val)
def test_management_commands(self): self.__reset_state() call_command('maintenance_mode', 'on') val = core.get_maintenance_mode() self.assertTrue(val) call_command('maintenance_mode', 'off') val = core.get_maintenance_mode() self.assertFalse(val)
def test_core(self): self.__reset_state() core.set_maintenance_mode(True) val = core.get_maintenance_mode() self.assertTrue(val) core.set_maintenance_mode(False) val = core.get_maintenance_mode() self.assertFalse(val)
def test_management_commands(self): self.__reset_state() call_command('maintenance_mode', 'on') val = core.get_maintenance_mode() self.assertTrue(val) call_command('maintenance_mode', 'off') val = core.get_maintenance_mode() self.assertFalse(val)
def test_views_superuser(self): self.__reset_state() url = reverse('maintenance_mode_on') request = self.__get_superuser_request(url) views.maintenance_mode_on(request) val = core.get_maintenance_mode() self.assertTrue(val) url = reverse('maintenance_mode_off') request = self.__get_superuser_request(url) views.maintenance_mode_off(request) val = core.get_maintenance_mode() self.assertFalse(val)
def test_urls(self): self.__reset_state() url = reverse('maintenance_mode_on') response = self.client.get(url) val = core.get_maintenance_mode() self.assertRedirects(response, '/') self.assertFalse(val) url = reverse('maintenance_mode_off') response = self.client.get(url) val = core.get_maintenance_mode() self.assertRedirects(response, '/') self.assertFalse(val)
def test_urls(self): self.__reset_state() url = reverse('maintenance_mode_on') response = self.client.get(url) val = core.get_maintenance_mode() self.assertRedirects(response, '/') self.assertFalse(val) url = reverse('maintenance_mode_off') response = self.client.get(url) val = core.get_maintenance_mode() self.assertRedirects(response, '/') self.assertFalse(val)
def test_views_superuser(self): self.__reset_state() url = reverse('maintenance_mode_on') request = self.__get_superuser_request(url) views.maintenance_mode_on(request) val = core.get_maintenance_mode() self.assertTrue(val) url = reverse('maintenance_mode_off') request = self.__get_superuser_request(url) views.maintenance_mode_off(request) val = core.get_maintenance_mode() self.assertFalse(val)
def test_core_maintenance_disabled(self): # Test `get_maintenance_mode` returns maintenance mode from settings - disabled self.__reset_state() core.set_maintenance_mode(True) # Enable maintenance mode in lock file settings.MAINTENANCE_MODE = False val = core.get_maintenance_mode() self.assertFalse(val)
def template_context(app=None): """ Returns the context for a template. :param app: the current active app :type app: str :return: the context :rtype: dict """ result = { 'apps': get_apps(), 'apps_short': APPS_SHORT, 'apps_long': APPS_LONG, 'local_users': LOCAL_USERS, 'auth_type': AUTHENTICATION_TYPE, 'email_enabled': EMAIL_ENABLED, 'announcements': db_models.get_active_announcements() } if app is not None: result['title'] = APPS_LONG[app] result['app'] = app else: result['title'] = 'Automated Reporting' result['options'] = REPORTING_OPTIONS result['maintenance'] = get_maintenance_mode() return result
def load_plugins(self): """ Load and activate all IntegrationPlugins """ if not settings.PLUGINS_ENABLED: # Plugins not enabled, do nothing return # pragma: no cover logger.info('Start loading plugins') # Set maintanace mode _maintenance = bool(get_maintenance_mode()) if not _maintenance: set_maintenance_mode(True) registered_successful = False blocked_plugin = None retry_counter = settings.PLUGIN_RETRY while not registered_successful: try: # We are using the db so for migrations etc we need to try this block self._init_plugins(blocked_plugin) self._activate_plugins() registered_successful = True except (OperationalError, ProgrammingError): # pragma: no cover # Exception if the database has not been migrated yet logger.info('Database not accessible while loading plugins') break except IntegrationPluginError as error: logger.error( f'[PLUGIN] Encountered an error with {error.path}:\n{error.message}' ) log_error({error.path: error.message}, 'load') blocked_plugin = error.path # we will not try to load this app again # Initialize apps without any plugins self._clean_registry() self._clean_installed_apps() self._activate_plugins(force_reload=True) # We do not want to end in an endless loop retry_counter -= 1 if retry_counter <= 0: # pragma: no cover if settings.PLUGIN_TESTING: print('[PLUGIN] Max retries, breaking loading') break if settings.PLUGIN_TESTING: print( f'[PLUGIN] Above error occured during testing - {retry_counter}/{settings.PLUGIN_RETRY} retries left' ) # now the loading will re-start up with init # Remove maintenance mode if not _maintenance: set_maintenance_mode(False) logger.info('Finished loading plugins')
def test_context_processor(self): with self.settings(MAINTENANCE_MODE_STATE_FILE_PATH=self.tmp_dir): for environ, override, result in self.override_cases: core.set_maintenance_mode(environ) with core.override_maintenance_mode(override): self.assertEqual(core.get_maintenance_mode(), result)
def process_request(self, request): if settings.MAINTENANCE_MODE or core.get_maintenance_mode(): 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 for ip_address_re in settings.MAINTENANCE_MODE_IGNORE_IP_ADDRESSES_RE: if ip_address_re.match(request.META['REMOTE_ADDR']): return None for url_re in settings.MAINTENANCE_MODE_IGNORE_URLS_RE: if url_re.match(request.path_info): return None if settings.MAINTENANCE_MODE_REDIRECT_URL: return HttpResponseRedirect(settings.MAINTENANCE_MODE_REDIRECT_URL) else: return render_to_response(settings.MAINTENANCE_MODE_TEMPLATE, {}, context_instance=RequestContext(request), content_type='text/html') else: return None
def unload_plugins(self): """ Unload and deactivate all IntegrationPlugins """ if not settings.PLUGINS_ENABLED: # Plugins not enabled, do nothing return # pragma: no cover logger.info('Start unloading plugins') # Set maintanace mode _maintenance = bool(get_maintenance_mode()) if not _maintenance: set_maintenance_mode(True) # pragma: no cover # remove all plugins from registry self._clean_registry() # deactivate all integrations self._deactivate_plugins() # remove maintenance if not _maintenance: set_maintenance_mode(False) # pragma: no cover logger.info('Finished unloading plugins')
def dispatch(self, request, *args, **kwargs): if request.user.is_authenticated: if get_maintenance_mode(): return redirect(reverse_lazy('users:users_login')) else: return redirect(reverse_lazy('patient:patient_index')) return super(IndexView, self).dispatch(request, *args, **kwargs)
def test_core_maintenance_disabled(self): self.__reset_state() core.set_maintenance_mode(True) settings.MAINTENANCE_MODE = False val = core.get_maintenance_mode() self.assertFalse(val)
def test_urls_superuser(self): self.__reset_state() self.__login_superuser() url = reverse('maintenance_mode_on') response = self.client.get(url) val = core.get_maintenance_mode() self.assertTrue(val) url = reverse('maintenance_mode_off') response = self.client.get(url) val = core.get_maintenance_mode() self.assertFalse(val) self.__logout()
def test_core_maintenance_disabled(self): self.__reset_state() core.set_maintenance_mode(True) settings.MAINTENANCE_MODE = False val = core.get_maintenance_mode() self.assertFalse(val)
def test_urls_superuser(self): self.__reset_state() self.__login_superuser() url = reverse('maintenance_mode_on') response = self.client.get(url) val = core.get_maintenance_mode() self.assertTrue(val) url = reverse('maintenance_mode_off') response = self.client.get(url) val = core.get_maintenance_mode() self.assertFalse(val) self.__logout()
def get_maintenance_mode(self): try: value = core.get_maintenance_mode() return value except IOError: raise CommandError( 'Unable to read state file at: %s' % ( settings.MAINTENANCE_MODE_STATE_FILE_NAME, ))
def process_request(self, request): if settings.MAINTENANCE_MODE or core.get_maintenance_mode(): 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 for ip_address_re in settings.MAINTENANCE_MODE_IGNORE_IP_ADDRESSES_RE: if ip_address_re.match(request.META['REMOTE_ADDR']): return None for url_re in settings.MAINTENANCE_MODE_IGNORE_URLS_RE: if url_re.match(request.path_info): return None if settings.MAINTENANCE_MODE_REDIRECT_URL: return HttpResponseRedirect( settings.MAINTENANCE_MODE_REDIRECT_URL) else: if django.VERSION < (1, 8): response = render_to_response( settings.MAINTENANCE_MODE_TEMPLATE, self.get_request_context(request), context_instance=RequestContext(request), content_type='text/html') else: response = render( request, settings.MAINTENANCE_MODE_TEMPLATE, context=self.get_request_context(request), content_type='text/html', status=503) add_never_cache_headers(response) return response else: return None
def get_context_data(self, **kwargs): context = super(ConfigurationUpdate, self).get_context_data(**kwargs) if 'form' not in context: context['form'] = self.form_class(self.request.GET) if 'form2' not in context: context['form2'] = self.second_form_class(initial={ 'maintenance': get_maintenance_mode() }) # Los mensajes de error se van si saco ese self.request.GET return context
def test_management_commands_interactive(self): self.__reset_state() sys_stdin = sys.stdin confirm_answer_file = StringIO('y') sys.stdin = confirm_answer_file call_command('maintenance_mode', 'on', interactive=True) val = core.get_maintenance_mode() self.assertTrue(val) confirm_answer_file.close() confirm_answer_file = StringIO('y') sys.stdin = confirm_answer_file call_command('maintenance_mode', 'on', interactive=True) val = core.get_maintenance_mode() self.assertTrue(val) confirm_answer_file.close() confirm_answer_file = StringIO('y') sys.stdin = confirm_answer_file call_command('maintenance_mode', 'off', interactive=True) val = core.get_maintenance_mode() self.assertFalse(val) confirm_answer_file.close() confirm_answer_file = StringIO('y') sys.stdin = confirm_answer_file call_command('maintenance_mode', 'off', interactive=True) val = core.get_maintenance_mode() self.assertFalse(val) confirm_answer_file.close() confirm_answer_file = StringIO('n') sys.stdin = confirm_answer_file call_command('maintenance_mode', 'on', interactive=True) val = core.get_maintenance_mode() self.assertFalse(val) confirm_answer_file.close() sys.stdin = sys_stdin
def test_urls_with_scriptname(self): self.__reset_state() url = reverse('maintenance_mode_on') request = self.request_factory.get(url) response = self.client.get(url) val = core.get_maintenance_mode() self.assertRedirects(response, '{}/'.format(request.META.get('SCRIPT_NAME')), fetch_redirect_response=False) self.assertFalse(val) url = reverse('maintenance_mode_off') request = self.request_factory.get(url) response = self.client.get(url) val = core.get_maintenance_mode() self.assertRedirects(response, '{}/'.format(request.META.get('SCRIPT_NAME')), fetch_redirect_response=False) self.assertFalse(val)
def unload_plugins(self): """unload and deactivate all IntegrationPlugins""" logger.info('Start unloading plugins') # set maintanace mode _maintenance = bool(get_maintenance_mode()) if not _maintenance: set_maintenance_mode(True) # remove all plugins from registry self._clean_registry() # deactivate all integrations self._deactivate_plugins() # remove maintenance if not _maintenance: set_maintenance_mode(False) logger.info('Finished unloading plugins')
def process_request(self, request): if settings.MAINTENANCE_MODE or core.get_maintenance_mode(): url_off = reverse('maintenance_mode_off') try: 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 for ip_address_re in settings.MAINTENANCE_MODE_IGNORE_IP_ADDRESSES_RE: if ip_address_re.match(request.META['REMOTE_ADDR']): return None for url_re in settings.MAINTENANCE_MODE_IGNORE_URLS_RE: if url_re.match(request.path_info): return None if settings.MAINTENANCE_MODE_REDIRECT_URL: return HttpResponseRedirect(settings.MAINTENANCE_MODE_REDIRECT_URL) else: return render_to_response(settings.MAINTENANCE_MODE_TEMPLATE, self.get_request_context(request), context_instance=RequestContext(request), content_type='text/html') else: return None
def need_maintenance_response(request): """ Tells if the given request needs a maintenance response or not. """ try: view_match = resolve(request.path) view_func = view_match[0] view_dict = view_func.__dict__ view_force_maintenance_mode_off = view_dict.get( 'force_maintenance_mode_off', False) if view_force_maintenance_mode_off: # view has 'force_maintenance_mode_off' decorator return False view_force_maintenance_mode_on = view_dict.get( 'force_maintenance_mode_on', False) if view_force_maintenance_mode_on: # view has 'force_maintenance_mode_on' decorator return True except Resolver404: pass if not get_maintenance_mode(): return False try: url_off = reverse('maintenance_mode_off') resolve(url_off) if url_off == request.path_info: return False except NoReverseMatch: # maintenance_mode.urls not added pass if hasattr(request, 'user'): if django.VERSION < (1, 10): if settings.MAINTENANCE_MODE_IGNORE_ANONYMOUS_USER \ and request.user.is_anonymous(): return False if settings.MAINTENANCE_MODE_IGNORE_AUTHENTICATED_USER \ and request.user.is_authenticated(): return False else: if settings.MAINTENANCE_MODE_IGNORE_ANONYMOUS_USER \ and request.user.is_anonymous: return False if settings.MAINTENANCE_MODE_IGNORE_AUTHENTICATED_USER \ and request.user.is_authenticated: return False if settings.MAINTENANCE_MODE_IGNORE_STAFF \ and request.user.is_staff: return False if settings.MAINTENANCE_MODE_IGNORE_SUPERUSER \ and request.user.is_superuser: return False if settings.MAINTENANCE_MODE_IGNORE_ADMIN_SITE: try: request_path = request.path if request.path else '' if not request_path.endswith('/'): request_path += '/' admin_url = reverse('admin:index') if request_path.startswith(admin_url): return False except NoReverseMatch: # admin.urls not added pass 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 False 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 = 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 False if settings.MAINTENANCE_MODE_IGNORE_URLS: for url in settings.MAINTENANCE_MODE_IGNORE_URLS: if not isinstance(url, pattern_class): url = str(url) url_re = re.compile(url) if url_re.match(request.path_info): return False 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 False return True
def need_maintenance_response(request): """ Tells if the given request needs a maintenance response or not. """ try: view_match = resolve(request.path) view_func = view_match[0] view_dict = view_func.__dict__ view_force_maintenance_mode_off = view_dict.get( 'force_maintenance_mode_off', False) if view_force_maintenance_mode_off: # view has 'force_maintenance_mode_off' decorator return False view_force_maintenance_mode_on = view_dict.get( 'force_maintenance_mode_on', False) if view_force_maintenance_mode_on: # view has 'force_maintenance_mode_on' decorator return True except Resolver404: pass if not get_maintenance_mode(): return False try: url_off = reverse('maintenance_mode_off') resolve(url_off) if url_off == request.path_info: return False except NoReverseMatch: # maintenance_mode.urls not added pass if hasattr(request, 'user'): if django.VERSION < (1, 10): if settings.MAINTENANCE_MODE_IGNORE_ANONYMOUS_USER \ and request.user.is_anonymous(): return False if settings.MAINTENANCE_MODE_IGNORE_AUTHENTICATED_USER \ and request.user.is_authenticated(): return False else: if settings.MAINTENANCE_MODE_IGNORE_ANONYMOUS_USER \ and request.user.is_anonymous: return False if settings.MAINTENANCE_MODE_IGNORE_AUTHENTICATED_USER \ and request.user.is_authenticated: return False if settings.MAINTENANCE_MODE_IGNORE_STAFF \ and request.user.is_staff: return False if settings.MAINTENANCE_MODE_IGNORE_SUPERUSER \ and request.user.is_superuser: return False if settings.MAINTENANCE_MODE_IGNORE_ADMIN_SITE: try: request_path = request.path if request.path else '' if not request_path.endswith('/'): request_path += '/' admin_url = reverse('admin:index') if request_path.startswith(admin_url): return False except NoReverseMatch: # admin.urls not added pass 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 False 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 = 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 False if settings.MAINTENANCE_MODE_IGNORE_URLS: for url in settings.MAINTENANCE_MODE_IGNORE_URLS: if not isinstance(url, pattern_class): url = str(url) url_re = re.compile(url) if url_re.match(request.path_info): return False 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 False return True
def logued_user(request): if get_maintenance_mode(): return redirect(reverse_lazy('configuration:configuration')) else: return redirect(reverse_lazy('patient:patient_index'))
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 maintenance_mode(request): return {'maintenance_mode': core.get_maintenance_mode()}
def test_function(): self.assertEqual(core.get_maintenance_mode(), result)
def maintenance_mode(request): return { 'maintenance_mode':get_maintenance_mode() }
def process_request(self, request): if settings.MAINTENANCE_MODE or core.get_maintenance_mode(): 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: get_client_ip_address_func = utils.import_function( settings.MAINTENANCE_MODE_GET_CLIENT_IP_ADDRESS) if get_client_ip_address_func: client_ip_address = get_client_ip_address_func(request) else: raise ImproperlyConfigured( 'settings.MAINTENANCE_MODE_GET_CLIENT_IP_ADDRESS is not a valid function path.' ) 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 HttpResponseRedirect( settings.MAINTENANCE_MODE_REDIRECT_URL) else: request_context = {} if settings.MAINTENANCE_MODE_GET_TEMPLATE_CONTEXT: get_request_context_func = utils.import_function( settings.MAINTENANCE_MODE_GET_TEMPLATE_CONTEXT) if get_request_context_func: request_context = get_request_context_func( request=request) else: raise ImproperlyConfigured( 'settings.MAINTENANCE_MODE_GET_TEMPLATE_CONTEXT is not a valid function path.' ) if django.VERSION < (1, 8): response = render_to_response( settings.MAINTENANCE_MODE_TEMPLATE, request_context, context_instance=RequestContext(request), content_type='text/html') else: response = render(request, settings.MAINTENANCE_MODE_TEMPLATE, context=request_context, content_type='text/html', status=503) add_never_cache_headers(response) return response else: return None
def test_function(): self.assertEqual(core.get_maintenance_mode(), result)