Skip to content

Commit e2a23c7

Browse files
committed
Don't do unnecessary queries when listing subprojects
This together with: - #6867 - #6865 - #6864 reduces a lot of queries and without changing the current UI. This is a benchmark using the django debug toolbar: Before: 138 queries with 7 subprojects, 17 queries per each additional subproject With all the changes: 28 queries with 7 subprojects, 1 query per each additional subproject
1 parent b72343f commit e2a23c7

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,8 @@
22

33
import csv
44
import logging
5+
from urllib.parse import urlparse
6+
from readthedocs.core.resolver import resolve, resolve_path
57

68
from allauth.socialaccount.models import SocialAccount
79
from django.conf import settings
@@ -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 of each subproject,
476+
we only resolve 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)