2005-07-13 09:25:57 +08:00
|
|
|
"""
|
|
|
|
This module converts requested URLs to callback view functions.
|
|
|
|
|
|
|
|
RegexURLResolver is the main class here. Its resolve() method takes a URL (as
|
|
|
|
a string) and returns a tuple in this format:
|
|
|
|
|
|
|
|
(view_function, dict_of_view_function_args)
|
|
|
|
"""
|
|
|
|
|
2005-11-24 05:44:48 +08:00
|
|
|
from django.core.exceptions import Http404, ImproperlyConfigured, ViewDoesNotExist
|
2005-07-13 09:25:57 +08:00
|
|
|
import re
|
|
|
|
|
2005-08-06 06:22:41 +08:00
|
|
|
class Resolver404(Http404):
|
|
|
|
pass
|
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
def get_mod_func(callback):
|
|
|
|
# Converts 'django.views.news.stories.story_detail' to
|
|
|
|
# ['django.views.news.stories', 'story_detail']
|
|
|
|
dot = callback.rindex('.')
|
|
|
|
return callback[:dot], callback[dot+1:]
|
|
|
|
|
|
|
|
class RegexURLPattern:
|
|
|
|
def __init__(self, regex, callback, default_args=None):
|
2005-08-06 04:50:19 +08:00
|
|
|
# regex is a string representing a regular expression.
|
2005-07-13 09:25:57 +08:00
|
|
|
# callback is something like 'foo.views.news.stories.story_detail',
|
|
|
|
# which represents the path to a module and a view function name.
|
2005-08-06 04:50:19 +08:00
|
|
|
self.regex = re.compile(regex)
|
2005-07-13 09:25:57 +08:00
|
|
|
self.callback = callback
|
|
|
|
self.default_args = default_args or {}
|
|
|
|
|
2005-08-06 04:50:19 +08:00
|
|
|
def resolve(self, path):
|
2005-07-13 09:25:57 +08:00
|
|
|
match = self.regex.search(path)
|
|
|
|
if match:
|
|
|
|
args = dict(match.groupdict(), **self.default_args)
|
|
|
|
try: # Lazily load self.func.
|
|
|
|
return self.func, args
|
|
|
|
except AttributeError:
|
|
|
|
self.func = self.get_callback()
|
|
|
|
return self.func, args
|
|
|
|
|
|
|
|
def get_callback(self):
|
|
|
|
mod_name, func_name = get_mod_func(self.callback)
|
|
|
|
try:
|
|
|
|
return getattr(__import__(mod_name, '', '', ['']), func_name)
|
2005-08-01 22:41:01 +08:00
|
|
|
except ImportError, e:
|
|
|
|
raise ViewDoesNotExist, "Could not import %s. Error was: %s" % (mod_name, str(e))
|
|
|
|
except AttributeError, e:
|
|
|
|
raise ViewDoesNotExist, "Tried %s in module %s. Error was: %s" % (func_name, mod_name, str(e))
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-08-11 06:20:18 +08:00
|
|
|
class RegexURLResolver(object):
|
2005-08-06 04:50:19 +08:00
|
|
|
def __init__(self, regex, urlconf_name):
|
|
|
|
# regex is a string representing a regular expression.
|
|
|
|
# urlconf_name is a string representing the module containing urlconfs.
|
2005-07-13 09:25:57 +08:00
|
|
|
self.regex = re.compile(regex)
|
2005-08-06 04:50:19 +08:00
|
|
|
self.urlconf_name = urlconf_name
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-08-06 04:50:19 +08:00
|
|
|
def resolve(self, path):
|
2005-08-06 06:22:41 +08:00
|
|
|
tried = []
|
2005-07-13 09:25:57 +08:00
|
|
|
match = self.regex.search(path)
|
|
|
|
if match:
|
|
|
|
new_path = path[match.end():]
|
2005-08-11 06:20:18 +08:00
|
|
|
for pattern in self.urlconf_module.urlpatterns:
|
2005-08-06 06:22:41 +08:00
|
|
|
try:
|
|
|
|
sub_match = pattern.resolve(new_path)
|
|
|
|
except Resolver404, e:
|
|
|
|
tried.extend([(pattern.regex.pattern + ' ' + t) for t in e.args[0]['tried']])
|
|
|
|
else:
|
|
|
|
if sub_match:
|
2005-09-27 12:24:19 +08:00
|
|
|
return sub_match[0], dict(match.groupdict(), **sub_match[1])
|
2005-08-06 06:22:41 +08:00
|
|
|
tried.append(pattern.regex.pattern)
|
|
|
|
raise Resolver404, {'tried': tried, 'path': new_path}
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-08-06 04:50:19 +08:00
|
|
|
def _get_urlconf_module(self):
|
2005-08-11 06:20:18 +08:00
|
|
|
try:
|
|
|
|
return self._urlconf_module
|
|
|
|
except AttributeError:
|
2005-11-24 05:44:48 +08:00
|
|
|
try:
|
|
|
|
self._urlconf_module = __import__(self.urlconf_name, '', '', [''])
|
|
|
|
except ValueError, e:
|
|
|
|
# Invalid urlconf_name, such as "foo.bar." (note trailing period)
|
|
|
|
raise ImproperlyConfigured, "Error while importing URLconf %r: %s" % (self.urlconf_name, e)
|
2005-08-11 06:20:18 +08:00
|
|
|
return self._urlconf_module
|
2005-08-06 04:50:19 +08:00
|
|
|
urlconf_module = property(_get_urlconf_module)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-09-29 21:29:12 +08:00
|
|
|
def _get_url_patterns(self):
|
2005-09-29 22:59:49 +08:00
|
|
|
return self.urlconf_module.urlpatterns
|
2005-09-29 21:29:12 +08:00
|
|
|
url_patterns = property(_get_url_patterns)
|
|
|
|
|
2005-08-06 04:50:19 +08:00
|
|
|
def _resolve_special(self, view_type):
|
|
|
|
callback = getattr(self.urlconf_module, 'handler%s' % view_type)
|
|
|
|
mod_name, func_name = get_mod_func(callback)
|
2005-07-13 09:25:57 +08:00
|
|
|
try:
|
|
|
|
return getattr(__import__(mod_name, '', '', ['']), func_name), {}
|
2005-07-19 12:30:23 +08:00
|
|
|
except (ImportError, AttributeError), e:
|
2005-08-06 04:50:19 +08:00
|
|
|
raise ViewDoesNotExist, "Tried %s. Error was: %s" % (callback, str(e))
|
|
|
|
|
|
|
|
def resolve404(self):
|
|
|
|
return self._resolve_special('404')
|
|
|
|
|
|
|
|
def resolve500(self):
|
|
|
|
return self._resolve_special('500')
|