Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# Imports
3from django.urls import reverse
5# Exports
7__all__ = (
8 "Breadcrumb",
9 "Breadcrumbs",
10)
12# Classes
15class Breadcrumb(object):
16 """Helper class that ensures an object has the required attributes."""
18 def __init__(self, text, url):
19 """Initialize the crumb.
21 :param text: The text of the link.
22 :type text: str
24 :param url: The URL of the link.
25 :type url: str
27 """
28 self.text = text
29 self.url = url
31 def __repr__(self):
32 return "<%s %s>" % (self.__class__.__name__, self.text)
34 def get_absolute_url(self):
35 """Allow the crumb to simulate a model with ``get_absolute_url()``.
37 :rtype: str
39 """
40 return self.url
43class Breadcrumbs(object):
44 """Collect objects to be displayed as breadcrumbs."""
46 def __init__(self):
47 """Initialize the ``items`` attribute for a new collection of crumbs."""
48 self.items = list()
50 def __iter__(self):
51 return iter(self.items)
53 def add(self, text, url, pattern_args=None, pattern_kwargs=None, namespace=None):
54 """Add a breadcrumb to the list.
56 :param text: The breadcrumb label/text.
57 :type text: str
59 :param url: The URL or pattern name.
60 :type url: str
62 :param pattern_args: Pattern arguments when the URL is given as a pattern name.
63 :type pattern_args: list
65 :param pattern_kwargs: Pattern keyword arguments when the URL is given as a pattern name.
66 :type pattern_kwargs: dict
68 :param namespace: The application namespace for the URL when given as a pattern name.
69 :type namespace: str
71 :rtype: Breadcrumb
73 """
74 if pattern_args or pattern_kwargs:
75 if namespace is not None:
76 _url = "%s:%s" % (namespace, url)
77 else:
78 _url = url
80 resolved_url = reverse(_url, args=pattern_args, kwargs=pattern_kwargs)
81 else:
82 resolved_url = url
84 breadcrumb = Breadcrumb(text, resolved_url)
85 self.items.append(breadcrumb)
87 return breadcrumb
89 def insert(self, index, text, url, pattern_args=None, pattern_kwargs=None, namespace=None):
90 """Insert a breadcrumb in the list.
92 :param index: The index of the new breadcrumb..
93 :type index: int
95 :param text: The breadcrumb label/text.
96 :type text: str
98 :param url: The URL or pattern name.
99 :type url: str
101 :param pattern_args: Pattern arguments when the URL is given as a pattern name.
102 :type pattern_args: list
104 :param pattern_kwargs: Pattern keyword arguments when the URL is given as a pattern name.
105 :type pattern_kwargs: dict
107 :param namespace: The application namespace for the URL when given as a pattern name.
108 :type namespace: str
110 :rtype: Breadcrumb
112 """
113 if pattern_args or pattern_kwargs:
114 if namespace is not None:
115 _url = "%s:%s" % (namespace, url)
116 else:
117 _url = url
119 resolved_url = reverse(_url, args=pattern_args, kwargs=pattern_kwargs)
120 else:
121 resolved_url = url
123 breadcrumb = Breadcrumb(text, resolved_url)
124 self.items.insert(index, breadcrumb)
126 return breadcrumb