Skip to content

Commit 8c37801

Browse files
committed
Support badge maxAge (fix #3000 and #1612)
Pass on the `maxAge` query parameter when redirecting to the badge image from Shields.io. This should help with GitHub's unwanted badge caching. The default is 1 day.
1 parent 96f6da5 commit 8c37801

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

readthedocs/projects/views/public.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,27 +107,30 @@ def project_badge(request, project_slug):
107107
"""Return a sweet badge for the project"""
108108
version_slug = request.GET.get('version', LATEST)
109109
style = request.GET.get('style', 'flat')
110+
# Default to 24 hour cache lifetime
111+
max_age = request.GET.get('maxAge', 86400)
110112
try:
111113
version = Version.objects.public(request.user).get(
112114
project__slug=project_slug, slug=version_slug)
113115
except Version.DoesNotExist:
114116
url = (
115-
'https://img.shields.io/badge/docs-unknown%20version-yellow.svg?style={style}'
116-
.format(style=style))
117+
'https://img.shields.io/badge/docs-unknown%20version-yellow.svg?style={style}&maxAge={max_age}'
118+
.format(style=style, max_age=max_age))
117119
return HttpResponseRedirect(url)
118120
version_builds = version.builds.filter(type='html', state='finished').order_by('-date')
119121
if not version_builds.exists():
120122
url = (
121-
'https://img.shields.io/badge/docs-no%20builds-yellow.svg?style={style}'
122-
.format(style=style))
123+
'https://img.shields.io/badge/docs-no%20builds-yellow.svg?style={style}&maxAge={max_age}'
124+
.format(style=style, max_age=max_age))
123125
return HttpResponseRedirect(url)
124126
last_build = version_builds[0]
125127
if last_build.success:
126128
color = 'brightgreen'
127129
else:
128130
color = 'red'
129-
url = 'https://img.shields.io/badge/docs-%s-%s.svg?style=%s' % (
130-
version.slug.replace('-', '--'), color, style)
131+
url = ('https://img.shields.io/badge/docs-{version}-{color}.svg?style={style}&maxAge={max_age}'
132+
.format(version=version.slug.replace('-', '--'), color=color,
133+
style=style, max_age=max_age))
131134
return HttpResponseRedirect(url)
132135

133136

0 commit comments

Comments
 (0)