Skip to content

WIP: Add basic badge support. #2071

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 32 additions & 23 deletions readthedocs/projects/views/public.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import mimetypes
import md5

from django.template import loader
from django.core.urlresolvers import reverse
from django.core.cache import cache
from django.conf import settings
Expand Down Expand Up @@ -106,45 +107,53 @@ def get_context_data(self, **kwargs):
return context


def _badge_return(redirect, url):
if redirect:
return HttpResponseRedirect(url)
def _badge_return(version, status):
template = loader.get_template('badges/badge.svg')
if status == 'passing':
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a nitpick, but using a {'passing': ..., 'failing': ..., 'unknown': ...} dictionary would be simpler and shorter.

fill = '#4c1'
elif status == 'failing':
fill = '#e05d44'
else:
response = requests.get(url)
http_response = HttpResponse(response.content,
content_type="image/svg+xml")
http_response['Cache-Control'] = 'no-cache'
http_response['Etag'] = md5.new(url)
return http_response
fill = '#dfb317'
content = template.render({
'project': version.project.slug,
'project_length': len(version.project.slug) * 5,
'project_fill': len(version.project.slug) * 5 / 2 + 1,
'version': version.slug,
'version_length': len(version.slug) * 5,
'version_fill': len(version.slug) * 5 / 2 + 1,
'status': status,
'fill': fill,
'full_width': (len(version.project.slug) + len(version.slug)) * 5,
})
http_response = HttpResponse(content,
content_type="image/svg+xml")
http_response['Cache-Control'] = 'no-cache'
http_response['Etag'] = md5.new(content)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

md5 module has been deprecated since Python 2.5. I'd replace it with hashlib.md5(content).hexdigest().

return http_response


@cache_control(no_cache=True)
def project_badge(request, project_slug, redirect=True):
"""Return a sweet badge for the project"""
version_slug = request.GET.get('version', LATEST)
style = request.GET.get('style', 'flat')

status = 'passing'

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))
return _badge_return(redirect, url)
status = 'unknown'
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))
return _badge_return(redirect, url)
status = 'unknown'
last_build = version_builds[0]
if last_build.success:
color = 'brightgreen'
status = 'passing'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the default state of status is 'passing', I think we can simplify this to:

if not last_build.success:
    status = 'failing'

else:
color = 'red'
url = 'https://img.shields.io/badge/docs-%s-%s.svg?style=%s' % (
version.slug.replace('-', '--'), color, style)
return _badge_return(redirect, url)
color = 'failing'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

color looks like a typo to me.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, the bigger issue here is fixing the SVG template. I just built a very basic example, but need to figure out the SVG part before we can ship it.

return _badge_return(version, status)


def project_downloads(request, project_slug):
Expand Down
10 changes: 10 additions & 0 deletions readthedocs/templates/badges/badge.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.