Hide keyboard shortcuts

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 

2 

3from django.urls import reverse 

4 

5# Exports 

6 

7__all__ = ( 

8 "Breadcrumb", 

9 "Breadcrumbs", 

10) 

11 

12# Classes 

13 

14 

15class Breadcrumb(object): 

16 """Helper class that ensures an object has the required attributes.""" 

17 

18 def __init__(self, text, url): 

19 """Initialize the crumb. 

20 

21 :param text: The text of the link. 

22 :type text: str 

23 

24 :param url: The URL of the link. 

25 :type url: str 

26 

27 """ 

28 self.text = text 

29 self.url = url 

30 

31 def __repr__(self): 

32 return "<%s %s>" % (self.__class__.__name__, self.text) 

33 

34 def get_absolute_url(self): 

35 """Allow the crumb to simulate a model with ``get_absolute_url()``. 

36 

37 :rtype: str 

38 

39 """ 

40 return self.url 

41 

42 

43class Breadcrumbs(object): 

44 """Collect objects to be displayed as breadcrumbs.""" 

45 

46 def __init__(self): 

47 """Initialize the ``items`` attribute for a new collection of crumbs.""" 

48 self.items = list() 

49 

50 def __iter__(self): 

51 return iter(self.items) 

52 

53 def add(self, text, url, pattern_args=None, pattern_kwargs=None, namespace=None): 

54 """Add a breadcrumb to the list. 

55 

56 :param text: The breadcrumb label/text. 

57 :type text: str 

58 

59 :param url: The URL or pattern name. 

60 :type url: str 

61 

62 :param pattern_args: Pattern arguments when the URL is given as a pattern name. 

63 :type pattern_args: list 

64 

65 :param pattern_kwargs: Pattern keyword arguments when the URL is given as a pattern name. 

66 :type pattern_kwargs: dict 

67 

68 :param namespace: The application namespace for the URL when given as a pattern name. 

69 :type namespace: str 

70 

71 :rtype: Breadcrumb 

72 

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 

79 

80 resolved_url = reverse(_url, args=pattern_args, kwargs=pattern_kwargs) 

81 else: 

82 resolved_url = url 

83 

84 breadcrumb = Breadcrumb(text, resolved_url) 

85 self.items.append(breadcrumb) 

86 

87 return breadcrumb 

88 

89 def insert(self, index, text, url, pattern_args=None, pattern_kwargs=None, namespace=None): 

90 """Insert a breadcrumb in the list. 

91 

92 :param index: The index of the new breadcrumb.. 

93 :type index: int 

94 

95 :param text: The breadcrumb label/text. 

96 :type text: str 

97 

98 :param url: The URL or pattern name. 

99 :type url: str 

100 

101 :param pattern_args: Pattern arguments when the URL is given as a pattern name. 

102 :type pattern_args: list 

103 

104 :param pattern_kwargs: Pattern keyword arguments when the URL is given as a pattern name. 

105 :type pattern_kwargs: dict 

106 

107 :param namespace: The application namespace for the URL when given as a pattern name. 

108 :type namespace: str 

109 

110 :rtype: Breadcrumb 

111 

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 

118 

119 resolved_url = reverse(_url, args=pattern_args, kwargs=pattern_kwargs) 

120 else: 

121 resolved_url = url 

122 

123 breadcrumb = Breadcrumb(text, resolved_url) 

124 self.items.insert(index, breadcrumb) 

125 

126 return breadcrumb