web/lib/django_extensions/management/commands/show_urls.py
changeset 3 526ebd3988b0
equal deleted inserted replaced
1:ebaad720f88b 3:526ebd3988b0
       
     1 from django.conf import settings
       
     2 from django.core.management.base import BaseCommand
       
     3 try:
       
     4     # 2008-05-30 admindocs found in newforms-admin brand
       
     5     from django.contrib.admindocs.views import extract_views_from_urlpatterns, simplify_regex
       
     6 except ImportError:
       
     7     # fall back to trunk, pre-NFA merge
       
     8     from django.contrib.admin.views.doc import extract_views_from_urlpatterns, simplify_regex
       
     9         
       
    10 from django_extensions.management.color import color_style
       
    11 
       
    12 class Command(BaseCommand):
       
    13     help = "Displays all of the url matching routes for the project."
       
    14     
       
    15     requires_model_validation = True
       
    16     
       
    17     def handle(self, *args, **options):
       
    18         if args:
       
    19             appname, = args
       
    20 
       
    21         style = color_style()
       
    22 
       
    23         if settings.ADMIN_FOR:
       
    24             settings_modules = [__import__(m, {}, {}, ['']) for m in settings.ADMIN_FOR]
       
    25         else:
       
    26             settings_modules = [settings]
       
    27 
       
    28         views = []
       
    29         for settings_mod in settings_modules:
       
    30             try:
       
    31                 urlconf = __import__(settings_mod.ROOT_URLCONF, {}, {}, [''])
       
    32             except Exception, e:
       
    33                 if options.get('traceback', None):
       
    34                     import traceback
       
    35                     traceback.print_exc()
       
    36                 print style.ERROR("Error occurred while trying to load %s: %s" % (settings_mod.ROOT_URLCONF, str(e)))
       
    37                 continue
       
    38             view_functions = extract_views_from_urlpatterns(urlconf.urlpatterns)
       
    39             for (func, regex) in view_functions:
       
    40                 func_name = hasattr(func, '__name__') and func.__name__ or repr(func)
       
    41                 views.append("%(url)s\t%(module)s.%(name)s" % {'name': style.MODULE_NAME(func_name),
       
    42                                        'module': style.MODULE(func.__module__),
       
    43                                        'url': style.URL(simplify_regex(regex))})
       
    44         return "\n".join([v for v in views])