Skip to content

Switch to static SVG badges #3057

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

Merged
merged 3 commits into from
Aug 29, 2017
Merged
Show file tree
Hide file tree
Changes from 2 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
16 changes: 4 additions & 12 deletions docs/badges.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,11 @@ since it will stay up to date with your Read the Docs project::

https://readthedocs.org/projects/pip/badge/

Style
-----

If you pass the ``style`` GET argument,
we will pass it along to shields.io as is.
This will allow you to have custom style badges.


.. _Read the Docs README: https://github.com/rtfd/readthedocs.org/blob/master/README.rst
.. _project page: https://readthedocs.org/projects/pip/
.. |green| image:: https://img.shields.io/badge/docs-latest-brightgreen.svg?style=flat
.. |red| image:: https://img.shields.io/badge/docs-release--1.6-red.svg?style=flat
.. |yellow| image:: https://img.shields.io/badge/docs-no%20builds-yellow.svg?style=flat
.. |nbsp| unicode:: 0xA0
.. |green| image:: https://media.readthedocs.org/static/projects/badges/passing.svg
.. |red| image:: https://media.readthedocs.org/static/projects/badges/failing.svg
.. |yellow| image:: https://media.readthedocs.org/static/projects/badges/unknown.svg
.. |nbsp| unicode:: 0xA0
:trim:

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"
Copy link
Member

Choose a reason for hiding this comment

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

linting says this should be lower case.

Copy link
Member

@ericholscher ericholscher Aug 29, 2017

Choose a reason for hiding this comment

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

I also believe this probably needs to have a MEDIA_URL prepended to it, for production.

Nevermind, I missed the static call :)

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)