2016-12-22 05:54:15 +08:00
|
|
|
from collections import Counter
|
|
|
|
|
2016-02-02 01:24:33 +08:00
|
|
|
from django.conf import settings
|
|
|
|
|
2016-12-22 05:54:15 +08:00
|
|
|
from . import Error, Tags, Warning, register
|
2015-09-17 06:07:39 +08:00
|
|
|
|
|
|
|
|
|
|
|
@register(Tags.urls)
|
|
|
|
def check_url_config(app_configs, **kwargs):
|
2016-02-02 01:24:33 +08:00
|
|
|
if getattr(settings, 'ROOT_URLCONF', None):
|
|
|
|
from django.urls import get_resolver
|
|
|
|
resolver = get_resolver()
|
|
|
|
return check_resolver(resolver)
|
|
|
|
return []
|
2015-09-17 06:07:39 +08:00
|
|
|
|
|
|
|
|
|
|
|
def check_resolver(resolver):
|
|
|
|
"""
|
2015-09-22 01:45:56 +08:00
|
|
|
Recursively check the resolver.
|
2015-09-17 06:07:39 +08:00
|
|
|
"""
|
2016-10-06 03:34:26 +08:00
|
|
|
check_method = getattr(resolver, 'check', None)
|
|
|
|
if check_method is not None:
|
|
|
|
return check_method()
|
|
|
|
elif not hasattr(resolver, 'resolve'):
|
|
|
|
return get_warning_for_invalid_pattern(resolver)
|
|
|
|
else:
|
|
|
|
return []
|
2015-09-17 06:07:39 +08:00
|
|
|
|
|
|
|
|
2016-12-22 05:54:15 +08:00
|
|
|
@register(Tags.urls)
|
|
|
|
def check_url_namespaces_unique(app_configs, **kwargs):
|
|
|
|
"""
|
|
|
|
Warn if URL namespaces used in applications aren't unique.
|
|
|
|
"""
|
|
|
|
if not getattr(settings, 'ROOT_URLCONF', None):
|
|
|
|
return []
|
|
|
|
|
|
|
|
from django.urls import get_resolver
|
|
|
|
resolver = get_resolver()
|
|
|
|
all_namespaces = _load_all_namespaces(resolver)
|
|
|
|
counter = Counter(all_namespaces)
|
|
|
|
non_unique_namespaces = [n for n, count in counter.items() if count > 1]
|
|
|
|
errors = []
|
|
|
|
for namespace in non_unique_namespaces:
|
|
|
|
errors.append(Warning(
|
|
|
|
"URL namespace '{}' isn't unique. You may not be able to reverse "
|
|
|
|
"all URLs in this namespace".format(namespace),
|
|
|
|
id="urls.W005",
|
|
|
|
))
|
|
|
|
return errors
|
|
|
|
|
|
|
|
|
2017-02-24 23:13:48 +08:00
|
|
|
def _load_all_namespaces(resolver, parents=()):
|
2016-12-22 05:54:15 +08:00
|
|
|
"""
|
|
|
|
Recursively load all namespaces from URL patterns.
|
|
|
|
"""
|
|
|
|
url_patterns = getattr(resolver, 'url_patterns', [])
|
|
|
|
namespaces = [
|
2017-02-24 23:13:48 +08:00
|
|
|
':'.join(parents + (url.namespace,)) for url in url_patterns
|
2016-12-22 05:54:15 +08:00
|
|
|
if getattr(url, 'namespace', None) is not None
|
|
|
|
]
|
|
|
|
for pattern in url_patterns:
|
2017-03-03 23:52:20 +08:00
|
|
|
namespace = getattr(pattern, 'namespace', None)
|
|
|
|
current = parents
|
|
|
|
if namespace is not None:
|
|
|
|
current += (namespace,)
|
2017-02-24 23:13:48 +08:00
|
|
|
namespaces.extend(_load_all_namespaces(pattern, current))
|
2016-12-22 05:54:15 +08:00
|
|
|
return namespaces
|
|
|
|
|
|
|
|
|
2016-04-02 18:49:12 +08:00
|
|
|
def get_warning_for_invalid_pattern(pattern):
|
|
|
|
"""
|
|
|
|
Return a list containing a warning that the pattern is invalid.
|
|
|
|
|
|
|
|
describe_pattern() cannot be used here, because we cannot rely on the
|
|
|
|
urlpattern having regex or name attributes.
|
|
|
|
"""
|
2016-12-29 23:27:49 +08:00
|
|
|
if isinstance(pattern, str):
|
2016-04-02 18:49:12 +08:00
|
|
|
hint = (
|
|
|
|
"Try removing the string '{}'. The list of urlpatterns should not "
|
|
|
|
"have a prefix string as the first element.".format(pattern)
|
|
|
|
)
|
|
|
|
elif isinstance(pattern, tuple):
|
2016-10-21 01:29:04 +08:00
|
|
|
hint = "Try using path() instead of a tuple."
|
2016-04-02 18:49:12 +08:00
|
|
|
else:
|
|
|
|
hint = None
|
|
|
|
|
|
|
|
return [Error(
|
|
|
|
"Your URL pattern {!r} is invalid. Ensure that urlpatterns is a list "
|
2016-10-21 01:29:04 +08:00
|
|
|
"of path() and/or re_path() instances.".format(pattern),
|
2016-04-02 18:49:12 +08:00
|
|
|
hint=hint,
|
|
|
|
id="urls.E004",
|
|
|
|
)]
|
2016-12-23 23:55:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
@register(Tags.urls)
|
|
|
|
def check_url_settings(app_configs, **kwargs):
|
|
|
|
errors = []
|
|
|
|
for name in ('STATIC_URL', 'MEDIA_URL'):
|
|
|
|
value = getattr(settings, name)
|
|
|
|
if value and not value.endswith('/'):
|
|
|
|
errors.append(E006(name))
|
|
|
|
return errors
|
|
|
|
|
|
|
|
|
|
|
|
def E006(name):
|
|
|
|
return Error(
|
|
|
|
'The {} setting must end with a slash.'.format(name),
|
|
|
|
id='urls.E006',
|
|
|
|
)
|