diff options
| author | tom christie tom@tomchristie.com | 2011-02-19 10:26:27 +0000 | 
|---|---|---|
| committer | tom christie tom@tomchristie.com | 2011-02-19 10:26:27 +0000 | 
| commit | 805aa03ec1871f6a766d9052b348ddce9e9843c3 (patch) | |
| tree | 8ab5b6a7396236aa45bbc61e8404cc77fc75a9c5 /djangorestframework/breadcrumbs.py | |
| parent | b749b950a1b4bede76b7e3900a6385779904902d (diff) | |
| download | django-rest-framework-805aa03ec1871f6a766d9052b348ddce9e9843c3.tar.bz2 | |
Yowzers.  Final big bunch of refactoring for 0.1 release.  Now support Django 1.3's views, admin style api is all polished off, loads of tests, new test project for running the test.  All sorts of goodness.  Getting ready to push this out now.
Diffstat (limited to 'djangorestframework/breadcrumbs.py')
| -rw-r--r-- | djangorestframework/breadcrumbs.py | 31 | 
1 files changed, 31 insertions, 0 deletions
diff --git a/djangorestframework/breadcrumbs.py b/djangorestframework/breadcrumbs.py new file mode 100644 index 00000000..ba779dd0 --- /dev/null +++ b/djangorestframework/breadcrumbs.py @@ -0,0 +1,31 @@ +from django.core.urlresolvers import resolve +from djangorestframework.description import get_name + +def get_breadcrumbs(url): +    """Given a url returns a list of breadcrumbs, which are each a tuple of (name, url).""" +     +    def breadcrumbs_recursive(url, breadcrumbs_list): +        """Add tuples of (name, url) to the breadcrumbs list, progressively chomping off parts of the url.""" +         +        # This is just like compsci 101 all over again... +        try: +            (view, unused_args, unused_kwargs) = resolve(url) +        except: +            pass +        else: +            if callable(view): +                breadcrumbs_list.insert(0, (get_name(view), url)) +         +        if url == '': +            # All done +            return breadcrumbs_list +     +        elif url.endswith('/'): +            # Drop trailing slash off the end and continue to try to resolve more breadcrumbs +            return breadcrumbs_recursive(url.rstrip('/'), breadcrumbs_list) +     +        # Drop trailing non-slash off the end and continue to try to resolve more breadcrumbs +        return breadcrumbs_recursive(url[:url.rfind('/') + 1], breadcrumbs_list) + +    return breadcrumbs_recursive(url, []) +  | 
