@@ -32,6 +32,55 @@ def cleanhtml(raw_html):
32
32
return cleantext
33
33
34
34
35
+ def prefetch_book_resources (queryset ):
36
+ """Prefetch related faculty and student resources for a queryset of books."""
37
+ return queryset .prefetch_related (
38
+ models .Prefetch ('bookfacultyresources_set' , queryset = BookFacultyResources .objects .all (), to_attr = 'prefetched_faculty_resources' ),
39
+ models .Prefetch ('bookstudentresources_set' , queryset = BookStudentResources .objects .all (), to_attr = 'prefetched_student_resources' )
40
+ )
41
+
42
+ def get_book_data (book ):
43
+ has_faculty_resources = hasattr (book , 'prefetched_faculty_resources' ) and bool (book .prefetched_faculty_resources )
44
+ has_student_resources = hasattr (book , 'prefetched_student_resources' ) and bool (book .prefetched_student_resources )
45
+ try :
46
+ return {
47
+ 'id' : book .id ,
48
+ 'slug' : f'books/{ book .slug } ' ,
49
+ 'book_state' : book .book_state ,
50
+ 'title' : book .title ,
51
+ 'subjects' : book .subjects (),
52
+ 'subject_categories' : book .subject_categories ,
53
+ 'k12subject' : book .k12subjects (),
54
+ 'is_ap' : book .is_ap ,
55
+ 'is_hs' : 'High School' in book .subjects (),
56
+ 'cover_url' : book .cover_url ,
57
+ 'cover_color' : book .cover_color ,
58
+ 'high_resolution_pdf_url' : book .high_resolution_pdf_url ,
59
+ 'low_resolution_pdf_url' : book .low_resolution_pdf_url ,
60
+ 'ibook_link' : book .ibook_link ,
61
+ 'ibook_link_volume_2' : book .ibook_link_volume_2 ,
62
+ 'webview_link' : book .webview_link ,
63
+ 'webview_rex_link' : book .webview_rex_link ,
64
+ 'bookshare_link' : book .bookshare_link ,
65
+ 'kindle_link' : book .kindle_link ,
66
+ 'amazon_coming_soon' : book .amazon_coming_soon ,
67
+ 'amazon_link' : book .amazon_link ,
68
+ 'bookstore_coming_soon' : book .bookstore_coming_soon ,
69
+ 'comp_copy_available' : book .comp_copy_available ,
70
+ 'salesforce_abbreviation' : book .salesforce_abbreviation ,
71
+ 'salesforce_name' : book .salesforce_name ,
72
+ 'urls' : book .book_urls (),
73
+ 'last_updated_pdf' : book .last_updated_pdf ,
74
+ 'has_faculty_resources' : has_faculty_resources ,
75
+ 'has_student_resources' : has_student_resources ,
76
+ 'assignable_book' : book .assignable_book ,
77
+ 'promote_tags' : [snippet .value .name for snippet in book .promote_snippet ],
78
+ }
79
+ except Exception as e :
80
+ capture_exception (e )
81
+ return None
82
+
83
+
35
84
class VideoFacultyResource (models .Model ):
36
85
resource_heading = models .CharField (max_length = 255 )
37
86
resource_description = RichTextField (blank = True , null = True )
@@ -1101,42 +1150,9 @@ def books(self):
1101
1150
books = Book .objects .live ().filter (locale = self .locale ).exclude (book_state = 'unlisted' ).order_by ('title' )
1102
1151
book_data = []
1103
1152
for book in books :
1104
- has_faculty_resources = BookFacultyResources .objects .filter (book_faculty_resource = book ).exists ()
1105
- has_student_resources = BookStudentResources .objects .filter (book_student_resource = book ).exists ()
1106
- try :
1107
- book_data .append ({
1108
- 'id' : book .id ,
1109
- 'cnx_id' : book .cnx_id ,
1110
- 'slug' : 'books/{}' .format (book .slug ),
1111
- 'book_state' : book .book_state ,
1112
- 'title' : book .title ,
1113
- 'subjects' : book .subjects (),
1114
- 'is_ap' : book .is_ap ,
1115
- 'cover_url' : book .cover_url ,
1116
- 'cover_color' : book .cover_color ,
1117
- 'high_resolution_pdf_url' : book .high_resolution_pdf_url ,
1118
- 'low_resolution_pdf_url' : book .low_resolution_pdf_url ,
1119
- 'ibook_link' : book .ibook_link ,
1120
- 'ibook_link_volume_2' : book .ibook_link_volume_2 ,
1121
- 'webview_link' : book .webview_link ,
1122
- 'webview_rex_link' : book .webview_rex_link ,
1123
- 'bookshare_link' : book .bookshare_link ,
1124
- 'kindle_link' : book .kindle_link ,
1125
- 'amazon_coming_soon' : book .amazon_coming_soon ,
1126
- 'amazon_link' : book .amazon_link ,
1127
- 'bookstore_coming_soon' : book .bookstore_coming_soon ,
1128
- 'comp_copy_available' : book .comp_copy_available ,
1129
- 'salesforce_abbreviation' : book .salesforce_abbreviation ,
1130
- 'salesforce_name' : book .salesforce_name ,
1131
- 'urls' : book .book_urls (),
1132
- 'last_updated_pdf' : book .last_updated_pdf ,
1133
- 'has_faculty_resources' : has_faculty_resources ,
1134
- 'has_student_resources' : has_student_resources ,
1135
- 'assignable_book' : book .assignable_book ,
1136
- 'promote_tags' : [snippet .value .name for snippet in book .promote_snippet ],
1137
- })
1138
- except Exception as e :
1139
- capture_exception (e )
1153
+ data = get_book_data (book )
1154
+ if data :
1155
+ book_data .append (data )
1140
1156
return book_data
1141
1157
1142
1158
content_panels = Page .content_panels + [
0 commit comments