Skip to content

Commit

Permalink
Use static SVGs to serve badges.
Browse files Browse the repository at this point in the history
  • Loading branch information
jerivas committed Aug 17, 2017
1 parent 27bf3d3 commit 91f6c08
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 20 deletions.
1 change: 1 addition & 0 deletions readthedocs/projects/static/projects/badges/failing.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions readthedocs/projects/static/projects/badges/passing.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions readthedocs/projects/static/projects/badges/unknown.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 6 additions & 18 deletions readthedocs/projects/views/public.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from django.conf import settings
from django.contrib import messages
from django.contrib.auth.models import User
from django.contrib.staticfiles.templatetags.staticfiles import static
from django.http import HttpResponse, HttpResponseRedirect, Http404
from django.shortcuts import get_object_or_404, render_to_response
from django.template import RequestContext
Expand Down Expand Up @@ -105,36 +106,23 @@ def get_context_data(self, **kwargs):
@never_cache
def project_badge(request, project_slug):
"""Return a sweet badge for the project"""
BADGE_PATH = "projects/badges/%s.svg"
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}&maxAge={max_age}'
.format(style=style, max_age=max_age))
url = static(BADGE_PATH % "unknown")
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}&maxAge={max_age}'
.format(style=style, max_age=max_age))
url = static(BADGE_PATH % "unknown")
return HttpResponseRedirect(url)
last_build = version_builds[0]
if last_build.success:
color = 'brightgreen'
url = static(BADGE_PATH % "passing")
else:
color = 'red'
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))
url = static(BADGE_PATH % "failing")
return HttpResponseRedirect(url)


Expand Down
32 changes: 30 additions & 2 deletions readthedocs/rtd_tests/tests/test_project_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@
from mock import patch
from django.test import TestCase
from django.contrib.auth.models import User
from django.contrib.staticfiles.templatetags.staticfiles import static
from django.contrib.messages import constants as message_const
from django.core.urlresolvers import reverse
from django.http.response import HttpResponseRedirect
from django.views.generic.base import ContextMixin
from django_dynamic_fixture import get
from django_dynamic_fixture import new

import six

from readthedocs.core.models import UserProfile
from readthedocs.builds.models import Build, Version
from readthedocs.rtd_tests.base import (WizardTestCase, MockBuildTestCase,
RequestFactoryTestMixin)
from readthedocs.oauth.models import RemoteRepository
from readthedocs.projects.exceptions import ProjectSpamError
from readthedocs.projects.forms import ProjectBasicsForm
from readthedocs.projects.models import Project, Domain
from readthedocs.projects.views.private import ImportWizardView
from readthedocs.projects.views.mixins import ProjectRelationMixin
Expand Down Expand Up @@ -418,3 +419,30 @@ def get_project_queryset(self):
self.assertEqual(view.get_project(), self.project)
self.assertEqual(view.get_queryset().first(), self.domain)
self.assertEqual(view.get_context_data()['project'], self.project)


class TestBadges(TestCase):
"""Test a static badge asset is served for each build."""

def setUp(self):
self.BADGE_PATH = 'projects/badges/%s.svg'
self.project = get(Project, slug='badgey')
self.version = Version.objects.get(project=self.project)
self.badge_url = reverse('project_badge', args=[self.project.slug])

def test_unknown_badge(self):
res = self.client.get(self.badge_url, {'version': self.version.slug})
static_badge = static(self.BADGE_PATH % 'unknown')
self.assertEquals(res.url, static_badge)

def test_passing_badge(self):
get(Build, project=self.project, version=self.version, success=True)
res = self.client.get(self.badge_url, {'version': self.version.slug})
static_badge = static(self.BADGE_PATH % 'passing')
self.assertEquals(res.url, static_badge)

def test_failing_badge(self):
get(Build, project=self.project, version=self.version, success=False)
res = self.client.get(self.badge_url, {'version': self.version.slug})
static_badge = static(self.BADGE_PATH % 'failing')
self.assertEquals(res.url, static_badge)

0 comments on commit 91f6c08

Please sign in to comment.