Repository: readthedocs/readthedocs.org

Issue:
Make /random/ path works
Currently hitting https://readthedocs.org/random/ produces 502 and hitting https://readthedocs.org/random/pip/ may return a `.js` file or a non `.html` file which is not useful.

We need to fix it and return always return a HTML file. For this, we could use the model `HTMLFile`.


Discussion:
working on it :)

Resolving patch:
diff --git a/readthedocs/core/views/__init__.py b/readthedocs/core/views/__init__.py
--- a/readthedocs/core/views/__init__.py
+++ b/readthedocs/core/views/__init__.py
@@ -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__)
 
@@ -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)
 
 


Test patch:
diff --git a/readthedocs/rtd_tests/tests/test_views.py b/readthedocs/rtd_tests/tests/test_views.py
--- a/readthedocs/rtd_tests/tests/test_views.py
+++ b/readthedocs/rtd_tests/tests/test_views.py
@@ -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):
@@ -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',
@@ -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)