Skip to content

Commit

Permalink
Support badge maxAge (fix #3000 and #1612)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
wmayner committed Jul 17, 2017
1 parent 96f6da5 commit 8c37801
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions readthedocs/projects/views/public.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,27 +107,30 @@ def project_badge(request, project_slug):
"""Return a sweet badge for the project"""
version_slug = request.GET.get('version', LATEST)
style = request.GET.get('style', 'flat')
# Default to 24 hour cache lifetime
max_age = request.GET.get('maxAge', 86400)
try:
version = Version.objects.public(request.user).get(
project__slug=project_slug, slug=version_slug)
except Version.DoesNotExist:
url = (
'https://img.shields.io/badge/docs-unknown%20version-yellow.svg?style={style}'
.format(style=style))
'https://img.shields.io/badge/docs-unknown%20version-yellow.svg?style={style}&maxAge={max_age}'
.format(style=style, max_age=max_age))
return HttpResponseRedirect(url)
version_builds = version.builds.filter(type='html', state='finished').order_by('-date')
if not version_builds.exists():
url = (
'https://img.shields.io/badge/docs-no%20builds-yellow.svg?style={style}'
.format(style=style))
'https://img.shields.io/badge/docs-no%20builds-yellow.svg?style={style}&maxAge={max_age}'
.format(style=style, max_age=max_age))
return HttpResponseRedirect(url)
last_build = version_builds[0]
if last_build.success:
color = 'brightgreen'
else:
color = 'red'
url = 'https://img.shields.io/badge/docs-%s-%s.svg?style=%s' % (
version.slug.replace('-', '--'), color, style)
url = ('https://img.shields.io/badge/docs-{version}-{color}.svg?style={style}&maxAge={max_age}'
.format(version=version.slug.replace('-', '--'), color=color,
style=style, max_age=max_age))
return HttpResponseRedirect(url)


Expand Down

0 comments on commit 8c37801

Please sign in to comment.