Skip to content

Commit 20e489c

Browse files
committed
Add docstrings
1 parent 100bac4 commit 20e489c

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

project_home_tags/templatetags/project_home.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,31 @@
1111
home_label = getattr(settings, 'PROJECT_HOME_LABEL', 'Home')
1212

1313
def home_url():
14+
"""Get project's home URL based on settings.PROJECT_HOME_NAMESPACE.
15+
16+
Returns None if PROJECT_HOME_NAMESPACE is not defined in settings.
17+
"""
1418
try:
1519
return reverse(home_namespace)
1620
except Exception:
1721
return None
1822

1923

2024
def silence_without_namespace(f):
25+
"""Decorator to silence template tags if 'PROJECT_HOME_NAMESPACE' is
26+
not defined in settings.
27+
28+
Usage Example:
29+
from django import template
30+
31+
32+
register = template.Library()
33+
34+
@register.simple_tag
35+
@silence_without_namespace
36+
def a_template_tag(*args):
37+
...
38+
"""
2139
@wraps(f)
2240
def wrapped(label=None):
2341
if not home_namespace:
@@ -32,6 +50,17 @@ def wrapped(label=None):
3250
@register.simple_tag
3351
@silence_without_namespace
3452
def project_home_url(*args):
53+
"""A template tag to return the project's home URL.
54+
55+
PROJECT_HOME_NAMESPACE must be defined in settings.
56+
For example:
57+
PROJECT_HOME_NAMESPACE = 'project_name:index_view'
58+
59+
Usage Example:
60+
{% load project_home_tags %}
61+
62+
<a href="{% project_home_url %}">Home</a>
63+
"""
3564
url = home_url()
3665
if url:
3766
return url
@@ -40,6 +69,36 @@ def project_home_url(*args):
4069
@register.simple_tag
4170
@silence_without_namespace
4271
def project_home_breadcrumb_bs3(label):
72+
"""A template tag to return the project's home URL and label
73+
formatted as a Bootstrap 3 breadcrumb.
74+
75+
PROJECT_HOME_NAMESPACE must be defined in settings, for example:
76+
PROJECT_HOME_NAMESPACE = 'project_name:index_view'
77+
78+
Usage Example:
79+
{% load project_home_tags %}
80+
81+
<ol class="breadcrumb">
82+
{% project_home_breadcrumb_bs3 %} {# <--- #}
83+
<li><a href="{% url 'app:namespace' %}">List of Objects</a></li>
84+
<li class="active">Object Detail</li>
85+
</ol>
86+
87+
This gets converted into:
88+
<ol class="breadcrumb">
89+
<li><a href="{% url 'project_name:index_view' %}">Home</a></li> {# <--- #}
90+
<li><a href="{% url 'app:namespace' %}">List of Objects</a></li>
91+
<li class="active">Object Detail</li>
92+
</ol>
93+
94+
By default, the link's text is 'Home'. A project-wide label can be
95+
defined with PROJECT_HOME_LABEL in settings. Both the default and
96+
the project-wide label can be overridden by passing a string to
97+
the template tag.
98+
99+
For example:
100+
{% project_home_breadcrumb_bs3 'Custom Label' %}
101+
"""
43102
url = home_url()
44103
if url:
45104
return format_html(
@@ -51,6 +110,36 @@ def project_home_breadcrumb_bs3(label):
51110
@register.simple_tag
52111
@silence_without_namespace
53112
def project_home_breadcrumb_bs4(label):
113+
"""A template tag to return the project's home URL and label
114+
formatted as a Bootstrap 4 breadcrumb.
115+
116+
PROJECT_HOME_NAMESPACE must be defined in settings, for example:
117+
PROJECT_HOME_NAMESPACE = 'project_name:index_view'
118+
119+
Usage Example:
120+
{% load project_home_tags %}
121+
122+
<ol class="breadcrumb">
123+
{% project_home_breadcrumb_bs4 %} {# <--- #}
124+
<li class="breadcrumb-item" aria-label="breadcrumb"><a href="{% url 'app:namespace' %}">List of Objects</a></li>
125+
<li class=" breadcrumb-item active" aria-label="breadcrumb" aria-current="page">Object Detail</li>
126+
</ol>
127+
128+
This gets converted into:
129+
<ol class="breadcrumb">
130+
<li class="breadcrumb-item" aria-label="breadcrumb"><a href="{% url 'project_name:index_view' %}">Home</a></li> {# <--- #}
131+
<li class="breadcrumb-item" aria-label="breadcrumb"><a href="{% url 'app:namespace' %}">List of Objects</a></li>
132+
<li class=" breadcrumb-item active" aria-label="breadcrumb" aria-current="page">Object Detail</li>
133+
</ol>
134+
135+
By default, the link's text is 'Home'. A project-wide label can be
136+
defined with PROJECT_HOME_LABEL in settings. Both the default and
137+
the project-wide label can be overridden by passing a string to
138+
the template tag.
139+
140+
For example:
141+
{% project_home_breadcrumb_bs4 'Custom Label' %}
142+
"""
54143
url = home_url()
55144
if url:
56145
return format_html(

0 commit comments

Comments
 (0)