Skip to content

Commit 839a6ec

Browse files
authored
Merge pull request #6869 from readthedocs/list-subprojects-fix
Don't do unnecessary queries when listing subprojects
2 parents 5355f90 + 7d3eea8 commit 839a6ec

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

readthedocs/projects/views/private.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import csv
44
import logging
5+
from urllib.parse import urlparse
56

67
from allauth.socialaccount.models import SocialAccount
78
from django.conf import settings
@@ -44,6 +45,7 @@
4445
LoginRequiredMixin,
4546
PrivateViewMixin,
4647
)
48+
from readthedocs.core.resolver import resolve, resolve_path
4749
from readthedocs.core.utils import broadcast, trigger_build
4850
from readthedocs.core.utils.extend import SettingsOverrideObject
4951
from readthedocs.integrations.models import HttpExchange, Integration
@@ -462,8 +464,36 @@ class ProjectRelationshipList(ProjectRelationshipMixin, ListView):
462464
def get_context_data(self, **kwargs):
463465
ctx = super().get_context_data(**kwargs)
464466
ctx['superproject'] = self.project.superprojects.first()
467+
ctx['subprojects_and_urls'] = self.get_subprojects_and_urls()
465468
return ctx
466469

470+
def get_subprojects_and_urls(self):
471+
"""
472+
Get a tuple of subprojects and its absolute URls.
473+
474+
All subprojects share the domain from the parent,
475+
so instead of resolving the domain and path for each subproject,
476+
we resolve only the path of each one.
477+
"""
478+
subprojects_and_urls = []
479+
480+
main_domain = resolve(self.project)
481+
parsed_main_domain = urlparse(main_domain)
482+
483+
subprojects = self.object_list.select_related('child')
484+
for subproject in subprojects:
485+
subproject_path = resolve_path(subproject.child)
486+
parsed_subproject_domain = parsed_main_domain._replace(
487+
path=subproject_path,
488+
)
489+
subprojects_and_urls.append(
490+
(
491+
subproject,
492+
parsed_subproject_domain.geturl(),
493+
)
494+
)
495+
return subprojects_and_urls
496+
467497

468498
class ProjectRelationshipCreate(ProjectRelationshipMixin, CreateView):
469499

readthedocs/templates/projects/projectrelationship_list.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
<div class="module-list">
4949
<div class="module-list-wrapper">
5050
<ul class="subprojects">
51-
{% for subproject in object_list %}
51+
{% for subproject, absolute_url in subprojects_and_urls %}
5252
<li class="module-item subproject">
5353
<div class="module-title">
5454
<a
@@ -58,8 +58,8 @@
5858
</a>
5959
<span>
6060
<a class="module-info subproject-url"
61-
href="{{ subproject.get_absolute_url }}">
62-
{{ subproject.get_absolute_url }}
61+
href="{{ absolute_url }}">
62+
{{ absolute_url }}
6363
</a>
6464
</span>
6565
</div>

0 commit comments

Comments
 (0)