diff options
author | Justin Bronn <jbronn@gmail.com> | 2008-08-05 17:15:33 +0000 |
---|---|---|
committer | Justin Bronn <jbronn@gmail.com> | 2008-08-05 17:15:33 +0000 |
commit | aa239e3e5405933af6a29dac3cf587b59a099927 (patch) | |
tree | ea2cbd139c9a8cf84c09e0b2008bff70e05927ef /django/core/urlresolvers.py | |
parent | 45b73c9a4685809236f84046cc7ffd32a50db958 (diff) | |
download | django-attic/gis.tar.gz |
gis: Merged revisions 7981-8001,8003-8011,8013-8033,8035-8036,8038-8039,8041-8063,8065-8076,8078-8139,8141-8154,8156-8214 via svnmerge from trunk.archive/attic/gisattic/gis
git-svn-id: http://code.djangoproject.com/svn/django/branches/gis@8215 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/core/urlresolvers.py')
-rw-r--r-- | django/core/urlresolvers.py | 33 |
1 files changed, 30 insertions, 3 deletions
diff --git a/django/core/urlresolvers.py b/django/core/urlresolvers.py index ff0bcbcfea..4ec725cb38 100644 --- a/django/core/urlresolvers.py +++ b/django/core/urlresolvers.py @@ -7,11 +7,13 @@ a string) and returns a tuple in this format: (view_function, function_args, function_kwargs) """ +import re + from django.http import Http404 from django.core.exceptions import ImproperlyConfigured, ViewDoesNotExist from django.utils.encoding import iri_to_uri, force_unicode, smart_str from django.utils.functional import memoize -import re +from django.utils.thread_support import currentThread try: reversed @@ -21,6 +23,11 @@ except NameError: _resolver_cache = {} # Maps urlconf modules to RegexURLResolver instances. _callable_cache = {} # Maps view and url pattern names to their view functions. +# SCRIPT_NAME prefixes for each thread are stored here. If there's no entry for +# the current thread (which is the only one we ever access), it is assumed to +# be empty. +_prefixes = {} + class Resolver404(Http404): pass @@ -291,13 +298,33 @@ class RegexURLResolver(object): def resolve(path, urlconf=None): return get_resolver(urlconf).resolve(path) -def reverse(viewname, urlconf=None, args=None, kwargs=None): +def reverse(viewname, urlconf=None, args=None, kwargs=None, prefix=None): args = args or [] kwargs = kwargs or {} - return iri_to_uri(u'/' + get_resolver(urlconf).reverse(viewname, *args, **kwargs)) + if prefix is None: + prefix = get_script_prefix() + return iri_to_uri(u'%s%s' % (prefix, get_resolver(urlconf).reverse(viewname, + *args, **kwargs))) def clear_url_caches(): global _resolver_cache global _callable_cache _resolver_cache.clear() _callable_cache.clear() + +def set_script_prefix(prefix): + """ + Sets the script prefix for the current thread. + """ + if not prefix.endswith('/'): + prefix += '/' + _prefixes[currentThread()] = prefix + +def get_script_prefix(): + """ + Returns the currently active script prefix. Useful for client code that + wishes to construct their own URLs manually (although accessing the request + instance is normally going to be a lot cleaner). + """ + return _prefixes.get(currentThread(), u'/') + |