mirror of
https://github.com/django/django.git
synced 2025-06-05 03:29:12 +00:00
Make get_url_patterns into a class method
This commit is contained in:
parent
761c846583
commit
d47f6892bc
@ -70,7 +70,7 @@ class Command(BaseCommand):
|
|||||||
|
|
||||||
def handle(self, *args, **options):
|
def handle(self, *args, **options):
|
||||||
prefixes = options["prefixes"]
|
prefixes = options["prefixes"]
|
||||||
url_patterns = get_url_patterns(prefixes=prefixes)
|
url_patterns = self.get_url_patterns(prefixes=prefixes)
|
||||||
|
|
||||||
if not url_patterns:
|
if not url_patterns:
|
||||||
raise CommandError("There are no URL patterns that match given prefixes")
|
raise CommandError("There are no URL patterns that match given prefixes")
|
||||||
@ -101,6 +101,53 @@ class Command(BaseCommand):
|
|||||||
|
|
||||||
return url_patterns
|
return url_patterns
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def get_url_patterns(cls, prefixes=None):
|
||||||
|
"""
|
||||||
|
Returns a list of URL patterns in the project with given prefixes.
|
||||||
|
|
||||||
|
Each object in the returned list is a tuple[str] with 3 elements:
|
||||||
|
(route, view, name)
|
||||||
|
"""
|
||||||
|
|
||||||
|
url_patterns = []
|
||||||
|
urlconf = import_module(settings.ROOT_URLCONF)
|
||||||
|
|
||||||
|
for view_func, regex, namespace_list, name in extract_views_from_urlpatterns(
|
||||||
|
urlconf.urlpatterns
|
||||||
|
):
|
||||||
|
# Route
|
||||||
|
route = simplify_regex(regex)
|
||||||
|
|
||||||
|
# View
|
||||||
|
view = "{}.{}".format(
|
||||||
|
view_func.__module__,
|
||||||
|
getattr(view_func, "__name__", view_func.__class__.__name__),
|
||||||
|
)
|
||||||
|
|
||||||
|
# Name
|
||||||
|
namespace = ""
|
||||||
|
|
||||||
|
if namespace_list:
|
||||||
|
for part in namespace_list:
|
||||||
|
namespace += part + ":"
|
||||||
|
|
||||||
|
name = namespace + name if name else None
|
||||||
|
name = name or ""
|
||||||
|
|
||||||
|
# Append to the list
|
||||||
|
url_patterns.append((route, view, name))
|
||||||
|
|
||||||
|
# Filter out when prefixes are given but the pattern's route doesn't match
|
||||||
|
if prefixes:
|
||||||
|
url_patterns = [
|
||||||
|
url_pattern
|
||||||
|
for url_pattern in url_patterns
|
||||||
|
if any(url_pattern[0].startswith(prefix) for prefix in prefixes)
|
||||||
|
]
|
||||||
|
|
||||||
|
return url_patterns
|
||||||
|
|
||||||
def apply_color(self, url_patterns):
|
def apply_color(self, url_patterns):
|
||||||
colored_url_patterns = []
|
colored_url_patterns = []
|
||||||
|
|
||||||
@ -228,50 +275,3 @@ class Command(BaseCommand):
|
|||||||
|
|
||||||
def format_pretty_json(self, url_patterns):
|
def format_pretty_json(self, url_patterns):
|
||||||
return self.format_json(url_patterns, pretty=True)
|
return self.format_json(url_patterns, pretty=True)
|
||||||
|
|
||||||
|
|
||||||
def get_url_patterns(prefixes=None):
|
|
||||||
"""
|
|
||||||
Returns a list of URL patterns in the project with given prefixes.
|
|
||||||
|
|
||||||
Each object in the returned list is a tuple[str] with 3 elements:
|
|
||||||
(route, view, name)
|
|
||||||
"""
|
|
||||||
|
|
||||||
url_patterns = []
|
|
||||||
urlconf = import_module(settings.ROOT_URLCONF)
|
|
||||||
|
|
||||||
for view_func, regex, namespace_list, name in extract_views_from_urlpatterns(
|
|
||||||
urlconf.urlpatterns
|
|
||||||
):
|
|
||||||
# Route
|
|
||||||
route = simplify_regex(regex)
|
|
||||||
|
|
||||||
# View
|
|
||||||
view = "{}.{}".format(
|
|
||||||
view_func.__module__,
|
|
||||||
getattr(view_func, "__name__", view_func.__class__.__name__),
|
|
||||||
)
|
|
||||||
|
|
||||||
# Name
|
|
||||||
namespace = ""
|
|
||||||
|
|
||||||
if namespace_list:
|
|
||||||
for part in namespace_list:
|
|
||||||
namespace += part + ":"
|
|
||||||
|
|
||||||
name = namespace + name if name else None
|
|
||||||
name = name or ""
|
|
||||||
|
|
||||||
# Append to the list
|
|
||||||
url_patterns.append((route, view, name))
|
|
||||||
|
|
||||||
# Filter out when prefixes are given but the pattern's route doesn't match
|
|
||||||
if prefixes:
|
|
||||||
url_patterns = [
|
|
||||||
url_pattern
|
|
||||||
for url_pattern in url_patterns
|
|
||||||
if any(url_pattern[0].startswith(prefix) for prefix in prefixes)
|
|
||||||
]
|
|
||||||
|
|
||||||
return url_patterns
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user