Statistics
| Branch: | Revision:

root / env / lib / python2.7 / site-packages / django / conf / urls / __init__.py @ 1a305335

History | View | Annotate | Download (2.44 KB)

1
from django.core.urlresolvers import (RegexURLPattern,
2
    RegexURLResolver, LocaleRegexURLResolver)
3
from django.core.exceptions import ImproperlyConfigured
4
from django.utils.importlib import import_module
5

    
6

    
7
__all__ = ['handler403', 'handler404', 'handler500', 'include', 'patterns', 'url']
8

    
9
handler403 = 'django.views.defaults.permission_denied'
10
handler404 = 'django.views.defaults.page_not_found'
11
handler500 = 'django.views.defaults.server_error'
12

    
13
def include(arg, namespace=None, app_name=None):
14
    if isinstance(arg, tuple):
15
        # callable returning a namespace hint
16
        if namespace:
17
            raise ImproperlyConfigured('Cannot override the namespace for a dynamic module that provides a namespace')
18
        urlconf_module, app_name, namespace = arg
19
    else:
20
        # No namespace hint - use manually provided namespace
21
        urlconf_module = arg
22

    
23
    if isinstance(urlconf_module, basestring):
24
        urlconf_module = import_module(urlconf_module)
25
    patterns = getattr(urlconf_module, 'urlpatterns', urlconf_module)
26

    
27
    # Make sure we can iterate through the patterns (without this, some
28
    # testcases will break).
29
    if isinstance(patterns, (list, tuple)):
30
        for url_pattern in patterns:
31
            # Test if the LocaleRegexURLResolver is used within the include;
32
            # this should throw an error since this is not allowed!
33
            if isinstance(url_pattern, LocaleRegexURLResolver):
34
                raise ImproperlyConfigured(
35
                    'Using i18n_patterns in an included URLconf is not allowed.')
36

    
37
    return (urlconf_module, app_name, namespace)
38

    
39
def patterns(prefix, *args):
40
    pattern_list = []
41
    for t in args:
42
        if isinstance(t, (list, tuple)):
43
            t = url(prefix=prefix, *t)
44
        elif isinstance(t, RegexURLPattern):
45
            t.add_prefix(prefix)
46
        pattern_list.append(t)
47
    return pattern_list
48

    
49
def url(regex, view, kwargs=None, name=None, prefix=''):
50
    if isinstance(view, (list,tuple)):
51
        # For include(...) processing.
52
        urlconf_module, app_name, namespace = view
53
        return RegexURLResolver(regex, urlconf_module, kwargs, app_name=app_name, namespace=namespace)
54
    else:
55
        if isinstance(view, basestring):
56
            if not view:
57
                raise ImproperlyConfigured('Empty URL pattern view name not permitted (for pattern %r)' % regex)
58
            if prefix:
59
                view = prefix + '.' + view
60
        return RegexURLPattern(regex, view, kwargs, name)
61