functions for use in urlsconfs from functools import partial from importlib import importmodule from django core exceptions import improperlyconfigured from resolvers import localeprefixpattern regexpattern routepattern urlpattern urlresolver def includearg namespacenone appname none if isinstancearg tuple callable returning a namespace hint try urlconfmodule appname arg except valueerror if namespace raise improperlyconfigured cannot override the namespace for a dynamic module that provides a namespace raise improperlyconfigured passing a dtuple to include is not supported pass a 2tuple containing the list of patterns and appname and provide the namespace argument to include instead lenarg else no namespace hint use manually provided namespace urlconfmodule arg if isinstanceurlconfmodule str urlconfmodule importmoduleurlconfmodule patterns getattrurlconfmodule urlpatterns urlconfmodule appname getattrurlconfmodule appname appname if namespace and not appname raise improperlyconfigured specifying a namespace in include without providing an appname is not supported set the appname attribute in the included module or pass a 2tuple containing the list of patterns and appname instead namespace namespace or appname make sure the patterns can be iterated through without this some testcases will break if isinstancepatterns list tuple for urlpattern in patterns pattern getattrurlpattern pattern none if isinstancepattern localeprefixpattern raise improperlyconfigured using i18npatterns in an included urlconf is not allowed return urlconfmodule appname namespace def pathroute view kwargsnone namenone patternnone from django views import view if kwargs is not none and not isinstancekwargs dict raise typeerror fkwargs argument must be a dict but got kwargs class name if isinstanceview list tuple for include processing pattern patternroute isendpointfalse urlconfmodule appname namespace view return urlresolver pattern urlconfmodule kwargs appnameappname namespacenamespace elif callableview pattern patternroute namename isendpointtrue return urlpatternpattern view kwargs name elif isinstanceview view viewclsname view class name raise typeerror fview must be a callable pass viewclsname asview not fviewclsname else raise typeerror view must be a callable or a listtuple in the case of include path partialpath patternroutepattern repath partialpath patternregexpattern callable returning a namespace hint no namespace hint use manually provided namespace make sure the patterns can be iterated through without this some testcases will break for include processing
from functools import partial
from importlib import import_module

from django.core.exceptions import ImproperlyConfigured

from .resolvers import (
    LocalePrefixPattern,
    RegexPattern,
    RoutePattern,
    URLPattern,
    URLResolver,
)


def include(arg, namespace=None):
    app_name = None
    if isinstance(arg, tuple):
        
        try:
            urlconf_module, app_name = arg
        except ValueError:
            if namespace:
                raise ImproperlyConfigured(
                    "Cannot override the namespace for a dynamic module that "
                    "provides a namespace."
                )
            raise ImproperlyConfigured(
                "Passing a %d-tuple to include() is not supported. Pass a "
                "2-tuple containing the list of patterns and app_name, and "
                "provide the namespace argument to include() instead." % len(arg)
            )
    else:
        
        urlconf_module = arg

    if isinstance(urlconf_module, str):
        urlconf_module = import_module(urlconf_module)
    patterns = getattr(urlconf_module, "urlpatterns", urlconf_module)
    app_name = getattr(urlconf_module, "app_name", app_name)
    if namespace and not app_name:
        raise ImproperlyConfigured(
            "Specifying a namespace in include() without providing an app_name "
            "is not supported. Set the app_name attribute in the included "
            "module, or pass a 2-tuple containing the list of patterns and "
            "app_name instead.",
        )
    namespace = namespace or app_name
    
    
    if isinstance(patterns, (list, tuple)):
        for url_pattern in patterns:
            pattern = getattr(url_pattern, "pattern", None)
            if isinstance(pattern, LocalePrefixPattern):
                raise ImproperlyConfigured(
                    "Using i18n_patterns in an included URLconf is not allowed."
                )
    return (urlconf_module, app_name, namespace)


def _path(route, view, kwargs=None, name=None, Pattern=None):
    from django.views import View

    if kwargs is not None and not isinstance(kwargs, dict):
        raise TypeError(
            f"kwargs argument must be a dict, but got {kwargs.__class__.__name__}."
        )
    if isinstance(view, (list, tuple)):
        
        pattern = Pattern(route, is_endpoint=False)
        urlconf_module, app_name, namespace = view
        return URLResolver(
            pattern,
            urlconf_module,
            kwargs,
            app_name=app_name,
            namespace=namespace,
        )
    elif callable(view):
        pattern = Pattern(route, name=name, is_endpoint=True)
        return URLPattern(pattern, view, kwargs, name)
    elif isinstance(view, View):
        view_cls_name = view.__class__.__name__
        raise TypeError(
            f"view must be a callable, pass {view_cls_name}.as_view(), not "
            f"{view_cls_name}()."
        )
    else:
        raise TypeError(
            "view must be a callable or a list/tuple in the case of include()."
        )


path = partial(_path, Pattern=RoutePattern)
re_path = partial(_path, Pattern=RegexPattern)