neithere / django-navigation

Django Navigation is a breadcrumbs navigation application for Django Web Framework.

Changed (Δ534 bytes):

raw changeset »

navigation/__init__.py (1 lines added, 1 lines removed)

navigation/templatetags/navigation_tags.py (42 lines added, 8 lines removed)

Up to file-list navigation/__init__.py:

12
12
__author__  = 'Andy Mikhailenko'
13
13
__license__ = 'GNU Lesser General Public License (GPL), Version 3'
14
14
__url__     = 'http://bitbucket.org/neithere/django-navigation/'
15
__version__ = '0.2.1'
15
__version__ = '0.3.0'

Up to file-list navigation/templatetags/navigation_tags.py:

@@ -12,6 +12,9 @@ Unified templated breadcrumbs for both F
12
12
Andy Mikhailenko <http://neithere.net>, <neithere@gmail.com>
13
13
"""
14
14
15
# TODO: tags instead of filters
16
17
15
18
# Python
16
19
from urlparse import urljoin
17
20
@@ -25,6 +28,7 @@ from django.utils.http import urlquote
25
28
from django.utils.translation import ugettext as _
26
29
from django.template.loader import render_to_string
27
30
31
28
32
# A list of site sections' URLs in desired order, i.e.:
29
33
#   NAVIGATION_SECTIONS = ('/about/', '/news/')
30
34
SECTIONS = getattr(settings, 'NAVIGATION_SECTIONS', ())
@@ -36,20 +40,29 @@ if 'django.contrib.flatpages.middleware.
36
40
else:
37
41
    ENABLE_FLATPAGES = False
38
42
43
39
44
register = template.Library()
40
45
41
class Crumb:
42
    """ A 'crumb' is just a navigation item """
46
47
class Crumb(object):
48
    """
49
    A navigation node.
50
    """
51
43
52
    def __init__(self, url, title, is_current=False, is_dummy=False):
44
53
        self.url        = url
45
54
        self.title      = title
46
55
        self.is_current = is_current
47
56
        self.is_dummy   = is_dummy
48
    __unicode__ = lambda self: self.url
49
57
50
@register.filter(name='get_navigation')
51
def get_navigation(request):
52
    # Prepare sections (horizontal cut, base level)
58
    def __unicode__(self):
59
        return unicode(self.title)
60
61
62
def _get_sections(request):
63
    """
64
    Returns list of sections (horizontal cut, base level).
65
    """
53
66
54
67
    sections = []
55
68
    for section_url in SECTIONS:
@@ -63,12 +76,18 @@ def get_navigation(request):
63
76
                    crumb.is_current = True
64
77
            sections.append(crumb)
65
78
66
    # Prepare trail (vertical cut, excluding base level)
79
    return sections
67
80
81
def _get_trail(request, exclude_section=False):
82
    """
83
    Returns trail of breadcrumbs (vertical cut, excluding base level)
84
    """
68
85
    trail = []
69
86
    url = request.path
70
87
    while url:
71
        if url == '/'  or  url in SECTIONS:
88
        if url == '/':
89
            break
90
        if exclude_section and url in SECTIONS:
72
91
            break
73
92
        crumb = find_crumb(url, request)
74
93
        if not crumb:
@@ -80,6 +99,21 @@ def get_navigation(request):
80
99
81
100
    trail.reverse()
82
101
102
    return trail
103
104
@register.filter
105
def get_sections(request):
106
    return _get_sections(request)
107
108
@register.filter
109
def get_trail(request, exclude_section=False):
110
    return _get_trail(request, exclude_section)
111
112
@register.filter
113
def get_navigation(request):
114
    sections = _get_sections(request)
115
    trail = _get_trail(request, exclude_section=True)
116
83
117
    return mark_safe(render_to_string('navigation.html', dict(sections=sections,trail=trail)))
84
118
85
119
    """