2015-09-17 06:07:39 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2016-04-02 18:49:12 +08:00
|
|
|
import six
|
|
|
|
|
2016-02-02 01:24:33 +08:00
|
|
|
from django.conf import settings
|
|
|
|
|
2016-04-02 18:49:12 +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
|
|
|
"""
|
2015-12-30 23:51:16 +08:00
|
|
|
from django.urls import RegexURLPattern, RegexURLResolver
|
2015-09-17 06:07:39 +08:00
|
|
|
warnings = []
|
|
|
|
for pattern in resolver.url_patterns:
|
|
|
|
if isinstance(pattern, RegexURLResolver):
|
|
|
|
warnings.extend(check_include_trailing_dollar(pattern))
|
|
|
|
# Check resolver recursively
|
|
|
|
warnings.extend(check_resolver(pattern))
|
|
|
|
elif isinstance(pattern, RegexURLPattern):
|
|
|
|
warnings.extend(check_pattern_name(pattern))
|
2016-04-02 18:49:12 +08:00
|
|
|
else:
|
|
|
|
# This is not a url() instance
|
|
|
|
warnings.extend(get_warning_for_invalid_pattern(pattern))
|
2015-09-17 06:07:39 +08:00
|
|
|
|
2016-04-02 18:49:12 +08:00
|
|
|
if not warnings:
|
|
|
|
warnings.extend(check_pattern_startswith_slash(pattern))
|
2015-09-17 06:07:39 +08:00
|
|
|
|
|
|
|
return warnings
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
if isinstance(pattern, six.string_types):
|
|
|
|
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):
|
|
|
|
hint = "Try using url() instead of a tuple."
|
|
|
|
else:
|
|
|
|
hint = None
|
|
|
|
|
|
|
|
return [Error(
|
|
|
|
"Your URL pattern {!r} is invalid. Ensure that urlpatterns is a list "
|
|
|
|
"of url() instances.".format(pattern),
|
|
|
|
hint=hint,
|
|
|
|
id="urls.E004",
|
|
|
|
)]
|
|
|
|
|
|
|
|
|
2015-09-17 06:07:39 +08:00
|
|
|
def describe_pattern(pattern):
|
|
|
|
"""
|
2015-09-22 01:45:56 +08:00
|
|
|
Format the URL pattern for display in warning messages.
|
2015-09-17 06:07:39 +08:00
|
|
|
"""
|
|
|
|
description = "'{}'".format(pattern.regex.pattern)
|
|
|
|
if getattr(pattern, 'name', False):
|
|
|
|
description += " [name='{}']".format(pattern.name)
|
|
|
|
return description
|
|
|
|
|
|
|
|
|
|
|
|
def check_include_trailing_dollar(pattern):
|
|
|
|
"""
|
2015-09-22 01:45:56 +08:00
|
|
|
Check that include is not used with a regex ending with a dollar.
|
2015-09-17 06:07:39 +08:00
|
|
|
"""
|
|
|
|
regex_pattern = pattern.regex.pattern
|
|
|
|
if regex_pattern.endswith('$') and not regex_pattern.endswith('\$'):
|
|
|
|
warning = Warning(
|
|
|
|
"Your URL pattern {} uses include with a regex ending with a '$'. "
|
|
|
|
"Remove the dollar from the regex to avoid problems including "
|
|
|
|
"URLs.".format(describe_pattern(pattern)),
|
|
|
|
id="urls.W001",
|
|
|
|
)
|
|
|
|
return [warning]
|
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
|
|
|
def check_pattern_startswith_slash(pattern):
|
|
|
|
"""
|
2015-09-22 01:45:56 +08:00
|
|
|
Check that the pattern does not begin with a forward slash.
|
2015-09-17 06:07:39 +08:00
|
|
|
"""
|
|
|
|
regex_pattern = pattern.regex.pattern
|
|
|
|
if regex_pattern.startswith('/') or regex_pattern.startswith('^/'):
|
|
|
|
warning = Warning(
|
|
|
|
"Your URL pattern {} has a regex beginning with a '/'. "
|
|
|
|
"Remove this slash as it is unnecessary.".format(describe_pattern(pattern)),
|
|
|
|
id="urls.W002",
|
|
|
|
)
|
|
|
|
return [warning]
|
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
|
|
|
def check_pattern_name(pattern):
|
|
|
|
"""
|
2015-09-22 01:45:56 +08:00
|
|
|
Check that the pattern name does not contain a colon.
|
2015-09-17 06:07:39 +08:00
|
|
|
"""
|
|
|
|
if pattern.name is not None and ":" in pattern.name:
|
|
|
|
warning = Warning(
|
|
|
|
"Your URL pattern {} has a name including a ':'. Remove the colon, to "
|
|
|
|
"avoid ambiguous namespace references.".format(describe_pattern(pattern)),
|
|
|
|
id="urls.W003",
|
|
|
|
)
|
|
|
|
return [warning]
|
|
|
|
else:
|
|
|
|
return []
|