urlresolvers.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. """
  2. This module converts requested URLs to callback view functions.
  3. RegexURLResolver is the main class here. Its resolve() method takes a URL (as
  4. a string) and returns a tuple in this format:
  5. (view_function, function_args, function_kwargs)
  6. """
  7. from __future__ import unicode_literals
  8. import functools
  9. from importlib import import_module
  10. import re
  11. from threading import local
  12. from django.http import Http404
  13. from django.core.exceptions import ImproperlyConfigured, ViewDoesNotExist
  14. from django.utils.datastructures import MultiValueDict
  15. from django.utils.encoding import force_str, force_text, iri_to_uri
  16. from django.utils.functional import lazy
  17. from django.utils.http import urlquote
  18. from django.utils.module_loading import module_has_submodule
  19. from django.utils.regex_helper import normalize
  20. from django.utils import six, lru_cache
  21. from django.utils.translation import get_language
  22. # SCRIPT_NAME prefixes for each thread are stored here. If there's no entry for
  23. # the current thread (which is the only one we ever access), it is assumed to
  24. # be empty.
  25. _prefixes = local()
  26. # Overridden URLconfs for each thread are stored here.
  27. _urlconfs = local()
  28. class ResolverMatch(object):
  29. def __init__(self, func, args, kwargs, url_name=None, app_name=None, namespaces=None):
  30. self.func = func
  31. self.args = args
  32. self.kwargs = kwargs
  33. self.app_name = app_name
  34. if namespaces:
  35. self.namespaces = [x for x in namespaces if x]
  36. else:
  37. self.namespaces = []
  38. if not url_name:
  39. if not hasattr(func, '__name__'):
  40. # An instance of a callable class
  41. url_name = '.'.join([func.__class__.__module__, func.__class__.__name__])
  42. else:
  43. # A function
  44. url_name = '.'.join([func.__module__, func.__name__])
  45. self.url_name = url_name
  46. @property
  47. def namespace(self):
  48. return ':'.join(self.namespaces)
  49. @property
  50. def view_name(self):
  51. return ':'.join(filter(bool, (self.namespace, self.url_name)))
  52. def __getitem__(self, index):
  53. return (self.func, self.args, self.kwargs)[index]
  54. def __repr__(self):
  55. return "ResolverMatch(func=%s, args=%s, kwargs=%s, url_name='%s', app_name='%s', namespace='%s')" % (
  56. self.func, self.args, self.kwargs, self.url_name, self.app_name, self.namespace)
  57. class Resolver404(Http404):
  58. pass
  59. class NoReverseMatch(Exception):
  60. pass
  61. @lru_cache.lru_cache(maxsize=None)
  62. def get_callable(lookup_view, can_fail=False):
  63. """
  64. Convert a string version of a function name to the callable object.
  65. If the lookup_view is not an import path, it is assumed to be a URL pattern
  66. label and the original string is returned.
  67. If can_fail is True, lookup_view might be a URL pattern label, so errors
  68. during the import fail and the string is returned.
  69. """
  70. if not callable(lookup_view):
  71. mod_name, func_name = get_mod_func(lookup_view)
  72. if func_name == '':
  73. return lookup_view
  74. try:
  75. mod = import_module(mod_name)
  76. except ImportError:
  77. parentmod, submod = get_mod_func(mod_name)
  78. if (not can_fail and submod != '' and
  79. not module_has_submodule(import_module(parentmod), submod)):
  80. raise ViewDoesNotExist(
  81. "Could not import %s. Parent module %s does not exist." %
  82. (lookup_view, mod_name))
  83. if not can_fail:
  84. raise
  85. else:
  86. try:
  87. lookup_view = getattr(mod, func_name)
  88. if not callable(lookup_view):
  89. raise ViewDoesNotExist(
  90. "Could not import %s.%s. View is not callable." %
  91. (mod_name, func_name))
  92. except AttributeError:
  93. if not can_fail:
  94. raise ViewDoesNotExist(
  95. "Could not import %s. View does not exist in module %s." %
  96. (lookup_view, mod_name))
  97. return lookup_view
  98. @lru_cache.lru_cache(maxsize=None)
  99. def get_resolver(urlconf):
  100. if urlconf is None:
  101. from django.conf import settings
  102. urlconf = settings.ROOT_URLCONF
  103. return RegexURLResolver(r'^/', urlconf)
  104. @lru_cache.lru_cache(maxsize=None)
  105. def get_ns_resolver(ns_pattern, resolver):
  106. # Build a namespaced resolver for the given parent urlconf pattern.
  107. # This makes it possible to have captured parameters in the parent
  108. # urlconf pattern.
  109. ns_resolver = RegexURLResolver(ns_pattern, resolver.url_patterns)
  110. return RegexURLResolver(r'^/', [ns_resolver])
  111. def get_mod_func(callback):
  112. # Converts 'django.views.news.stories.story_detail' to
  113. # ['django.views.news.stories', 'story_detail']
  114. try:
  115. dot = callback.rindex('.')
  116. except ValueError:
  117. return callback, ''
  118. return callback[:dot], callback[dot + 1:]
  119. class LocaleRegexProvider(object):
  120. """
  121. A mixin to provide a default regex property which can vary by active
  122. language.
  123. """
  124. def __init__(self, regex):
  125. # regex is either a string representing a regular expression, or a
  126. # translatable string (using ugettext_lazy) representing a regular
  127. # expression.
  128. self._regex = regex
  129. self._regex_dict = {}
  130. @property
  131. def regex(self):
  132. """
  133. Returns a compiled regular expression, depending upon the activated
  134. language-code.
  135. """
  136. language_code = get_language()
  137. if language_code not in self._regex_dict:
  138. if isinstance(self._regex, six.string_types):
  139. regex = self._regex
  140. else:
  141. regex = force_text(self._regex)
  142. try:
  143. compiled_regex = re.compile(regex, re.UNICODE)
  144. except re.error as e:
  145. raise ImproperlyConfigured(
  146. '"%s" is not a valid regular expression: %s' %
  147. (regex, six.text_type(e)))
  148. self._regex_dict[language_code] = compiled_regex
  149. return self._regex_dict[language_code]
  150. class RegexURLPattern(LocaleRegexProvider):
  151. def __init__(self, regex, callback, default_args=None, name=None):
  152. LocaleRegexProvider.__init__(self, regex)
  153. # callback is either a string like 'foo.views.news.stories.story_detail'
  154. # which represents the path to a module and a view function name, or a
  155. # callable object (view).
  156. if callable(callback):
  157. self._callback = callback
  158. else:
  159. self._callback = None
  160. self._callback_str = callback
  161. self.default_args = default_args or {}
  162. self.name = name
  163. def __repr__(self):
  164. return force_str('<%s %s %s>' % (self.__class__.__name__, self.name, self.regex.pattern))
  165. def add_prefix(self, prefix):
  166. """
  167. Adds the prefix string to a string-based callback.
  168. """
  169. if not prefix or not hasattr(self, '_callback_str'):
  170. return
  171. self._callback_str = prefix + '.' + self._callback_str
  172. def resolve(self, path):
  173. match = self.regex.search(path)
  174. if match:
  175. # If there are any named groups, use those as kwargs, ignoring
  176. # non-named groups. Otherwise, pass all non-named arguments as
  177. # positional arguments.
  178. kwargs = match.groupdict()
  179. if kwargs:
  180. args = ()
  181. else:
  182. args = match.groups()
  183. # In both cases, pass any extra_kwargs as **kwargs.
  184. kwargs.update(self.default_args)
  185. return ResolverMatch(self.callback, args, kwargs, self.name)
  186. @property
  187. def callback(self):
  188. if self._callback is not None:
  189. return self._callback
  190. self._callback = get_callable(self._callback_str)
  191. return self._callback
  192. class RegexURLResolver(LocaleRegexProvider):
  193. def __init__(self, regex, urlconf_name, default_kwargs=None, app_name=None, namespace=None):
  194. LocaleRegexProvider.__init__(self, regex)
  195. # urlconf_name is a string representing the module containing URLconfs.
  196. self.urlconf_name = urlconf_name
  197. if not isinstance(urlconf_name, six.string_types):
  198. self._urlconf_module = self.urlconf_name
  199. self.callback = None
  200. self.default_kwargs = default_kwargs or {}
  201. self.namespace = namespace
  202. self.app_name = app_name
  203. self._reverse_dict = {}
  204. self._namespace_dict = {}
  205. self._app_dict = {}
  206. # set of dotted paths to all functions and classes that are used in
  207. # urlpatterns
  208. self._callback_strs = set()
  209. self._populated = False
  210. def __repr__(self):
  211. if isinstance(self.urlconf_name, list) and len(self.urlconf_name):
  212. # Don't bother to output the whole list, it can be huge
  213. urlconf_repr = '<%s list>' % self.urlconf_name[0].__class__.__name__
  214. else:
  215. urlconf_repr = repr(self.urlconf_name)
  216. return str('<%s %s (%s:%s) %s>') % (
  217. self.__class__.__name__, urlconf_repr, self.app_name,
  218. self.namespace, self.regex.pattern)
  219. def _populate(self):
  220. lookups = MultiValueDict()
  221. namespaces = {}
  222. apps = {}
  223. language_code = get_language()
  224. for pattern in reversed(self.url_patterns):
  225. if hasattr(pattern, '_callback_str'):
  226. self._callback_strs.add(pattern._callback_str)
  227. elif hasattr(pattern, '_callback'):
  228. callback = pattern._callback
  229. if isinstance(callback, functools.partial):
  230. callback = callback.func
  231. if not hasattr(callback, '__name__'):
  232. lookup_str = callback.__module__ + "." + callback.__class__.__name__
  233. else:
  234. lookup_str = callback.__module__ + "." + callback.__name__
  235. self._callback_strs.add(lookup_str)
  236. p_pattern = pattern.regex.pattern
  237. if p_pattern.startswith('^'):
  238. p_pattern = p_pattern[1:]
  239. if isinstance(pattern, RegexURLResolver):
  240. if pattern.namespace:
  241. namespaces[pattern.namespace] = (p_pattern, pattern)
  242. if pattern.app_name:
  243. apps.setdefault(pattern.app_name, []).append(pattern.namespace)
  244. else:
  245. parent_pat = pattern.regex.pattern
  246. for name in pattern.reverse_dict:
  247. for matches, pat, defaults in pattern.reverse_dict.getlist(name):
  248. new_matches = normalize(parent_pat + pat)
  249. lookups.appendlist(name, (new_matches, p_pattern + pat, dict(defaults, **pattern.default_kwargs)))
  250. for namespace, (prefix, sub_pattern) in pattern.namespace_dict.items():
  251. namespaces[namespace] = (p_pattern + prefix, sub_pattern)
  252. for app_name, namespace_list in pattern.app_dict.items():
  253. apps.setdefault(app_name, []).extend(namespace_list)
  254. self._callback_strs.update(pattern._callback_strs)
  255. else:
  256. bits = normalize(p_pattern)
  257. lookups.appendlist(pattern.callback, (bits, p_pattern, pattern.default_args))
  258. if pattern.name is not None:
  259. lookups.appendlist(pattern.name, (bits, p_pattern, pattern.default_args))
  260. self._reverse_dict[language_code] = lookups
  261. self._namespace_dict[language_code] = namespaces
  262. self._app_dict[language_code] = apps
  263. self._populated = True
  264. @property
  265. def reverse_dict(self):
  266. language_code = get_language()
  267. if language_code not in self._reverse_dict:
  268. self._populate()
  269. return self._reverse_dict[language_code]
  270. @property
  271. def namespace_dict(self):
  272. language_code = get_language()
  273. if language_code not in self._namespace_dict:
  274. self._populate()
  275. return self._namespace_dict[language_code]
  276. @property
  277. def app_dict(self):
  278. language_code = get_language()
  279. if language_code not in self._app_dict:
  280. self._populate()
  281. return self._app_dict[language_code]
  282. def resolve(self, path):
  283. path = force_text(path) # path may be a reverse_lazy object
  284. tried = []
  285. match = self.regex.search(path)
  286. if match:
  287. new_path = path[match.end():]
  288. for pattern in self.url_patterns:
  289. try:
  290. sub_match = pattern.resolve(new_path)
  291. except Resolver404 as e:
  292. sub_tried = e.args[0].get('tried')
  293. if sub_tried is not None:
  294. tried.extend([pattern] + t for t in sub_tried)
  295. else:
  296. tried.append([pattern])
  297. else:
  298. if sub_match:
  299. sub_match_dict = dict(match.groupdict(), **self.default_kwargs)
  300. sub_match_dict.update(sub_match.kwargs)
  301. return ResolverMatch(sub_match.func, sub_match.args, sub_match_dict, sub_match.url_name, self.app_name or sub_match.app_name, [self.namespace] + sub_match.namespaces)
  302. tried.append([pattern])
  303. raise Resolver404({'tried': tried, 'path': new_path})
  304. raise Resolver404({'path': path})
  305. @property
  306. def urlconf_module(self):
  307. try:
  308. return self._urlconf_module
  309. except AttributeError:
  310. self._urlconf_module = import_module(self.urlconf_name)
  311. return self._urlconf_module
  312. @property
  313. def url_patterns(self):
  314. # urlconf_module might be a valid set of patterns, so we default to it
  315. patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  316. try:
  317. iter(patterns)
  318. except TypeError:
  319. msg = (
  320. "The included urlconf '{name}' does not appear to have any "
  321. "patterns in it. If you see valid patterns in the file then "
  322. "the issue is probably caused by a circular import."
  323. )
  324. raise ImproperlyConfigured(msg.format(name=self.urlconf_name))
  325. return patterns
  326. def _resolve_special(self, view_type):
  327. callback = getattr(self.urlconf_module, 'handler%s' % view_type, None)
  328. if not callback:
  329. # No handler specified in file; use default
  330. # Lazy import, since django.urls imports this file
  331. from django.conf import urls
  332. callback = getattr(urls, 'handler%s' % view_type)
  333. return get_callable(callback), {}
  334. def resolve400(self):
  335. return self._resolve_special('400')
  336. def resolve403(self):
  337. return self._resolve_special('403')
  338. def resolve404(self):
  339. return self._resolve_special('404')
  340. def resolve500(self):
  341. return self._resolve_special('500')
  342. def reverse(self, lookup_view, *args, **kwargs):
  343. return self._reverse_with_prefix(lookup_view, '', *args, **kwargs)
  344. def _reverse_with_prefix(self, lookup_view, _prefix, *args, **kwargs):
  345. if args and kwargs:
  346. raise ValueError("Don't mix *args and **kwargs in call to reverse()!")
  347. text_args = [force_text(v) for v in args]
  348. text_kwargs = dict((k, force_text(v)) for (k, v) in kwargs.items())
  349. if not self._populated:
  350. self._populate()
  351. try:
  352. if lookup_view in self._callback_strs:
  353. lookup_view = get_callable(lookup_view, True)
  354. except (ImportError, AttributeError) as e:
  355. raise NoReverseMatch("Error importing '%s': %s." % (lookup_view, e))
  356. possibilities = self.reverse_dict.getlist(lookup_view)
  357. prefix_norm, prefix_args = normalize(urlquote(_prefix))[0]
  358. for possibility, pattern, defaults in possibilities:
  359. for result, params in possibility:
  360. if args:
  361. if len(args) != len(params) + len(prefix_args):
  362. continue
  363. candidate_subs = dict(zip(prefix_args + params, text_args))
  364. else:
  365. if set(kwargs.keys()) | set(defaults.keys()) != set(params) | set(defaults.keys()) | set(prefix_args):
  366. continue
  367. matches = True
  368. for k, v in defaults.items():
  369. if kwargs.get(k, v) != v:
  370. matches = False
  371. break
  372. if not matches:
  373. continue
  374. candidate_subs = text_kwargs
  375. # WSGI provides decoded URLs, without %xx escapes, and the URL
  376. # resolver operates on such URLs. First substitute arguments
  377. # without quoting to build a decoded URL and look for a match.
  378. # Then, if we have a match, redo the substitution with quoted
  379. # arguments in order to return a properly encoded URL.
  380. candidate_pat = prefix_norm.replace('%', '%%') + result
  381. if re.search('^%s%s' % (prefix_norm, pattern), candidate_pat % candidate_subs, re.UNICODE):
  382. candidate_subs = dict((k, urlquote(v)) for (k, v) in candidate_subs.items())
  383. url = candidate_pat % candidate_subs
  384. # Don't allow construction of scheme relative urls.
  385. if url.startswith('//'):
  386. url = '/%%2F%s' % url[2:]
  387. return url
  388. # lookup_view can be URL label, or dotted path, or callable, Any of
  389. # these can be passed in at the top, but callables are not friendly in
  390. # error messages.
  391. m = getattr(lookup_view, '__module__', None)
  392. n = getattr(lookup_view, '__name__', None)
  393. if m is not None and n is not None:
  394. lookup_view_s = "%s.%s" % (m, n)
  395. else:
  396. lookup_view_s = lookup_view
  397. patterns = [pattern for (possibility, pattern, defaults) in possibilities]
  398. raise NoReverseMatch("Reverse for '%s' with arguments '%s' and keyword "
  399. "arguments '%s' not found. %d pattern(s) tried: %s" %
  400. (lookup_view_s, args, kwargs, len(patterns), patterns))
  401. class LocaleRegexURLResolver(RegexURLResolver):
  402. """
  403. A URL resolver that always matches the active language code as URL prefix.
  404. Rather than taking a regex argument, we just override the ``regex``
  405. function to always return the active language-code as regex.
  406. """
  407. def __init__(self, urlconf_name, default_kwargs=None, app_name=None, namespace=None):
  408. super(LocaleRegexURLResolver, self).__init__(
  409. None, urlconf_name, default_kwargs, app_name, namespace)
  410. @property
  411. def regex(self):
  412. language_code = get_language()
  413. if language_code not in self._regex_dict:
  414. regex_compiled = re.compile('^%s/' % language_code, re.UNICODE)
  415. self._regex_dict[language_code] = regex_compiled
  416. return self._regex_dict[language_code]
  417. def resolve(path, urlconf=None):
  418. if urlconf is None:
  419. urlconf = get_urlconf()
  420. return get_resolver(urlconf).resolve(path)
  421. def reverse(viewname, urlconf=None, args=None, kwargs=None, prefix=None, current_app=None):
  422. if urlconf is None:
  423. urlconf = get_urlconf()
  424. resolver = get_resolver(urlconf)
  425. args = args or []
  426. kwargs = kwargs or {}
  427. if prefix is None:
  428. prefix = get_script_prefix()
  429. if not isinstance(viewname, six.string_types):
  430. view = viewname
  431. else:
  432. parts = viewname.split(':')
  433. parts.reverse()
  434. view = parts[0]
  435. path = parts[1:]
  436. resolved_path = []
  437. ns_pattern = ''
  438. while path:
  439. ns = path.pop()
  440. # Lookup the name to see if it could be an app identifier
  441. try:
  442. app_list = resolver.app_dict[ns]
  443. # Yes! Path part matches an app in the current Resolver
  444. if current_app and current_app in app_list:
  445. # If we are reversing for a particular app,
  446. # use that namespace
  447. ns = current_app
  448. elif ns not in app_list:
  449. # The name isn't shared by one of the instances
  450. # (i.e., the default) so just pick the first instance
  451. # as the default.
  452. ns = app_list[0]
  453. except KeyError:
  454. pass
  455. try:
  456. extra, resolver = resolver.namespace_dict[ns]
  457. resolved_path.append(ns)
  458. ns_pattern = ns_pattern + extra
  459. except KeyError as key:
  460. if resolved_path:
  461. raise NoReverseMatch(
  462. "%s is not a registered namespace inside '%s'" %
  463. (key, ':'.join(resolved_path)))
  464. else:
  465. raise NoReverseMatch("%s is not a registered namespace" %
  466. key)
  467. if ns_pattern:
  468. resolver = get_ns_resolver(ns_pattern, resolver)
  469. return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
  470. reverse_lazy = lazy(reverse, str)
  471. def clear_url_caches():
  472. get_callable.cache_clear()
  473. get_resolver.cache_clear()
  474. get_ns_resolver.cache_clear()
  475. def set_script_prefix(prefix):
  476. """
  477. Sets the script prefix for the current thread.
  478. """
  479. if not prefix.endswith('/'):
  480. prefix += '/'
  481. _prefixes.value = prefix
  482. def get_script_prefix():
  483. """
  484. Returns the currently active script prefix. Useful for client code that
  485. wishes to construct their own URLs manually (although accessing the request
  486. instance is normally going to be a lot cleaner).
  487. """
  488. return getattr(_prefixes, "value", '/')
  489. def clear_script_prefix():
  490. """
  491. Unsets the script prefix for the current thread.
  492. """
  493. try:
  494. del _prefixes.value
  495. except AttributeError:
  496. pass
  497. def set_urlconf(urlconf_name):
  498. """
  499. Sets the URLconf for the current thread (overriding the default one in
  500. settings). Set to None to revert back to the default.
  501. """
  502. if urlconf_name:
  503. _urlconfs.value = urlconf_name
  504. else:
  505. if hasattr(_urlconfs, "value"):
  506. del _urlconfs.value
  507. def get_urlconf(default=None):
  508. """
  509. Returns the root URLconf to use for the current thread if it has been
  510. changed from the default one.
  511. """
  512. return getattr(_urlconfs, "value", default)
  513. def is_valid_path(path, urlconf=None):
  514. """
  515. Returns True if the given path resolves against the default URL resolver,
  516. False otherwise.
  517. This is a convenience method to make working with "is this a match?" cases
  518. easier, avoiding unnecessarily indented try...except blocks.
  519. """
  520. try:
  521. resolve(path, urlconf)
  522. return True
  523. except Resolver404:
  524. return False