Skip to content

Commit 91f6c08

Browse files
committed
Use static SVGs to serve badges.
1 parent 27bf3d3 commit 91f6c08

File tree

5 files changed

+39
-20
lines changed

5 files changed

+39
-20
lines changed
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 1 addition & 0 deletions
Loading

readthedocs/projects/views/public.py

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from django.conf import settings
1414
from django.contrib import messages
1515
from django.contrib.auth.models import User
16+
from django.contrib.staticfiles.templatetags.staticfiles import static
1617
from django.http import HttpResponse, HttpResponseRedirect, Http404
1718
from django.shortcuts import get_object_or_404, render_to_response
1819
from django.template import RequestContext
@@ -105,36 +106,23 @@ def get_context_data(self, **kwargs):
105106
@never_cache
106107
def project_badge(request, project_slug):
107108
"""Return a sweet badge for the project"""
109+
BADGE_PATH = "projects/badges/%s.svg"
108110
version_slug = request.GET.get('version', LATEST)
109-
style = request.GET.get('style', 'flat')
110-
# Default to 24 hour cache lifetime
111-
max_age = request.GET.get('maxAge', 86400)
112111
try:
113112
version = Version.objects.public(request.user).get(
114113
project__slug=project_slug, slug=version_slug)
115114
except Version.DoesNotExist:
116-
url = (
117-
'https://img.shields.io/badge/docs-unknown%20version-yellow.svg'
118-
'?style={style}&maxAge={max_age}'
119-
.format(style=style, max_age=max_age))
115+
url = static(BADGE_PATH % "unknown")
120116
return HttpResponseRedirect(url)
121117
version_builds = version.builds.filter(type='html', state='finished').order_by('-date')
122118
if not version_builds.exists():
123-
url = (
124-
'https://img.shields.io/badge/docs-no%20builds-yellow.svg'
125-
'?style={style}&maxAge={max_age}'
126-
.format(style=style, max_age=max_age))
119+
url = static(BADGE_PATH % "unknown")
127120
return HttpResponseRedirect(url)
128121
last_build = version_builds[0]
129122
if last_build.success:
130-
color = 'brightgreen'
123+
url = static(BADGE_PATH % "passing")
131124
else:
132-
color = 'red'
133-
url = (
134-
'https://img.shields.io/badge/docs-{version}-{color}.svg'
135-
'?style={style}&maxAge={max_age}'
136-
.format(version=version.slug.replace('-', '--'), color=color,
137-
style=style, max_age=max_age))
125+
url = static(BADGE_PATH % "failing")
138126
return HttpResponseRedirect(url)
139127

140128

readthedocs/rtd_tests/tests/test_project_views.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,21 @@
44
from mock import patch
55
from django.test import TestCase
66
from django.contrib.auth.models import User
7+
from django.contrib.staticfiles.templatetags.staticfiles import static
78
from django.contrib.messages import constants as message_const
9+
from django.core.urlresolvers import reverse
810
from django.http.response import HttpResponseRedirect
911
from django.views.generic.base import ContextMixin
1012
from django_dynamic_fixture import get
1113
from django_dynamic_fixture import new
1214

1315
import six
1416

15-
from readthedocs.core.models import UserProfile
17+
from readthedocs.builds.models import Build, Version
1618
from readthedocs.rtd_tests.base import (WizardTestCase, MockBuildTestCase,
1719
RequestFactoryTestMixin)
1820
from readthedocs.oauth.models import RemoteRepository
1921
from readthedocs.projects.exceptions import ProjectSpamError
20-
from readthedocs.projects.forms import ProjectBasicsForm
2122
from readthedocs.projects.models import Project, Domain
2223
from readthedocs.projects.views.private import ImportWizardView
2324
from readthedocs.projects.views.mixins import ProjectRelationMixin
@@ -418,3 +419,30 @@ def get_project_queryset(self):
418419
self.assertEqual(view.get_project(), self.project)
419420
self.assertEqual(view.get_queryset().first(), self.domain)
420421
self.assertEqual(view.get_context_data()['project'], self.project)
422+
423+
424+
class TestBadges(TestCase):
425+
"""Test a static badge asset is served for each build."""
426+
427+
def setUp(self):
428+
self.BADGE_PATH = 'projects/badges/%s.svg'
429+
self.project = get(Project, slug='badgey')
430+
self.version = Version.objects.get(project=self.project)
431+
self.badge_url = reverse('project_badge', args=[self.project.slug])
432+
433+
def test_unknown_badge(self):
434+
res = self.client.get(self.badge_url, {'version': self.version.slug})
435+
static_badge = static(self.BADGE_PATH % 'unknown')
436+
self.assertEquals(res.url, static_badge)
437+
438+
def test_passing_badge(self):
439+
get(Build, project=self.project, version=self.version, success=True)
440+
res = self.client.get(self.badge_url, {'version': self.version.slug})
441+
static_badge = static(self.BADGE_PATH % 'passing')
442+
self.assertEquals(res.url, static_badge)
443+
444+
def test_failing_badge(self):
445+
get(Build, project=self.project, version=self.version, success=False)
446+
res = self.client.get(self.badge_url, {'version': self.version.slug})
447+
static_badge = static(self.BADGE_PATH % 'failing')
448+
self.assertEquals(res.url, static_badge)

0 commit comments

Comments
 (0)