Repository: readthedocs/readthedocs.org

Issue:
502 Bad Gateway on Projects > Versions page
The Projects > Versions page for our project is throwing a 502 Bad Gateway error. It's happening at the following URL: 

https://readthedocs.org/projects/htcondor/versions

I thought this might be a transient error but it's been happening consistently for the last day or so. All the other pages I've tried seem to work fine.

It's worth noting a similar ticket  #5441  was filed a couple of days ago for a 502 error on the Import a Repository page. This seems to be working correctly now.

## Details

* Read the Docs project URL: https://htcondor.readthedocs.io
* Build URL (if applicable): 
* Read the Docs username (if applicable): htcondor

## Expected Result

A list of the different versions of our docs, dropdown menus to connect them to different branches, etc.

## Actual Result

Server Error

502 - Web server received an invalid response while acting as a gateway or proxy server.

There is a problem with the page you are looking for, and it cannot be displayed. When the Web server (while acting as a gateway or proxy) contacted the upstream content server, it received an invalid response from the content server.



Discussion:
Hi @markcoatsworth 
I am able to open the webpage.
![Screenshot from 2019-03-14 21-36-04](https://user-images.githubusercontent.com/29149191/54372408-41417a80-46a1-11e9-9d8a-7c1b0e97ed96.png)

I'm seeing 502 too, Investigating now
Looks like this only happens to users with admin/owner privileges.
Thanks @dojutsu-user for looking into this! I just checked again and @stsewd is correct. The page loads correctly when not logged in. But when I log in as the project owner it's still throwing the 502 error.
Just an update here, the problem is caused due the amount of versions (branches and tags) that your project has. We are trying to figure out where the bottleneck is, but if you don't need all those branches/tags removing them from the repo could fix the problem.

Resolving patch:
diff --git a/readthedocs/core/permissions.py b/readthedocs/core/permissions.py
--- a/readthedocs/core/permissions.py
+++ b/readthedocs/core/permissions.py
@@ -9,6 +9,8 @@ class AdminPermissionBase:
 
     @classmethod
     def is_admin(cls, user, project):
+        # This explicitly uses "user in project.users.all" so that
+        # users on projects can be cached using prefetch_related or prefetch_related_objects
         return user in project.users.all() or user.is_superuser
 
     @classmethod
diff --git a/readthedocs/projects/views/public.py b/readthedocs/projects/views/public.py
--- a/readthedocs/projects/views/public.py
+++ b/readthedocs/projects/views/public.py
@@ -13,6 +13,7 @@
 from django.contrib.auth.models import User
 from django.core.cache import cache
 from django.core.files.storage import get_storage_class
+from django.db.models import prefetch_related_objects
 from django.http import HttpResponse, HttpResponseRedirect
 from django.shortcuts import get_object_or_404, render
 from django.urls import reverse
@@ -271,6 +272,9 @@ def project_versions(request, project_slug):
     if wiped and wiped_version.count():
         messages.success(request, 'Version wiped: ' + wiped)
 
+    # Optimize project permission checks
+    prefetch_related_objects([project], 'users')
+
     return render(
         request,
         'projects/project_version_list.html',


Test patch: