Skip to content

Commit c032b12

Browse files
committed
Use a cached property for the latest build
1 parent a3793d9 commit c032b12

File tree

3 files changed

+6
-3
lines changed

3 files changed

+6
-3
lines changed

readthedocs/core/templatetags/privacy_tags.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def get_public_projects(context, user):
2828
)
2929
# Filters the latest builds of projects.
3030
latest_build = Prefetch('builds', Build.objects.filter(
31-
pk__in=subquery), to_attr='_latest_build'
31+
pk__in=subquery), to_attr=Project.LATEST_BUILD_CACHE
3232
)
3333
# 'Exists()' checks if the project has any good builds.
3434
projects = Project.objects.for_user_and_viewer(

readthedocs/projects/models.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,9 @@ class Project(models.Model):
392392
objects = ProjectQuerySet.as_manager()
393393
all_objects = models.Manager()
394394

395+
# Property used for storing the latest build for a project when prefetching
396+
LATEST_BUILD_CACHE = '_latest_build'
397+
395398
class Meta:
396399
ordering = ('slug',)
397400
permissions = (
@@ -857,7 +860,7 @@ def get_latest_build(self, finished=True):
857860
"""
858861
# Check if there is `_latest_build` attribute in the Queryset.
859862
# Used for Database optimization.
860-
if hasattr(self, '_latest_build'):
863+
if hasattr(self, self.LATEST_BUILD_CACHE):
861864
if self._latest_build:
862865
return self._latest_build[0]
863866
return None

readthedocs/projects/querysets.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def dashboard(self, user=None):
9090
latest_build = Prefetch(
9191
'builds',
9292
Build.objects.filter(pk__in=subquery),
93-
to_attr='_latest_build',
93+
to_attr=self.model.LATEST_BUILD_CACHE,
9494
)
9595

9696
return projects.prefetch_related(latest_build)

0 commit comments

Comments
 (0)