from django.conf import settings
from urls import urlpatterns

'''
Access shell via
./manage.py shell 
(or shell_plus if you have django-extensions)
Dont forget you may need to set environment variables:
 - DJANGO_SETTINGS_MODULE to the settings file (python module load syntax like settings.filename) and 
 - PYTHONPATH to include the path where the Django code sits
 
Install ipython and django-extensions to get a better shell (shell_plus)
pip install django-extensions

This also has show_urls command which will do something similar to get_urls_friendly below

urls will not contain urlpatterns in later django releases
'''


# all the configured apps settings are now in here
settings

# this prints out mapped urls and associated views
def get_urls_friendly(raw_urls, nice_urls=[], urlbase=''):
    '''Recursively builds a list of all the urls in the current project and the name of their associated view'''
    for entry in raw_urls:
        fullurl = (urlbase + entry.regex.pattern).replace('^','')
        if entry.callback: 
            viewname = entry.callback.func_name
            nice_urls.append('%s  -  %s' %(fullurl, viewname))
        else: 
            get_urls_friendly(entry.url_patterns, nice_urls, fullurl)
    nice_urls = sorted(list(set(nice_urls))) 
    return nice_urls

Create django_audit_snippets.py