Statistics
| Branch: | Tag: | Revision:

root / snf-webproject / synnefo / webproject / management / commands / show_urls.py @ d63a86d6

History | View | Annotate | Download (2.3 kB)

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