Skip to content

Make /random/ path work #5496

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
Mar 19, 2019
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
20 changes: 11 additions & 9 deletions readthedocs/core/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
from readthedocs.core.utils.general import wipe_version_via_slugs
from readthedocs.core.resolver import resolve_path
from readthedocs.core.symlink import PrivateSymlink, PublicSymlink
from readthedocs.core.utils import broadcast
from readthedocs.core.views.serve import _serve_file
from readthedocs.projects.constants import PRIVATE
from readthedocs.projects.models import Project, ImportedFile
from readthedocs.projects.tasks import remove_dirs
from readthedocs.redirects.utils import get_redirect_response, project_and_path_from_request, language_and_version_from_path
from readthedocs.projects.models import HTMLFile, Project
from readthedocs.redirects.utils import (
get_redirect_response,
project_and_path_from_request,
language_and_version_from_path
)

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -66,13 +68,13 @@ def get_context_data(self, **kwargs):


def random_page(request, project_slug=None): # pylint: disable=unused-argument
imported_file = ImportedFile.objects.order_by('?')
html_file = HTMLFile.objects.order_by('?')
if project_slug:
imported_file = imported_file.filter(project__slug=project_slug)
imported_file = imported_file.first()
if imported_file is None:
html_file = html_file.filter(project__slug=project_slug)
html_file = html_file.first()
if html_file is None:
raise Http404
url = imported_file.get_absolute_url()
url = html_file.get_absolute_url()
return HttpResponseRedirect(url)


Expand Down
10 changes: 5 additions & 5 deletions readthedocs/rtd_tests/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from readthedocs.builds.models import Build
from readthedocs.core.permissions import AdminPermission
from readthedocs.projects.forms import UpdateProjectForm
from readthedocs.projects.models import ImportedFile, Project
from readthedocs.projects.models import HTMLFile, Project


class Testmaker(TestCase):
Expand Down Expand Up @@ -170,10 +170,10 @@ class RandomPageTests(TestCase):
def setUp(self):
self.pip = Project.objects.get(slug='pip')
self.pip_version = self.pip.versions.all()[0]
ImportedFile.objects.create(
HTMLFile.objects.create(
project=self.pip,
version=self.pip_version,
name='File',
name='file.html',
slug='file',
path='file.html',
md5='abcdef',
Expand All @@ -193,8 +193,8 @@ 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()
def test_404_for_with_no_html_files(self):
HTMLFile.objects.all().delete()
response = self.client.get('/random/pip/')
self.assertEqual(response.status_code, 404)

Expand Down