Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/django/urls/conf.py: 71%
42 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
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: 41 ↛ 42line 41 didn't jump to line 42, because the condition on line 41 was never true
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)): 51 ↛ 58line 51 didn't jump to line 58, because the condition on line 51 was never false
52 for url_pattern in patterns:
53 pattern = getattr(url_pattern, "pattern", None)
54 if isinstance(pattern, LocalePrefixPattern): 54 ↛ 55line 54 didn't jump to line 55, because the condition on line 54 was never true
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 isinstance(view, (list, tuple)):
65 # For include(...) processing.
66 pattern = Pattern(route, is_endpoint=False)
67 urlconf_module, app_name, namespace = view
68 return URLResolver(
69 pattern,
70 urlconf_module,
71 kwargs,
72 app_name=app_name,
73 namespace=namespace,
74 )
75 elif callable(view): 75 ↛ 78line 75 didn't jump to line 78, because the condition on line 75 was never false
76 pattern = Pattern(route, name=name, is_endpoint=True)
77 return URLPattern(pattern, view, kwargs, name)
78 elif isinstance(view, View):
79 view_cls_name = view.__class__.__name__
80 raise TypeError(
81 f"view must be a callable, pass {view_cls_name}.as_view(), not "
82 f"{view_cls_name}()."
83 )
84 else:
85 raise TypeError(
86 "view must be a callable or a list/tuple in the case of include()."
87 )
90path = partial(_path, Pattern=RoutePattern)
91re_path = partial(_path, Pattern=RegexPattern)