You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. """Functions for use in URLsconfs."""
  2. from functools import partial
  3. from importlib import import_module
  4. from django.core.exceptions import ImproperlyConfigured
  5. from .resolvers import (
  6. LocalePrefixPattern, RegexPattern, RoutePattern, URLPattern, URLResolver,
  7. )
  8. def include(arg, namespace=None):
  9. app_name = None
  10. if isinstance(arg, tuple):
  11. # Callable returning a namespace hint.
  12. try:
  13. urlconf_module, app_name = arg
  14. except ValueError:
  15. if namespace:
  16. raise ImproperlyConfigured(
  17. 'Cannot override the namespace for a dynamic module that '
  18. 'provides a namespace.'
  19. )
  20. raise ImproperlyConfigured(
  21. 'Passing a %d-tuple to include() is not supported. Pass a '
  22. '2-tuple containing the list of patterns and app_name, and '
  23. 'provide the namespace argument to include() instead.' % len(arg)
  24. )
  25. else:
  26. # No namespace hint - use manually provided namespace.
  27. urlconf_module = arg
  28. if isinstance(urlconf_module, str):
  29. urlconf_module = import_module(urlconf_module)
  30. patterns = getattr(urlconf_module, 'urlpatterns', urlconf_module)
  31. app_name = getattr(urlconf_module, 'app_name', app_name)
  32. if namespace and not app_name:
  33. raise ImproperlyConfigured(
  34. 'Specifying a namespace in include() without providing an app_name '
  35. 'is not supported. Set the app_name attribute in the included '
  36. 'module, or pass a 2-tuple containing the list of patterns and '
  37. 'app_name instead.',
  38. )
  39. namespace = namespace or app_name
  40. # Make sure the patterns can be iterated through (without this, some
  41. # testcases will break).
  42. if isinstance(patterns, (list, tuple)):
  43. for url_pattern in patterns:
  44. pattern = getattr(url_pattern, 'pattern', None)
  45. if isinstance(pattern, LocalePrefixPattern):
  46. raise ImproperlyConfigured(
  47. 'Using i18n_patterns in an included URLconf is not allowed.'
  48. )
  49. return (urlconf_module, app_name, namespace)
  50. def _path(route, view, kwargs=None, name=None, Pattern=None):
  51. if isinstance(view, (list, tuple)):
  52. # For include(...) processing.
  53. pattern = Pattern(route, is_endpoint=False)
  54. urlconf_module, app_name, namespace = view
  55. return URLResolver(
  56. pattern,
  57. urlconf_module,
  58. kwargs,
  59. app_name=app_name,
  60. namespace=namespace,
  61. )
  62. elif callable(view):
  63. pattern = Pattern(route, name=name, is_endpoint=True)
  64. return URLPattern(pattern, view, kwargs, name)
  65. else:
  66. raise TypeError('view must be a callable or a list/tuple in the case of include().')
  67. path = partial(_path, Pattern=RoutePattern)
  68. re_path = partial(_path, Pattern=RegexPattern)