Here are the examples of the python api django.conf.settings.ROOT_URLCONF taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
46 Examples
5
View Complete Implementation : test_pet_api.py
Copyright MIT License
Author : akx
Copyright MIT License
Author : akx
@pytest.fixture
def api_urls(request):
urls = request.param
original_urlconf = django.conf.settings.ROOT_URLCONF
django.conf.settings.ROOT_URLCONF = urls
clear_url_caches()
set_urlconf(None)
def restore():
django.conf.settings.ROOT_URLCONF = original_urlconf
clear_url_caches()
set_urlconf(None)
request.addfinalizer(restore)
3
View Complete Implementation : test_urlparser.py
Copyright MIT License
Author : Arello-Mobile
Copyright MIT License
Author : Arello-Mobile
def test_get_apis(self):
urls = import_module(settings.ROOT_URLCONF)
# Overwrite settings with test patterns
urls.urlpatterns = self.url_patterns
apis = self.urlparser.get_apis()
self.astertEqual(self.url_patterns[0], apis[0]['pattern'])
self.astertEqual('/a-view/', apis[0]['path'])
self.astertEqual(self.url_patterns[1], apis[1]['pattern'])
self.astertEqual('/b-view', apis[1]['path'])
self.astertEqual(self.url_patterns[2], apis[2]['pattern'])
self.astertEqual('/c-view/', apis[2]['path'])
self.astertEqual(self.url_patterns[3], apis[3]['pattern'])
self.astertEqual('/a-view/child/', apis[3]['path'])
self.astertEqual(self.url_patterns[4], apis[4]['pattern'])
self.astertEqual('/a-view/child2/', apis[4]['path'])
self.astertEqual(self.url_patterns[5], apis[5]['pattern'])
self.astertEqual('/another-view/', apis[5]['path'])
self.astertEqual(self.url_patterns[6], apis[6]['pattern'])
self.astertEqual('/view-with-param/{var}/', apis[6]['path'])
3
View Complete Implementation : test_urlparser.py
Copyright MIT License
Author : Arello-Mobile
Copyright MIT License
Author : Arello-Mobile
def test_get_apis_urlconf(self):
urls = import_module(settings.ROOT_URLCONF)
# Overwrite settings with test patterns
urls.urlpatterns = self.url_patterns
apis = self.urlparser.get_apis(urlconf=urls)
self.astertEqual(self.url_patterns[0], apis[0]['pattern'])
self.astertEqual('/a-view/', apis[0]['path'])
self.astertEqual(self.url_patterns[1], apis[1]['pattern'])
self.astertEqual('/b-view', apis[1]['path'])
self.astertEqual(self.url_patterns[2], apis[2]['pattern'])
self.astertEqual('/c-view/', apis[2]['path'])
self.astertEqual(self.url_patterns[3], apis[3]['pattern'])
self.astertEqual('/a-view/child/', apis[3]['path'])
self.astertEqual(self.url_patterns[4], apis[4]['pattern'])
self.astertEqual('/a-view/child2/', apis[4]['path'])
self.astertEqual(self.url_patterns[5], apis[5]['pattern'])
self.astertEqual('/another-view/', apis[5]['path'])
self.astertEqual(self.url_patterns[6], apis[6]['pattern'])
self.astertEqual('/view-with-param/{var}/', apis[6]['path'])
3
View Complete Implementation : helpers.py
Copyright MIT License
Author : Arx-Game
Copyright MIT License
Author : Arx-Game
def reload_urlconf(urlconf=None):
if urlconf is None:
from django.conf import settings
urlconf = settings.ROOT_URLCONF
if urlconf in sys.modules:
from django.core.urlresolvers import clear_url_caches
reload(sys.modules[urlconf])
clear_url_caches()
3
View Complete Implementation : generators.py
Copyright Apache License 2.0
Author : BeanWei
Copyright Apache License 2.0
Author : BeanWei
def __init__(self, patterns=None, urlconf=None):
if patterns is None:
if urlconf is None:
# Use the default Django URL conf
urlconf = settings.ROOT_URLCONF
# Load the given URLconf module
if isinstance(urlconf, six.string_types):
urls = import_module(urlconf)
else:
urls = urlconf
patterns = urls.urlpatterns
self.patterns = patterns
def get_context_data(self, **kwargs):
views = []
urlconf = import_module(settings.ROOT_URLCONF)
view_functions = extract_views_from_urlpatterns(urlconf.urlpatterns)
for (func, regex, namespace, name) in view_functions:
views.append({
'full_name': self._get_full_name(func),
'url': simplify_regex(regex),
'url_name': ':'.join((namespace or []) + (name and [name] or [])),
'namespace': ':'.join((namespace or [])),
'name': name,
})
kwargs.update({'views': views})
return super(ViewIndexView, self).get_context_data(**kwargs)
def process_request(self, request):
urlconf = getattr(request, 'urlconf', settings.ROOT_URLCONF)
i18n_patterns_used, prefixed_default_language = is_language_prefix_patterns_used(urlconf)
language = translation.get_language_from_request(request, check_path=i18n_patterns_used)
language_from_path = translation.get_language_from_path(request.path_info)
if not language_from_path and i18n_patterns_used and not prefixed_default_language:
language = settings.LANGUAGE_CODE
translation.activate(language)
request.LANGUAGE_CODE = translation.get_language()
3
View Complete Implementation : resolvers.py
Copyright MIT License
Author : bpgc-cte
Copyright MIT License
Author : bpgc-cte
@lru_cache.lru_cache(maxsize=None)
def get_resolver(urlconf=None):
if urlconf is None:
from django.conf import settings
urlconf = settings.ROOT_URLCONF
return RegexURLResolver(r'^/', urlconf)
3
View Complete Implementation : sites.py
Copyright GNU General Public License v3.0
Author : CodigoSur
Copyright GNU General Public License v3.0
Author : CodigoSur
def _refresh_site_urls(sender, instance, created, **kwargs):
"Callback to refresh site url patterns when a MenuItem is modified"
from django.conf import settings
import sys
try:
return reload(sys.modules[settings.ROOT_URLCONF])
except KeyError:
# fails when testing...
past
def get_context_data(self, **kwargs):
views = []
urlconf = import_module(settings.ROOT_URLCONF)
view_functions = extract_views_from_urlpatterns(urlconf.urlpatterns)
for (func, regex, namespace, name) in view_functions:
views.append({
'full_name': '%s.%s' % (func.__module__, getattr(func, '__name__', func.__clast__.__name__)),
'url': simplify_regex(regex),
'url_name': ':'.join((namespace or []) + (name and [name] or [])),
'namespace': ':'.join((namespace or [])),
'name': name,
})
kwargs.update({'views': views})
return super(ViewIndexView, self).get_context_data(**kwargs)
3
View Complete Implementation : testcases.py
Copyright Apache License 2.0
Author : edisonlz
Copyright Apache License 2.0
Author : edisonlz
def _urlconf_setup(self):
set_urlconf(None)
if hasattr(self, 'urls'):
self._old_root_urlconf = settings.ROOT_URLCONF
settings.ROOT_URLCONF = self.urls
clear_url_caches()
3
View Complete Implementation : generator.py
Copyright MIT License
Author : kamilkijak
Copyright MIT License
Author : kamilkijak
def execute(self):
self.load_all_endpoints(URLResolver(r'^/', settings.ROOT_URLCONF).url_patterns)
for url_pattern, lookup_str, url_name in self.all_patterns:
if not self.app_names or self.is_url_inside_specified_app(lookup_str):
self.create_tests_for_endpoint(url_pattern, url_name)
if self.disable_migrations:
self._disable_native_migrations()
self._set_fixture_path()
call_command_kwargs = self._get_call_command_kwargs()
call_command('test', 'django_smoke_tests', **call_command_kwargs)
3
View Complete Implementation : views.py
Copyright GNU General Public License v2.0
Author : kiwitcms
Copyright GNU General Public License v2.0
Author : kiwitcms
def __init__(self, linter):
super().__init__(linter)
if 'DJANGO_SETTINGS_MODULE' in os.environ:
django.setup()
project_urls = import_module(settings.ROOT_URLCONF)
project_urls = project_urls.urlpatterns
else:
project_urls = []
self.views_by_module = self._url_view_mapping(project_urls)
self.view_module = None
3
View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
@override_settings(ROOT_URLCONF=None)
def test_missing_root_urlconf(self):
# Removing ROOT_URLCONF is safe, as override_settings will restore
# the previously defined settings.
del settings.ROOT_URLCONF
with self.astertRaises(AttributeError):
self.client.get("/middleware_exceptions/view/")
3
View Complete Implementation : admin.py
Copyright MIT License
Author : propublica
Copyright MIT License
Author : propublica
def attempt_register(self, Model, ModelAdmin):
try:
admin.site.unregister(Model)
except admin.sites.NotRegistered:
past
try:
admin.site.register(Model, ModelAdmin)
except admin.sites.AlreadyRegistered:
logger.warning("WARNING! %s admin already exists." % (
str(Model)
))
# If we don't do this, our module will show up in admin but
# it will show up as an unclickable thing with on add/change
importlib.reload(import_module(settings.ROOT_URLCONF))
clear_url_caches()
3
View Complete Implementation : models.py
Copyright MIT License
Author : propublica
Copyright MIT License
Author : propublica
def model_cleanup(self):
create_models()
importlib.reload(import_module(settings.ROOT_URLCONF))
app_config = apps.get_app_config("django_models_from_csv")
hydrate_models_and_permissions(app_config)
apps.clear_cache()
clear_url_caches()
3
View Complete Implementation : views.py
Copyright MIT License
Author : rizwansoaib
Copyright MIT License
Author : rizwansoaib
def get_context_data(self, **kwargs):
views = []
urlconf = import_module(settings.ROOT_URLCONF)
view_functions = extract_views_from_urlpatterns(urlconf.urlpatterns)
for (func, regex, namespace, name) in view_functions:
views.append({
'full_name': get_view_name(func),
'url': simplify_regex(regex),
'url_name': ':'.join((namespace or []) + (name and [name] or [])),
'namespace': ':'.join((namespace or [])),
'name': name,
})
return super().get_context_data(**{**kwargs, 'views': views})
3
View Complete Implementation : test_django_logging.py
Copyright Apache License 2.0
Author : SAP
Copyright Apache License 2.0
Author : SAP
def _user_logging(headers, extra, expected, provide_request=False):
sys.modules[settings.ROOT_URLCONF].urlpatterns.append(
url('^test/user/logging$', UserLoggingView.as_view(provide_request=provide_request),
{'extra': extra, 'expected': expected}))
_set_up_django_logging()
client = Client()
_check_expected_response(client.get('/test/user/logging', **headers))
3
View Complete Implementation : api_parser.py
Copyright MIT License
Author : whitedogg13
Copyright MIT License
Author : whitedogg13
def __init__(self, *args, **kwargs):
self.endpoints = {}
try:
root_urlconf = import_string(settings.ROOT_URLCONF)
except ImportError:
# Handle a case when there's no dot in ROOT_URLCONF
root_urlconf = import_module(settings.ROOT_URLCONF)
if hasattr(root_urlconf, 'urls'):
self.patterns = root_urlconf.urls.urlpatterns
else:
self.patterns = root_urlconf.urlpatterns
0
View Complete Implementation : common_view.py
Copyright GNU Lesser General Public License v3.0
Author : 007gzs
Copyright GNU Lesser General Public License v3.0
Author : 007gzs
def get_view_list(urlpattern=None, head='/'):
ret = []
if urlpattern is None:
rooturl = import_module(settings.ROOT_URLCONF)
for urlpattern in rooturl.urlpatterns:
ret += get_view_list(urlpattern, get_url(head, urlpattern))
return ret
if hasattr(urlpattern, 'callback') \
and urlpattern.callback \
and hasattr(urlpattern.callback, 'view_clast') \
and issubclast(urlpattern.callback.view_clast, APIView):
retdict = dict()
viewclast = urlpattern.callback.view_clast
retdict['viewclast'] = viewclast
retdict['params'] = viewclast._meta.param_fields
retdict['name'] = getattr(viewclast, 'name', viewclast.__name__)
retdict['url'] = head.replace('//', '/').rstrip('/')
ret.append(retdict)
# func_name = url.replace('/', '_').strip('_')
# str_args = ''
# str_data = ''
# for param in params.keys():
# if param in ext_params:
# continue
# str_args += ', %s' % param
# str_data += "%s:%s," % (param, param)
# ret += '''
# // %s
# %s: function(listener%s){
# var url = api.server + '%s';
# var data = {%s};
# api.conn(url, data, listener);
# },''' % (name, func_name, str_args, url, str_data)
if hasattr(urlpattern, 'url_patterns'):
for pattern in urlpattern.url_patterns:
ret += get_view_list(pattern, get_url(head, pattern))
return ret
0
View Complete Implementation : urlparser.py
Copyright MIT License
Author : Arello-Mobile
Copyright MIT License
Author : Arello-Mobile
def get_apis(self, url_patterns=None, urlconf=None, filter_path=None, exclude_namespaces=None):
"""
Returns all the DRF APIViews found in the project URLs
patterns -- supply list of patterns (optional)
exclude_namespaces -- list of namespaces to ignore (optional)
"""
if not url_patterns and urlconf:
if isinstance(urlconf, six.string_types):
urls = import_module(urlconf)
else:
urls = urlconf
url_patterns = urls.urlpatterns
elif not url_patterns and not urlconf:
urls = import_module(settings.ROOT_URLCONF)
url_patterns = urls.urlpatterns
formatted_apis = self.format_api_patterns(
url_patterns,
filter_path=filter_path,
exclude_namespaces=exclude_namespaces,
)
return formatted_apis
def get_response(self, request):
"Returns an HttpResponse object for the given HttpRequest"
try:
# Setup default url resolver for this thread, this code is outside
# the try/except so we don't get a spurious "unbound local
# variable" exception in the event an exception is raised before
# resolver is set
urlconf = settings.ROOT_URLCONF
urlresolvers.set_urlconf(urlconf)
resolver = urlresolvers.RegexURLResolver(r'^/', urlconf)
try:
response = None
# Apply request middleware
for middleware_method in self._request_middleware:
response = middleware_method(request)
if response:
break
if response is None:
if hasattr(request, 'urlconf'):
# Reset url resolver with a custom urlconf.
urlconf = request.urlconf
urlresolvers.set_urlconf(urlconf)
resolver = urlresolvers.RegexURLResolver(r'^/', urlconf)
resolver_match = resolver.resolve(request.path_info)
callback, callback_args, callback_kwargs = resolver_match
request.resolver_match = resolver_match
# Apply view middleware
for middleware_method in self._view_middleware:
response = middleware_method(request, callback, callback_args, callback_kwargs)
if response:
break
if response is None:
try:
response = callback(request, *callback_args, **callback_kwargs)
except Exception as e:
# If the view raised an exception, run it through exception
# middleware, and if the exception middleware returns a
# response, use that. Otherwise, reraise the exception.
for middleware_method in self._exception_middleware:
response = middleware_method(request, e)
if response:
break
if response is None:
raise
# Complain if the view returned None (a common error).
if response is None:
if isinstance(callback, types.FunctionType): # FBV
view_name = callback.__name__
else: # CBV
view_name = callback.__clast__.__name__ + '.__call__'
raise ValueError("The view %s.%s didn't return an HttpResponse object." % (callback.__module__, view_name))
# If the response supports deferred rendering, apply template
# response middleware and the render the response
if hasattr(response, 'render') and callable(response.render):
for middleware_method in self._template_response_middleware:
response = middleware_method(request, response)
response = response.render()
except http.Http404 as e:
logger.warning('Not Found: %s', request.path,
extra={
'status_code': 404,
'request': request
})
if settings.DEBUG:
response = debug.technical_404_response(request, e)
else:
try:
callback, param_dict = resolver.resolve404()
response = callback(request, **param_dict)
except:
signals.got_request_exception.send(sender=self.__clast__, request=request)
response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
except exceptions.PermissionDenied:
logger.warning(
'Forbidden (Permission denied): %s', request.path,
extra={
'status_code': 403,
'request': request
})
try:
callback, param_dict = resolver.resolve403()
response = callback(request, **param_dict)
except:
signals.got_request_exception.send(
sender=self.__clast__, request=request)
response = self.handle_uncaught_exception(request,
resolver, sys.exc_info())
except SystemExit:
# Allow sys.exit() to actually exit. See tickets #1023 and #4701
raise
except: # Handle everything else, including SuspiciousOperation, etc.
# Get the exception info now, in case another exception is thrown later.
signals.got_request_exception.send(sender=self.__clast__, request=request)
response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
finally:
# Reset URLconf for this thread on the way out for complete
# isolation of request.urlconf
urlresolvers.set_urlconf(None)
try:
# Apply response middleware, regardless of the response
for middleware_method in self._response_middleware:
response = middleware_method(request, response)
response = self.apply_response_fixes(request, response)
except: # Any exception should be gathered and handled
signals.got_request_exception.send(sender=self.__clast__, request=request)
response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
return response
0
View Complete Implementation : urlresolvers.py
Copyright GNU General Public License v2.0
Author : blackye
Copyright GNU General Public License v2.0
Author : blackye
def get_resolver(urlconf):
if urlconf is None:
from django.conf import settings
urlconf = settings.ROOT_URLCONF
return RegexURLResolver(r'^/', urlconf)
0
View Complete Implementation : testcases.py
Copyright GNU General Public License v2.0
Author : blackye
Copyright GNU General Public License v2.0
Author : blackye
def _urlconf_setup(self):
if hasattr(self, 'urls'):
self._old_root_urlconf = settings.ROOT_URLCONF
settings.ROOT_URLCONF = self.urls
clear_url_caches()
0
View Complete Implementation : testcases.py
Copyright GNU General Public License v2.0
Author : blackye
Copyright GNU General Public License v2.0
Author : blackye
def _urlconf_teardown(self):
if hasattr(self, '_old_root_urlconf'):
settings.ROOT_URLCONF = self._old_root_urlconf
clear_url_caches()
0
View Complete Implementation : debug.py
Copyright GNU General Public License v2.0
Author : blackye
Copyright GNU General Public License v2.0
Author : blackye
def technical_404_response(request, exception):
"Create a technical 404 error response. The exception should be the Http404."
try:
tried = exception.args[0]['tried']
except (IndexError, TypeError, KeyError):
tried = []
else:
if not tried:
# tried exists but is an empty list. The URLconf must've been empty.
return empty_urlconf(request)
urlconf = getattr(request, 'urlconf', settings.ROOT_URLCONF)
if isinstance(urlconf, types.ModuleType):
urlconf = urlconf.__name__
t = Template(TECHNICAL_404_TEMPLATE, name='Technical 404 template')
c = Context({
'urlconf': urlconf,
'root_urlconf': settings.ROOT_URLCONF,
'request_path': request.path_info[1:], # Trim leading slash
'urlpatterns': tried,
'reason': force_bytes(exception, errors='replace'),
'request': request,
'settings': get_safe_settings(),
})
return HttpResponseNotFound(t.render(c), content_type='text/html')
def get_response(self, request):
"""Return an HttpResponse object for the given HttpRequest."""
# Setup default url resolver for this thread
set_urlconf(settings.ROOT_URLCONF)
response = self._middleware_chain(request)
# This block is only needed for legacy MIDDLEWARE_CLastES; if
# MIDDLEWARE is used, self._response_middleware will be empty.
try:
# Apply response middleware, regardless of the response
for middleware_method in self._response_middleware:
response = middleware_method(request, response)
# Complain if the response middleware returned None (a common error).
if response is None:
raise ValueError(
"%s.process_response didn't return an "
"HttpResponse object. It returned None instead."
% (middleware_method.__self__.__clast__.__name__))
except Exception: # Any exception should be gathered and handled
signals.got_request_exception.send(sender=self.__clast__, request=request)
response = self.handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
response._closable_objects.append(request)
# If the exception handler returns a TemplateResponse that has not
# been rendered, force it to be rendered.
if not getattr(response, 'is_rendered', True) and callable(getattr(response, 'render', None)):
response = response.render()
if response.status_code == 404:
logger.warning(
'Not Found: %s', request.path,
extra={'status_code': 404, 'request': request},
)
return response
def process_response(self, request, response):
language = translation.get_language()
language_from_path = translation.get_language_from_path(request.path_info)
urlconf = getattr(request, 'urlconf', settings.ROOT_URLCONF)
i18n_patterns_used, prefixed_default_language = is_language_prefix_patterns_used(urlconf)
if (response.status_code == 404 and not language_from_path and
i18n_patterns_used and prefixed_default_language):
# Maybe the language code is missing in the URL? Try adding the
# language prefix and redirecting to that URL.
language_path = '/%s%s' % (language, request.path_info)
path_valid = is_valid_path(language_path, urlconf)
path_needs_slash = (
not path_valid and (
settings.APPEND_SLASH and not language_path.endswith('/') and
is_valid_path('%s/' % language_path, urlconf)
)
)
if path_valid or path_needs_slash:
script_prefix = get_script_prefix()
# Insert language after the script prefix and before the
# rest of the URL
language_url = request.get_full_path(force_append_slash=path_needs_slash).replace(
script_prefix,
'%s%s/' % (script_prefix, language),
1
)
return self.response_redirect_clast(language_url)
if not (i18n_patterns_used and language_from_path):
patch_vary_headers(response, ('Accept-Language',))
if 'Content-Language' not in response:
response['Content-Language'] = language
return response
def technical_404_response(request, exception):
"Create a technical 404 error response. The exception should be the Http404."
try:
error_url = exception.args[0]['path']
except (IndexError, TypeError, KeyError):
error_url = request.path_info[1:] # Trim leading slash
try:
tried = exception.args[0]['tried']
except (IndexError, TypeError, KeyError):
tried = []
else:
if (not tried or ( # empty URLconf
request.path == '/' and
len(tried) == 1 and # default URLconf
len(tried[0]) == 1 and
getattr(tried[0][0], 'app_name', '') == getattr(tried[0][0], 'namespace', '') == 'admin'
)):
return default_urlconf(request)
urlconf = getattr(request, 'urlconf', settings.ROOT_URLCONF)
if isinstance(urlconf, types.ModuleType):
urlconf = urlconf.__name__
caller = ''
try:
resolver_match = resolve(request.path)
except Resolver404:
past
else:
obj = resolver_match.func
if hasattr(obj, '__name__'):
caller = obj.__name__
elif hasattr(obj, '__clast__') and hasattr(obj.__clast__, '__name__'):
caller = obj.__clast__.__name__
if hasattr(obj, '__module__'):
module = obj.__module__
caller = '%s.%s' % (module, caller)
t = DEBUG_ENGINE.from_string(TECHNICAL_404_TEMPLATE)
c = Context({
'urlconf': urlconf,
'root_urlconf': settings.ROOT_URLCONF,
'request_path': error_url,
'urlpatterns': tried,
'reason': force_bytes(exception, errors='replace'),
'request': request,
'settings': get_safe_settings(),
'raising_view_name': caller,
})
return HttpResponseNotFound(t.render(c), content_type='text/html')
def get_response(self, request):
"Returns an HttpResponse object for the given HttpRequest"
# Setup default url resolver for this thread, this code is outside
# the try/except so we don't get a spurious "unbound local
# variable" exception in the event an exception is raised before
# resolver is set
urlconf = settings.ROOT_URLCONF
urlresolvers.set_urlconf(urlconf)
resolver = urlresolvers.get_resolver(urlconf)
# Use a flag to check if the response was rendered to prevent
# multiple renderings or to force rendering if necessary.
response_is_rendered = False
try:
response = None
# Apply request middleware
for middleware_method in self._request_middleware:
response = middleware_method(request)
if response:
break
if response is None:
if hasattr(request, 'urlconf'):
# Reset url resolver with a custom URLconf.
urlconf = request.urlconf
urlresolvers.set_urlconf(urlconf)
resolver = urlresolvers.get_resolver(urlconf)
resolver_match = resolver.resolve(request.path_info)
callback, callback_args, callback_kwargs = resolver_match
request.resolver_match = resolver_match
# Apply view middleware
for middleware_method in self._view_middleware:
response = middleware_method(request, callback, callback_args, callback_kwargs)
if response:
break
if response is None:
wrapped_callback = self.make_view_atomic(callback)
try:
response = wrapped_callback(request, *callback_args, **callback_kwargs)
except Exception as e:
response = self.process_exception_by_middleware(e, request)
# Complain if the view returned None (a common error).
if response is None:
if isinstance(callback, types.FunctionType): # FBV
view_name = callback.__name__
else: # CBV
view_name = callback.__clast__.__name__ + '.__call__'
raise ValueError("The view %s.%s didn't return an HttpResponse object. It returned None instead."
% (callback.__module__, view_name))
# If the response supports deferred rendering, apply template
# response middleware and then render the response
if hasattr(response, 'render') and callable(response.render):
for middleware_method in self._template_response_middleware:
response = middleware_method(request, response)
# Complain if the template response middleware returned None (a common error).
if response is None:
raise ValueError(
"%s.process_template_response didn't return an "
"HttpResponse object. It returned None instead."
% (middleware_method.__self__.__clast__.__name__))
try:
response = response.render()
except Exception as e:
response = self.process_exception_by_middleware(e, request)
response_is_rendered = True
except http.Http404 as exc:
logger.warning('Not Found: %s', request.path,
extra={
'status_code': 404,
'request': request
})
if settings.DEBUG:
response = debug.technical_404_response(request, exc)
else:
response = self.get_exception_response(request, resolver, 404, exc)
except PermissionDenied as exc:
logger.warning(
'Forbidden (Permission denied): %s', request.path,
extra={
'status_code': 403,
'request': request
})
response = self.get_exception_response(request, resolver, 403, exc)
except MultiPartParserError as exc:
logger.warning(
'Bad request (Unable to parse request body): %s', request.path,
extra={
'status_code': 400,
'request': request
})
response = self.get_exception_response(request, resolver, 400, exc)
except SuspiciousOperation as exc:
# The request logger receives events for any problematic request
# The security logger receives events for all SuspiciousOperations
security_logger = logging.getLogger('django.security.%s' %
exc.__clast__.__name__)
security_logger.error(
force_text(exc),
extra={
'status_code': 400,
'request': request
})
if settings.DEBUG:
return debug.technical_500_response(request, *sys.exc_info(), status_code=400)
response = self.get_exception_response(request, resolver, 400, exc)
except SystemExit:
# Allow sys.exit() to actually exit. See tickets #1023 and #4701
raise
except: # Handle everything else.
# Get the exception info now, in case another exception is thrown later.
signals.got_request_exception.send(sender=self.__clast__, request=request)
response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
try:
# Apply response middleware, regardless of the response
for middleware_method in self._response_middleware:
response = middleware_method(request, response)
# Complain if the response middleware returned None (a common error).
if response is None:
raise ValueError(
"%s.process_response didn't return an "
"HttpResponse object. It returned None instead."
% (middleware_method.__self__.__clast__.__name__))
response = self.apply_response_fixes(request, response)
except: # Any exception should be gathered and handled
signals.got_request_exception.send(sender=self.__clast__, request=request)
response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
response._closable_objects.append(request)
# If the exception handler returns a TemplateResponse that has not
# been rendered, force it to be rendered.
if not response_is_rendered and callable(getattr(response, 'render', None)):
response = response.render()
return response
def technical_404_response(request, exception):
"Create a technical 404 error response. The exception should be the Http404."
try:
error_url = exception.args[0]['path']
except (IndexError, TypeError, KeyError):
error_url = request.path_info[1:] # Trim leading slash
try:
tried = exception.args[0]['tried']
except (IndexError, TypeError, KeyError):
tried = []
else:
if (not tried # empty URLconf
or (request.path == '/'
and len(tried) == 1 # default URLconf
and len(tried[0]) == 1
and getattr(tried[0][0], 'app_name', '') == getattr(tried[0][0], 'namespace', '') == 'admin')):
return default_urlconf(request)
urlconf = getattr(request, 'urlconf', settings.ROOT_URLCONF)
if isinstance(urlconf, types.ModuleType):
urlconf = urlconf.__name__
caller = ''
try:
resolver_match = resolve(request.path)
except Resolver404:
past
else:
obj = resolver_match.func
if hasattr(obj, '__name__'):
caller = obj.__name__
elif hasattr(obj, '__clast__') and hasattr(obj.__clast__, '__name__'):
caller = obj.__clast__.__name__
if hasattr(obj, '__module__'):
module = obj.__module__
caller = '%s.%s' % (module, caller)
t = DEBUG_ENGINE.from_string(TECHNICAL_404_TEMPLATE)
c = Context({
'urlconf': urlconf,
'root_urlconf': settings.ROOT_URLCONF,
'request_path': error_url,
'urlpatterns': tried,
'reason': force_bytes(exception, errors='replace'),
'request': request,
'settings': get_safe_settings(),
'raising_view_name': caller,
})
return HttpResponseNotFound(t.render(c), content_type='text/html')
0
View Complete Implementation : middleware.py
Copyright Apache License 2.0
Author : edisonlz
Copyright Apache License 2.0
Author : edisonlz
def process_request(self, request):
__traceback_hide__ = True
if self.show_toolbar(request):
urlconf = getattr(request, 'urlconf', settings.ROOT_URLCONF)
if isinstance(urlconf, basestring):
urlconf = import_module(getattr(request, 'urlconf', settings.ROOT_URLCONF))
if urlconf not in self._urlconfs:
new_urlconf = imp.new_module('urlconf')
new_urlconf.urlpatterns = debug_toolbar.urls.urlpatterns + \
urlconf.urlpatterns
if hasattr(urlconf, 'handler404'):
new_urlconf.handler404 = urlconf.handler404
if hasattr(urlconf, 'handler500'):
new_urlconf.handler500 = urlconf.handler500
self._urlconfs[urlconf] = new_urlconf
request.urlconf = self._urlconfs[urlconf]
toolbar = DebugToolbar(request)
for panel in toolbar.panels:
panel.process_request(request)
self.__clast__.debug_toolbars[thread.get_ident()] = toolbar
def get_response(self, request):
"Returns an HttpResponse object for the given HttpRequest"
# Setup default url resolver for this thread, this code is outside
# the try/except so we don't get a spurious "unbound local
# variable" exception in the event an exception is raised before
# resolver is set
urlconf = settings.ROOT_URLCONF
urlresolvers.set_urlconf(urlconf)
resolver = urlresolvers.RegexURLResolver(r'^/', urlconf)
try:
response = None
# Apply request middleware
for middleware_method in self._request_middleware:
response = middleware_method(request)
if response:
break
if response is None:
if hasattr(request, 'urlconf'):
# Reset url resolver with a custom urlconf.
urlconf = request.urlconf
urlresolvers.set_urlconf(urlconf)
resolver = urlresolvers.RegexURLResolver(r'^/', urlconf)
resolver_match = resolver.resolve(request.path_info)
callback, callback_args, callback_kwargs = resolver_match
request.resolver_match = resolver_match
# Apply view middleware
for middleware_method in self._view_middleware:
response = middleware_method(request, callback, callback_args, callback_kwargs)
if response:
break
if response is None:
wrapped_callback = self.make_view_atomic(callback)
try:
response = wrapped_callback(request, *callback_args, **callback_kwargs)
except Exception as e:
# If the view raised an exception, run it through exception
# middleware, and if the exception middleware returns a
# response, use that. Otherwise, reraise the exception.
for middleware_method in self._exception_middleware:
response = middleware_method(request, e)
if response:
break
if response is None:
raise
# Complain if the view returned None (a common error).
if response is None:
if isinstance(callback, types.FunctionType): # FBV
view_name = callback.__name__
else: # CBV
view_name = callback.__clast__.__name__ + '.__call__'
raise ValueError("The view %s.%s didn't return an HttpResponse object." % (callback.__module__, view_name))
# If the response supports deferred rendering, apply template
# response middleware and then render the response
if hasattr(response, 'render') and callable(response.render):
for middleware_method in self._template_response_middleware:
response = middleware_method(request, response)
response = response.render()
except http.Http404 as e:
logger.warning('Not Found: %s', request.path,
extra={
'status_code': 404,
'request': request
})
if settings.DEBUG:
response = debug.technical_404_response(request, e)
else:
try:
callback, param_dict = resolver.resolve404()
response = callback(request, **param_dict)
except:
signals.got_request_exception.send(sender=self.__clast__, request=request)
response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
except PermissionDenied:
logger.warning(
'Forbidden (Permission denied): %s', request.path,
extra={
'status_code': 403,
'request': request
})
try:
callback, param_dict = resolver.resolve403()
response = callback(request, **param_dict)
except:
signals.got_request_exception.send(
sender=self.__clast__, request=request)
response = self.handle_uncaught_exception(request,
resolver, sys.exc_info())
except SuspiciousOperation as e:
# The request logger receives events for any problematic request
# The security logger receives events for all SuspiciousOperations
security_logger = logging.getLogger('django.security.%s' %
e.__clast__.__name__)
security_logger.error(force_text(e))
try:
callback, param_dict = resolver.resolve400()
response = callback(request, **param_dict)
except:
signals.got_request_exception.send(
sender=self.__clast__, request=request)
response = self.handle_uncaught_exception(request,
resolver, sys.exc_info())
except SystemExit:
# Allow sys.exit() to actually exit. See tickets #1023 and #4701
raise
except: # Handle everything else.
# Get the exception info now, in case another exception is thrown later.
signals.got_request_exception.send(sender=self.__clast__, request=request)
response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
try:
# Apply response middleware, regardless of the response
for middleware_method in self._response_middleware:
response = middleware_method(request, response)
response = self.apply_response_fixes(request, response)
except: # Any exception should be gathered and handled
signals.got_request_exception.send(sender=self.__clast__, request=request)
response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
return response
0
View Complete Implementation : testcases.py
Copyright Apache License 2.0
Author : edisonlz
Copyright Apache License 2.0
Author : edisonlz
def _urlconf_teardown(self):
set_urlconf(None)
if hasattr(self, '_old_root_urlconf'):
settings.ROOT_URLCONF = self._old_root_urlconf
clear_url_caches()
def technical_404_response(request, exception):
"Create a technical 404 error response. The exception should be the Http404."
try:
tried = exception.args[0]['tried']
except (IndexError, TypeError, KeyError):
tried = []
else:
if (not tried # empty URLconf
or (request.path == '/'
and len(tried) == 1 # default URLconf
and len(tried[0]) == 1
and tried[0][0].app_name == tried[0][0].namespace == 'admin')):
return default_urlconf(request)
urlconf = getattr(request, 'urlconf', settings.ROOT_URLCONF)
if isinstance(urlconf, types.ModuleType):
urlconf = urlconf.__name__
t = Template(TECHNICAL_404_TEMPLATE, name='Technical 404 template')
c = Context({
'urlconf': urlconf,
'root_urlconf': settings.ROOT_URLCONF,
'request_path': request.path_info[1:], # Trim leading slash
'urlpatterns': tried,
'reason': force_bytes(exception, errors='replace'),
'request': request,
'settings': get_safe_settings(),
})
return HttpResponseNotFound(t.render(c), content_type='text/html')
0
View Complete Implementation : routers.py
Copyright MIT License
Author : Eeyhan
Copyright MIT License
Author : Eeyhan
def get_all_url_dict():
url_order_dict = OrderedDict()
root = import_string(settings.ROOT_URLCONF)
recursive_url(None, '/', root.urlpatterns, url_order_dict)
return url_order_dict
0
View Complete Implementation : finders.py
Copyright MIT License
Author : GibbsConsulting
Copyright MIT License
Author : GibbsConsulting
def __init__(self):
# Ensure urls are loaded
root_urls = settings.ROOT_URLCONF
importlib.import_module(root_urls)
# Get all registered django dash apps
self.apps = all_apps()
self.locations = []
self.storages = OrderedDict()
self.ignore_patterns = ["*.py", "*.pyc",]
added_locations = {}
for app_slug, obj in self.apps.items():
caller_module = obj.caller_module
location = obj.caller_module_location
subdir = obj.astets_folder
path_directory = os.path.join(os.path.dirname(location), subdir)
if os.path.isdir(path_directory):
component_name = app_slug
storage = FileSystemStorage(location=path_directory)
path = full_astet_path(obj.caller_module.__name__,"")
storage.prefix = path
self.locations.append(component_name)
self.storages[component_name] = storage
super(DashastetFinder, self).__init__()
0
View Complete Implementation : middleware.py
Copyright MIT License
Author : learningequality
Copyright MIT License
Author : learningequality
def __call__(self, request):
# First get the language code, and whether this was calculated from the path
# i.e. was this a language-prefixed URL.
language, language_from_path = get_language_from_request_and_is_from_path(
request
)
# If this URL has been resolved to a view, and the view is not on a language prefixed
# URL, then the function above will return None for the language code to indicate that
# no translation is necessary.
if language is not None:
# Only activate translation if there is a language code returned.
translation.activate(language)
request.LANGUAGE_CODE = translation.get_language()
response = self.get_response(request)
if language is not None:
language = translation.get_language()
if response.status_code == 404 and not language_from_path:
# Maybe the language code is missing in the URL? Try adding the
# language prefix and redirecting to that URL.
# First get any global prefix that is being used.
script_prefix = OPTIONS["Deployment"]["URL_PATH_PREFIX"]
# Replace the global prefix with the global prefix and the language prefix.
language_path = request.path_info.replace(
script_prefix, "%s%s/" % (script_prefix, language), 1
)
# Get the urlconf from the request, default to the global settings ROOT_URLCONF
urlconf = getattr(request, "urlconf", settings.ROOT_URLCONF)
# Check if this is a valid path
path_valid = is_valid_path(language_path, urlconf)
# Check if the path is only invalid because it is missing a trailing slash
path_needs_slash = not path_valid and (
settings.APPEND_SLASH
and not language_path.endswith("/")
and is_valid_path("%s/" % language_path, urlconf)
)
# If the constructed path is valid, or it would be valid with a trailing slash
# then redirect to the prefixed path, with a trailing slash added if needed.
if path_valid or path_needs_slash:
# Insert language after the script prefix and before the
# rest of the URL
language_url = request.get_full_path(
force_append_slash=path_needs_slash
).replace(script_prefix, "%s%s/" % (script_prefix, language), 1)
return HttpResponseRedirect(language_url)
# Add a content language header to the response if not already present.
if "Content-Language" not in response:
response["Content-Language"] = language
return response
def ready(self):
self.setup()
urlpatterns = import_string(settings.ROOT_URLCONF + '.urlpatterns')
urlpatterns += router.urls
def __init__(self, apps=None):
root_urlconf = importlib.import_module(settings.ROOT_URLCONF)
endpoints = self.get_all_view_names(root_urlconf.urlpatterns)
self.build_docs_tree(endpoints)
self.apps = apps
0
View Complete Implementation : checks.py
Copyright GNU Affero General Public License v3.0
Author : ra-systems
Copyright GNU Affero General Public License v3.0
Author : ra-systems
def check_custom_error_pages(app_configs, **kwargs):
# If there is a problem looking like a circular dependency
# like "(project) does not define a "urls" attribute/clast
# it's probably because urls module isn't fully loaded and functinal
# make sure to call reverse() before
errors = []
urls = import_string(settings.ROOT_URLCONF)
if not hasattr(urls, 'handler500'):
errors.append(
Error('Custom handler500 is missing',
hint='Add "handler500 = ra.utils.views.server_error" to %s' % settings.ROOT_URLCONF,
obj='urls',
id='ra.E003',
)
)
if not hasattr(urls, 'handler404'):
errors.append(
Error('Custom handler404 is missing',
hint='Add "handler404 = ra.utils.views.not_found_error " to %s' % settings.ROOT_URLCONF,
obj='urls',
id='ra.E003',
)
)
return errors
0
View Complete Implementation : base.py
Copyright MIT License
Author : rizwansoaib
Copyright MIT License
Author : rizwansoaib
def get_response(self, request):
"""Return an HttpResponse object for the given HttpRequest."""
# Setup default url resolver for this thread
set_urlconf(settings.ROOT_URLCONF)
response = self._middleware_chain(request)
response._closable_objects.append(request)
# If the exception handler returns a TemplateResponse that has not
# been rendered, force it to be rendered.
if not getattr(response, 'is_rendered', True) and callable(getattr(response, 'render', None)):
response = response.render()
if response.status_code >= 400:
log_response(
'%s: %s', response.reason_phrase, request.path,
response=response,
request=request,
)
return response
0
View Complete Implementation : locale.py
Copyright MIT License
Author : rizwansoaib
Copyright MIT License
Author : rizwansoaib
def process_response(self, request, response):
language = translation.get_language()
language_from_path = translation.get_language_from_path(request.path_info)
urlconf = getattr(request, 'urlconf', settings.ROOT_URLCONF)
i18n_patterns_used, prefixed_default_language = is_language_prefix_patterns_used(urlconf)
if (response.status_code == 404 and not language_from_path and
i18n_patterns_used and prefixed_default_language):
# Maybe the language code is missing in the URL? Try adding the
# language prefix and redirecting to that URL.
language_path = '/%s%s' % (language, request.path_info)
path_valid = is_valid_path(language_path, urlconf)
path_needs_slash = (
not path_valid and (
settings.APPEND_SLASH and not language_path.endswith('/') and
is_valid_path('%s/' % language_path, urlconf)
)
)
if path_valid or path_needs_slash:
script_prefix = get_script_prefix()
# Insert language after the script prefix and before the
# rest of the URL
language_url = request.get_full_path(force_append_slash=path_needs_slash).replace(
script_prefix,
'%s%s/' % (script_prefix, language),
1
)
return self.response_redirect_clast(language_url)
if not (i18n_patterns_used and language_from_path):
patch_vary_headers(response, ('Accept-Language',))
response.setdefault('Content-Language', language)
return response
0
View Complete Implementation : resolvers.py
Copyright MIT License
Author : rizwansoaib
Copyright MIT License
Author : rizwansoaib
@functools.lru_cache(maxsize=None)
def get_resolver(urlconf=None):
if urlconf is None:
urlconf = settings.ROOT_URLCONF
return URLResolver(RegexPattern(r'^/'), urlconf)
0
View Complete Implementation : debug.py
Copyright MIT License
Author : rizwansoaib
Copyright MIT License
Author : rizwansoaib
def technical_404_response(request, exception):
"""Create a technical 404 error response. `exception` is the Http404."""
try:
error_url = exception.args[0]['path']
except (IndexError, TypeError, KeyError):
error_url = request.path_info[1:] # Trim leading slash
try:
tried = exception.args[0]['tried']
except (IndexError, TypeError, KeyError):
tried = []
else:
if (not tried or ( # empty URLconf
request.path == '/' and
len(tried) == 1 and # default URLconf
len(tried[0]) == 1 and
getattr(tried[0][0], 'app_name', '') == getattr(tried[0][0], 'namespace', '') == 'admin'
)):
return default_urlconf(request)
urlconf = getattr(request, 'urlconf', settings.ROOT_URLCONF)
if isinstance(urlconf, types.ModuleType):
urlconf = urlconf.__name__
caller = ''
try:
resolver_match = resolve(request.path)
except Resolver404:
past
else:
obj = resolver_match.func
if hasattr(obj, '__name__'):
caller = obj.__name__
elif hasattr(obj, '__clast__') and hasattr(obj.__clast__, '__name__'):
caller = obj.__clast__.__name__
if hasattr(obj, '__module__'):
module = obj.__module__
caller = '%s.%s' % (module, caller)
with Path(CURRENT_DIR, 'templates', 'technical_404.html').open() as fh:
t = DEBUG_ENGINE.from_string(fh.read())
c = Context({
'urlconf': urlconf,
'root_urlconf': settings.ROOT_URLCONF,
'request_path': error_url,
'urlpatterns': tried,
'reason': str(exception),
'request': request,
'settings': get_safe_settings(),
'raising_view_name': caller,
})
return HttpResponseNotFound(t.render(c), content_type='text/html')
0
View Complete Implementation : middleware.py
Copyright MIT License
Author : scoutapp
Copyright MIT License
Author : scoutapp
def track_request_view_data(request, tracked_request):
path = request.path
tracked_request.tag(
"path",
create_filtered_path(
path, [(k, v) for k, vs in request.GET.lists() for v in vs]
),
)
if ignore_path(path):
tracked_request.tag("ignore_transaction", True)
try:
# Determine a remote IP to astociate with the request. The value is
# spoofable by the requester so this is not suitable to use in any
# security sensitive context.
user_ip = (
request.META.get("HTTP_X_FORWARDED_FOR", "").split(",")[0]
or request.META.get("HTTP_CLIENT_IP", "").split(",")[0]
or request.META.get("REMOTE_ADDR", None)
)
tracked_request.tag("user_ip", user_ip)
except Exception:
past
user = getattr(request, "user", None)
if user is not None:
try:
tracked_request.tag("username", user.get_username())
except Exception:
past
tracked_request.tag("urlconf", get_urlconf(settings.ROOT_URLCONF))