2018-03-07 21:49:02 -05:00
|
|
|
from django.urls import reverse
|
|
|
|
|
2017-10-24 09:25:41 -04:00
|
|
|
breadcrumbs = {}
|
|
|
|
|
|
|
|
|
2018-03-07 21:49:02 -05:00
|
|
|
class Crumb:
|
|
|
|
def __init__(self, route, label=None, parent=None):
|
|
|
|
self.route = route
|
|
|
|
self._label = label
|
|
|
|
self.parent = parent
|
|
|
|
|
|
|
|
@property
|
|
|
|
def label(self):
|
|
|
|
return self._label
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
if hasattr(other, 'route'):
|
|
|
|
return self.route == other.route
|
|
|
|
return self.route == other
|
|
|
|
|
|
|
|
def __hash__(self):
|
|
|
|
return hash(self.route)
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "Crumb('{0}')".format(self.route)
|
|
|
|
|
|
|
|
def use_match(self, match):
|
|
|
|
self.match = match
|
|
|
|
|
|
|
|
@property
|
|
|
|
def url(self):
|
|
|
|
return reverse(self.route)
|
|
|
|
|
|
|
|
|
2017-10-24 21:31:08 -04:00
|
|
|
def add(route, label=None, parent=None):
|
2018-03-07 21:49:02 -05:00
|
|
|
if not isinstance(route, Crumb):
|
|
|
|
route = Crumb(route, label, parent)
|
|
|
|
breadcrumbs[route.route] = route
|
2017-10-24 09:25:41 -04:00
|
|
|
|
|
|
|
|
2018-03-07 21:49:02 -05:00
|
|
|
def find(match):
|
2017-10-24 09:25:41 -04:00
|
|
|
crumbs = []
|
2018-03-07 21:49:02 -05:00
|
|
|
route = match.view_name
|
2017-10-24 09:25:41 -04:00
|
|
|
while route:
|
|
|
|
crumb = breadcrumbs[route]
|
2018-03-07 21:49:02 -05:00
|
|
|
crumb.use_match(match)
|
2017-10-24 09:25:41 -04:00
|
|
|
crumbs.append(crumb)
|
2018-03-07 21:49:02 -05:00
|
|
|
route = crumb.parent
|
2017-10-24 09:25:41 -04:00
|
|
|
crumbs.reverse()
|
|
|
|
return crumbs
|