Skip to content

Fix undeclared variable in core.views.random_page #1514

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 1 commit into from
Jul 31, 2015
Merged
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
7 changes: 5 additions & 2 deletions readthedocs/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,12 @@ def get_context_data(self, **kwargs):

def random_page(request, project_slug=None):
imported_file = ImportedFile.objects.order_by('?')
if project:
if project_slug:
imported_file = imported_file.filter(project__slug=project_slug)
url = imported_file.first().get_absolute_url()
imported_file = imported_file.first()
if imported_file is None:
raise Http404
url = imported_file.get_absolute_url()
return HttpResponseRedirect(url)


Expand Down
35 changes: 35 additions & 0 deletions readthedocs/rtd_tests/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.utils.six.moves.urllib.parse import urlsplit

from readthedocs.builds.constants import LATEST
from readthedocs.projects.models import ImportedFile
from readthedocs.projects.models import Project
from readthedocs.projects.forms import UpdateProjectForm

Expand Down Expand Up @@ -168,3 +169,37 @@ def test_project_redirects(self):
def test_project_redirects_delete(self):
response = self.client.get('/dashboard/pip/redirects/delete/')
self.assertRedirectToLogin(response)


class RandomPageTests(TestCase):
fixtures = ['eric', 'test_data']

def setUp(self):
self.pip = Project.objects.get(slug='pip')
self.pip_version = self.pip.versions.all()[0]
ImportedFile.objects.create(
project=self.pip,
version=self.pip_version,
name='File',
slug='file',
path='file.html',
md5='abcdef',
commit='1234567890abcdef')

def test_random_page_view_redirects(self):
response = self.client.get('/random/')
self.assertEqual(response.status_code, 302)

def test_takes_project_slug(self):
response = self.client.get('/random/pip/')
self.assertEqual(response.status_code, 302)
self.assertTrue('/pip/' in response['Location'])

def test_404_for_unknown_project(self):
response = self.client.get('/random/not-existent/')
self.assertEqual(response.status_code, 404)

def test_404_for_with_no_imported_files(self):
ImportedFile.objects.all().delete()
response = self.client.get('/random/pip/')
self.assertEqual(response.status_code, 404)