Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
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.

1 year ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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,
  7. RegexPattern,
  8. RoutePattern,
  9. URLPattern,
  10. URLResolver,
  11. )
  12. def include(arg, namespace=None):
  13. app_name = None
  14. if isinstance(arg, tuple):
  15. # Callable returning a namespace hint.
  16. try:
  17. urlconf_module, app_name = arg
  18. except ValueError:
  19. if namespace:
  20. raise ImproperlyConfigured(
  21. "Cannot override the namespace for a dynamic module that "
  22. "provides a namespace."
  23. )
  24. raise ImproperlyConfigured(
  25. "Passing a %d-tuple to include() is not supported. Pass a "
  26. "2-tuple containing the list of patterns and app_name, and "
  27. "provide the namespace argument to include() instead." % len(arg)
  28. )
  29. else:
  30. # No namespace hint - use manually provided namespace.
  31. urlconf_module = arg
  32. if isinstance(urlconf_module, str):
  33. urlconf_module = import_module(urlconf_module)
  34. patterns = getattr(urlconf_module, "urlpatterns", urlconf_module)
  35. app_name = getattr(urlconf_module, "app_name", app_name)
  36. if namespace and not app_name:
  37. raise ImproperlyConfigured(
  38. "Specifying a namespace in include() without providing an app_name "
  39. "is not supported. Set the app_name attribute in the included "
  40. "module, or pass a 2-tuple containing the list of patterns and "
  41. "app_name instead.",
  42. )
  43. namespace = namespace or app_name
  44. # Make sure the patterns can be iterated through (without this, some
  45. # testcases will break).
  46. if isinstance(patterns, (list, tuple)):
  47. for url_pattern in patterns:
  48. pattern = getattr(url_pattern, "pattern", None)
  49. if isinstance(pattern, LocalePrefixPattern):
  50. raise ImproperlyConfigured(
  51. "Using i18n_patterns in an included URLconf is not allowed."
  52. )
  53. return (urlconf_module, app_name, namespace)
  54. def _path(route, view, kwargs=None, name=None, Pattern=None):
  55. from django.views import View
  56. if kwargs is not None and not isinstance(kwargs, dict):
  57. raise TypeError(
  58. f"kwargs argument must be a dict, but got {kwargs.__class__.__name__}."
  59. )
  60. if isinstance(view, (list, tuple)):
  61. # For include(...) processing.
  62. pattern = Pattern(route, is_endpoint=False)
  63. urlconf_module, app_name, namespace = view
  64. return URLResolver(
  65. pattern,
  66. urlconf_module,
  67. kwargs,
  68. app_name=app_name,
  69. namespace=namespace,
  70. )
  71. elif callable(view):
  72. pattern = Pattern(route, name=name, is_endpoint=True)
  73. return URLPattern(pattern, view, kwargs, name)
  74. elif isinstance(view, View):
  75. view_cls_name = view.__class__.__name__
  76. raise TypeError(
  77. f"view must be a callable, pass {view_cls_name}.as_view(), not "
  78. f"{view_cls_name}()."
  79. )
  80. else:
  81. raise TypeError(
  82. "view must be a callable or a list/tuple in the case of include()."
  83. )
  84. path = partial(_path, Pattern=RoutePattern)
  85. re_path = partial(_path, Pattern=RegexPattern)