File tree 2 files changed +12
-17
lines changed
2 files changed +12
-17
lines changed Original file line number Diff line number Diff line change @@ -20,21 +20,11 @@ def is_admin(user, project):
20
20
21
21
@register .simple_tag (takes_context = True )
22
22
def get_public_projects (context , user ):
23
- # Creates a Subquery object which returns the latest builds 'id'.
24
- # Used for optimization purpose.
25
- subquery = Subquery (
26
- Build .objects .filter (
27
- project = OuterRef ('project_id' )).values_list ('id' , flat = True )[:1 ]
28
- )
29
- # Filters the latest builds of projects.
30
- latest_build = Prefetch ('builds' , Build .objects .filter (
31
- pk__in = subquery ), to_attr = Project .LATEST_BUILD_CACHE
32
- )
33
23
# 'Exists()' checks if the project has any good builds.
34
24
projects = Project .objects .for_user_and_viewer (
35
25
user = user ,
36
26
viewer = context ['request' ].user ,
37
- ).prefetch_related ( 'users' , latest_build ).annotate (
27
+ ).prefetch_latest_build ( ).annotate (
38
28
_good_build = Exists (
39
29
Build .objects .filter (success = True , project = OuterRef ('pk' )))
40
30
)
Original file line number Diff line number Diff line change @@ -75,14 +75,14 @@ def is_active(self, project):
75
75
76
76
return True
77
77
78
- # Aliases
78
+ def prefetch_latest_build (self ):
79
+ """
80
+ For a given queryset of projects, prefetch the latest build for each project
79
81
80
- def dashboard ( self , user = None ):
81
- """Get the projects for this user including the latest build"""
82
+ This should come after any filtering.
83
+ """
82
84
from readthedocs .builds .models import Build
83
85
84
- projects = self .for_admin_user (user )
85
-
86
86
# Prefetch the latest build for each project.
87
87
subquery = Subquery (
88
88
Build .objects .filter (project = OuterRef ('project_id' )).values_list ('id' , flat = True )[:1 ]
@@ -92,8 +92,13 @@ def dashboard(self, user=None):
92
92
Build .objects .filter (pk__in = subquery ),
93
93
to_attr = self .model .LATEST_BUILD_CACHE ,
94
94
)
95
+ return self .prefetch_related (latest_build )
96
+
97
+ # Aliases
95
98
96
- return projects .prefetch_related (latest_build )
99
+ def dashboard (self , user = None ):
100
+ """Get the projects for this user including the latest build"""
101
+ return self .for_admin_user (user ).prefetch_latest_build ()
97
102
98
103
def api (self , user = None , detail = True ):
99
104
if detail :
You can’t perform that action at this time.
0 commit comments