Skip to content

Commit

Permalink
Store version media availability (#6278)
Browse files Browse the repository at this point in the history
* Store version media availability

- Stored during the build process
- Does a particular version have a PDF? ePub? etc.

* Add tests for missing version formats

Co-Authored-By: Manuel Kaufmann <[email protected]>
  • Loading branch information
2 people authored and agjohnson committed Oct 14, 2019
1 parent f21e6fa commit cb56df4
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 0 deletions.
3 changes: 3 additions & 0 deletions readthedocs/api/v2/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ class Meta:
'built',
'downloads',
'type',
'has_pdf',
'has_epub',
'has_htmlzip',
)


Expand Down
30 changes: 30 additions & 0 deletions readthedocs/builds/migrations/0011_version-media-availability.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.23 on 2019-10-07 23:32
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('builds', '0010_add-description-field-to-automation-rule'),
]

operations = [
migrations.AddField(
model_name='version',
name='has_epub',
field=models.BooleanField(default=False, verbose_name='Has ePub'),
),
migrations.AddField(
model_name='version',
name='has_htmlzip',
field=models.BooleanField(default=False, verbose_name='Has HTML Zip'),
),
migrations.AddField(
model_name='version',
name='has_pdf',
field=models.BooleanField(default=False, verbose_name='Has PDF'),
),
]
5 changes: 5 additions & 0 deletions readthedocs/builds/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ class Version(models.Model):
)
machine = models.BooleanField(_('Machine Created'), default=False)

# Whether the latest successful build for this version contains certain media types
has_pdf = models.BooleanField(_('Has PDF'), default=False)
has_epub = models.BooleanField(_('Has ePub'), default=False)
has_htmlzip = models.BooleanField(_('Has HTML Zip'), default=False)

objects = VersionManager.from_queryset(VersionQuerySet)()
# Only include BRANCH, TAG, UNKNOWN type Versions.
internal = InternalVersionManager.from_queryset(VersionQuerySet)()
Expand Down
4 changes: 4 additions & 0 deletions readthedocs/projects/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -904,11 +904,15 @@ def update_app_instances(
downloads, and search. Tasks are broadcast to all web servers from here.
"""
# Update version if we have successfully built HTML output
# And store whether the build had other media types
try:
if html:
version = api_v2.version(self.version.pk)
version.patch({
'built': True,
'has_pdf': pdf,
'has_epub': epub,
'has_htmlzip': localmedia,
})
except HttpClientError:
log.exception(
Expand Down
22 changes: 22 additions & 0 deletions readthedocs/rtd_tests/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2173,6 +2173,9 @@ def test_get_version_by_id(self):
'downloads': {},
'identifier': '2404a34eba4ee9c48cc8bc4055b99a48354f4950',
'slug': '0.8',
'has_epub': False,
'has_htmlzip': False,
'has_pdf': False,
}

self.assertDictEqual(
Expand Down Expand Up @@ -2213,6 +2216,25 @@ def test_get_active_versions(self):
self.assertEqual(resp.status_code, 200)
self.assertEqual(resp.data['count'], pip.versions.filter(active=False).count())

def test_modify_version(self):
pip = Project.objects.get(slug='pip')
version = pip.versions.get(slug='0.8')

data = {
'pk': version.pk,
}
resp = self.client.patch(
reverse('version-detail', kwargs=data),
data=json.dumps({'built': False, 'has_pdf': True}),
content_type='application/json',
HTTP_AUTHORIZATION='Basic {}'.format(eric_auth), # Eric is staff
)
self.assertEqual(resp.status_code, 200)
self.assertEqual(resp.data['built'], False)
self.assertEqual(resp.data['has_pdf'], True)
self.assertEqual(resp.data['has_epub'], False)
self.assertEqual(resp.data['has_htmlzip'], False)


class TaskViewsTests(TestCase):

Expand Down

0 comments on commit cb56df4

Please sign in to comment.