Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/django/urls/conf.py: 18%
44 statements
« prev ^ index » next coverage.py v7.0.5, created at 2023-01-17 06:13 +0000
« prev ^ index » next coverage.py v7.0.5, created at 2023-01-17 06:13 +0000
1"""Functions for use in URLsconfs."""
2from functools import partial
3from importlib import import_module
5from django.core.exceptions import ImproperlyConfigured
7from .resolvers import (
8 LocalePrefixPattern,
9 RegexPattern,
10 RoutePattern,
11 URLPattern,
12 URLResolver,
13)
16def include(arg, namespace=None):
17 app_name = None
18 if isinstance(arg, tuple):
19 # Callable returning a namespace hint.
20 try:
21 urlconf_module, app_name = arg
22 except ValueError:
23 if namespace:
24 raise ImproperlyConfigured(
25 "Cannot override the namespace for a dynamic module that "
26 "provides a namespace."
27 )
28 raise ImproperlyConfigured(
29 "Passing a %d-tuple to include() is not supported. Pass a "
30 "2-tuple containing the list of patterns and app_name, and "
31 "provide the namespace argument to include() instead." % len(arg)
32 )
33 else:
34 # No namespace hint - use manually provided namespace.
35 urlconf_module = arg
37 if isinstance(urlconf_module, str):
38 urlconf_module = import_module(urlconf_module)
39 patterns = getattr(urlconf_module, "urlpatterns", urlconf_module)
40 app_name = getattr(urlconf_module, "app_name", app_name)
41 if namespace and not app_name:
42 raise ImproperlyConfigured(
43 "Specifying a namespace in include() without providing an app_name "
44 "is not supported. Set the app_name attribute in the included "
45 "module, or pass a 2-tuple containing the list of patterns and "
46 "app_name instead.",
47 )
48 namespace = namespace or app_name
49 # Make sure the patterns can be iterated through (without this, some
50 # testcases will break).
51 if isinstance(patterns, (list, tuple)):
52 for url_pattern in patterns:
53 pattern = getattr(url_pattern, "pattern", None)
54 if isinstance(pattern, LocalePrefixPattern):
55 raise ImproperlyConfigured(
56 "Using i18n_patterns in an included URLconf is not allowed."
57 )
58 return (urlconf_module, app_name, namespace)
61def _path(route, view, kwargs=None, name=None, Pattern=None):
62 from django.views import View
64 if kwargs is not None and not isinstance(kwargs, dict):
65 raise TypeError(
66 f"kwargs argument must be a dict, but got {kwargs.__class__.__name__}."
67 )
68 if isinstance(view, (list, tuple)):
69 # For include(...) processing.
70 pattern = Pattern(route, is_endpoint=False)
71 urlconf_module, app_name, namespace = view
72 return URLResolver(
73 pattern,
74 urlconf_module,
75 kwargs,
76 app_name=app_name,
77 namespace=namespace,
78 )
79 elif callable(view):
80 pattern = Pattern(route, name=name, is_endpoint=True)
81 return URLPattern(pattern, view, kwargs, name)
82 elif isinstance(view, View):
83 view_cls_name = view.__class__.__name__
84 raise TypeError(
85 f"view must be a callable, pass {view_cls_name}.as_view(), not "
86 f"{view_cls_name}()."
87 )
88 else:
89 raise TypeError(
90 "view must be a callable or a list/tuple in the case of include()."
91 )
94path = partial(_path, Pattern=RoutePattern)
95re_path = partial(_path, Pattern=RegexPattern)