2015-06-16 02:07:31 +08:00
|
|
|
from importlib import import_module
|
2013-07-29 21:50:58 +08:00
|
|
|
|
2011-09-12 06:36:16 +08:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2015-12-30 23:51:16 +08:00
|
|
|
from django.urls import (
|
2015-06-16 02:07:31 +08:00
|
|
|
LocaleRegexURLResolver, RegexURLPattern, RegexURLResolver,
|
|
|
|
)
|
2012-07-20 20:22:00 +08:00
|
|
|
from django.utils import six
|
2011-09-12 06:36:16 +08:00
|
|
|
|
2015-08-17 22:45:00 +08:00
|
|
|
__all__ = ['handler400', 'handler403', 'handler404', 'handler500', 'include', 'url']
|
2011-09-12 06:36:16 +08:00
|
|
|
|
2013-05-16 07:14:28 +08:00
|
|
|
handler400 = 'django.views.defaults.bad_request'
|
2011-09-12 06:36:16 +08:00
|
|
|
handler403 = 'django.views.defaults.permission_denied'
|
|
|
|
handler404 = 'django.views.defaults.page_not_found'
|
|
|
|
handler500 = 'django.views.defaults.server_error'
|
|
|
|
|
2013-10-31 23:42:28 +08:00
|
|
|
|
2017-01-10 22:57:49 +08:00
|
|
|
def include(arg, namespace=None):
|
|
|
|
app_name = None
|
2011-09-12 06:36:16 +08:00
|
|
|
if isinstance(arg, tuple):
|
|
|
|
# callable returning a namespace hint
|
2015-05-28 23:25:52 +08:00
|
|
|
try:
|
|
|
|
urlconf_module, app_name = arg
|
|
|
|
except ValueError:
|
|
|
|
if namespace:
|
|
|
|
raise ImproperlyConfigured(
|
|
|
|
'Cannot override the namespace for a dynamic module that provides a namespace'
|
|
|
|
)
|
2017-01-10 22:57:49 +08:00
|
|
|
raise ImproperlyConfigured(
|
|
|
|
'Passing a 3-tuple to django.conf.urls.include() is not supported. '
|
2015-05-28 23:25:52 +08:00
|
|
|
'Pass a 2-tuple containing the list of patterns and app_name, '
|
|
|
|
'and provide the namespace argument to include() instead.',
|
|
|
|
)
|
2011-09-12 06:36:16 +08:00
|
|
|
else:
|
|
|
|
# No namespace hint - use manually provided namespace
|
|
|
|
urlconf_module = arg
|
|
|
|
|
2012-07-20 20:22:00 +08:00
|
|
|
if isinstance(urlconf_module, six.string_types):
|
2011-09-12 06:36:16 +08:00
|
|
|
urlconf_module = import_module(urlconf_module)
|
|
|
|
patterns = getattr(urlconf_module, 'urlpatterns', urlconf_module)
|
2015-05-28 23:25:52 +08:00
|
|
|
app_name = getattr(urlconf_module, 'app_name', app_name)
|
|
|
|
if namespace and not app_name:
|
2017-01-10 22:57:49 +08:00
|
|
|
raise ImproperlyConfigured(
|
2015-05-28 23:25:52 +08:00
|
|
|
'Specifying a namespace in django.conf.urls.include() without '
|
2017-01-10 22:57:49 +08:00
|
|
|
'providing an app_name is not supported. Set the app_name attribute '
|
2015-05-28 23:25:52 +08:00
|
|
|
'in the included module, or pass a 2-tuple containing the list of '
|
|
|
|
'patterns and app_name instead.',
|
|
|
|
)
|
|
|
|
|
|
|
|
namespace = namespace or app_name
|
2011-09-12 06:36:16 +08:00
|
|
|
|
|
|
|
# Make sure we can iterate through the patterns (without this, some
|
|
|
|
# testcases will break).
|
|
|
|
if isinstance(patterns, (list, tuple)):
|
|
|
|
for url_pattern in patterns:
|
|
|
|
# Test if the LocaleRegexURLResolver is used within the include;
|
|
|
|
# this should throw an error since this is not allowed!
|
|
|
|
if isinstance(url_pattern, LocaleRegexURLResolver):
|
|
|
|
raise ImproperlyConfigured(
|
|
|
|
'Using i18n_patterns in an included URLconf is not allowed.')
|
|
|
|
|
|
|
|
return (urlconf_module, app_name, namespace)
|
|
|
|
|
2013-10-31 23:42:28 +08:00
|
|
|
|
2015-08-17 23:07:03 +08:00
|
|
|
def url(regex, view, kwargs=None, name=None):
|
2013-10-25 01:30:03 +08:00
|
|
|
if isinstance(view, (list, tuple)):
|
2011-09-12 06:36:16 +08:00
|
|
|
# For include(...) processing.
|
|
|
|
urlconf_module, app_name, namespace = view
|
|
|
|
return RegexURLResolver(regex, urlconf_module, kwargs, app_name=app_name, namespace=namespace)
|
2015-08-18 01:45:07 +08:00
|
|
|
elif callable(view):
|
2011-09-12 06:36:16 +08:00
|
|
|
return RegexURLPattern(regex, view, kwargs, name)
|
2015-08-18 01:45:07 +08:00
|
|
|
else:
|
|
|
|
raise TypeError('view must be a callable or a list/tuple in the case of include().')
|