Skip to content

Don't do unnecessary queries when listing subprojects #6869

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 4 commits into from
Apr 7, 2020
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
30 changes: 30 additions & 0 deletions readthedocs/projects/views/private.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import csv
import logging
from urllib.parse import urlparse

from allauth.socialaccount.models import SocialAccount
from django.conf import settings
Expand Down Expand Up @@ -44,6 +45,7 @@
LoginRequiredMixin,
PrivateViewMixin,
)
from readthedocs.core.resolver import resolve, resolve_path
from readthedocs.core.utils import broadcast, trigger_build
from readthedocs.core.utils.extend import SettingsOverrideObject
from readthedocs.integrations.models import HttpExchange, Integration
Expand Down Expand Up @@ -462,8 +464,36 @@ class ProjectRelationshipList(ProjectRelationshipMixin, ListView):
def get_context_data(self, **kwargs):
ctx = super().get_context_data(**kwargs)
ctx['superproject'] = self.project.superprojects.first()
ctx['subprojects_and_urls'] = self.get_subprojects_and_urls()
return ctx

def get_subprojects_and_urls(self):
"""
Get a tuple of subprojects and its absolute URls.

All subprojects share the domain from the parent,
so instead of resolving the domain and path for each subproject,
we resolve only the path of each one.
"""
subprojects_and_urls = []

main_domain = resolve(self.project)
parsed_main_domain = urlparse(main_domain)

subprojects = self.object_list.select_related('child')
for subproject in subprojects:
subproject_path = resolve_path(subproject.child)
parsed_subproject_domain = parsed_main_domain._replace(
path=subproject_path,
)
subprojects_and_urls.append(
(
subproject,
parsed_subproject_domain.geturl(),
)
)
return subprojects_and_urls


class ProjectRelationshipCreate(ProjectRelationshipMixin, CreateView):

Expand Down
6 changes: 3 additions & 3 deletions readthedocs/templates/projects/projectrelationship_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
<div class="module-list">
<div class="module-list-wrapper">
<ul class="subprojects">
{% for subproject in object_list %}
{% for subproject, absolute_url in subprojects_and_urls %}
<li class="module-item subproject">
<div class="module-title">
<a
Expand All @@ -58,8 +58,8 @@
</a>
<span>
<a class="module-info subproject-url"
href="{{ subproject.get_absolute_url }}">
{{ subproject.get_absolute_url }}
href="{{ absolute_url }}">
{{ absolute_url }}
</a>
</span>
</div>
Expand Down