root / snf-webproject / synnefo / webproject / management / commands / show_urls.py @ 2ce0636e
History | View | Annotate | Download (2.3 kB)
1 |
# code shared from django-command-extensions
|
---|---|
2 |
# http://code.google.com/p/django-command-extensions/
|
3 |
|
4 |
from django.conf import settings |
5 |
from django.core.management.base import BaseCommand |
6 |
from django.core.management import color |
7 |
from django.utils import termcolors |
8 |
|
9 |
|
10 |
def color_style(): |
11 |
style = color.color_style() |
12 |
if color.supports_color():
|
13 |
style.URL = termcolors.make_style(fg='green', opts=('bold',)) |
14 |
style.MODULE = termcolors.make_style(fg='yellow')
|
15 |
style.MODULE_NAME = termcolors.make_style(opts=('bold',))
|
16 |
style.URL_NAME = termcolors.make_style(fg='red')
|
17 |
return style
|
18 |
|
19 |
try:
|
20 |
# 2008-05-30 admindocs found in newforms-admin brand
|
21 |
from django.contrib.admindocs.views import extract_views_from_urlpatterns, simplify_regex |
22 |
except ImportError: |
23 |
# fall back to trunk, pre-NFA merge
|
24 |
from django.contrib.admin.views.doc import extract_views_from_urlpatterns, simplify_regex |
25 |
|
26 |
class Command(BaseCommand): |
27 |
help = "Displays all of the url matching routes for the project."
|
28 |
|
29 |
requires_model_validation = True
|
30 |
|
31 |
def handle(self, *args, **options): |
32 |
if args:
|
33 |
appname, = args |
34 |
|
35 |
style = color_style() |
36 |
|
37 |
if settings.ADMIN_FOR:
|
38 |
settings_modules = [__import__(m, {}, {}, ['']) for m in settings.ADMIN_FOR] |
39 |
else:
|
40 |
settings_modules = [settings] |
41 |
|
42 |
views = [] |
43 |
for settings_mod in settings_modules: |
44 |
try:
|
45 |
urlconf = __import__(settings_mod.ROOT_URLCONF, {}, {}, ['']) |
46 |
except Exception, e: |
47 |
if options.get('traceback', None): |
48 |
import traceback |
49 |
traceback.print_exc() |
50 |
print style.ERROR("Error occurred while trying to load %s: %s" % (settings_mod.ROOT_URLCONF, str(e))) |
51 |
continue
|
52 |
view_functions = extract_views_from_urlpatterns(urlconf.urlpatterns) |
53 |
for (func, regex) in view_functions: |
54 |
func_name = hasattr(func, '__name__') and func.__name__ or repr(func) |
55 |
views.append("%(url)s\t%(module)s.%(name)s" % {'name': style.MODULE_NAME(func_name), |
56 |
'module': style.MODULE(func.__module__),
|
57 |
'url': style.URL(simplify_regex(regex))})
|
58 |
|
59 |
return "\n".join([v for v in views]) |