Skip to content

Commit dde2bf0

Browse files
committed
add book list block to flex pages
1 parent cd14a00 commit dde2bf0

File tree

4 files changed

+473
-38
lines changed

4 files changed

+473
-38
lines changed

books/models.py

Lines changed: 44 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,47 @@ def cleanhtml(raw_html):
3232
return cleantext
3333

3434

35+
def get_book_data(book):
36+
"""Return the book data dict for a single Book instance, matching BookIndex.books property."""
37+
has_faculty_resources = BookFacultyResources.objects.filter(book_faculty_resource=book).exists()
38+
has_student_resources = BookStudentResources.objects.filter(book_student_resource=book).exists()
39+
try:
40+
return {
41+
'id': book.id,
42+
'cnx_id': book.cnx_id,
43+
'slug': f'books/{book.slug}',
44+
'book_state': book.book_state,
45+
'title': book.title,
46+
'subjects': book.subjects(),
47+
'is_ap': book.is_ap,
48+
'cover_url': book.cover_url,
49+
'cover_color': book.cover_color,
50+
'high_resolution_pdf_url': book.high_resolution_pdf_url,
51+
'low_resolution_pdf_url': book.low_resolution_pdf_url,
52+
'ibook_link': book.ibook_link,
53+
'ibook_link_volume_2': book.ibook_link_volume_2,
54+
'webview_link': book.webview_link,
55+
'webview_rex_link': book.webview_rex_link,
56+
'bookshare_link': book.bookshare_link,
57+
'kindle_link': book.kindle_link,
58+
'amazon_coming_soon': book.amazon_coming_soon,
59+
'amazon_link': book.amazon_link,
60+
'bookstore_coming_soon': book.bookstore_coming_soon,
61+
'comp_copy_available': book.comp_copy_available,
62+
'salesforce_abbreviation': book.salesforce_abbreviation,
63+
'salesforce_name': book.salesforce_name,
64+
'urls': book.book_urls(),
65+
'last_updated_pdf': book.last_updated_pdf,
66+
'has_faculty_resources': has_faculty_resources,
67+
'has_student_resources': has_student_resources,
68+
'assignable_book': book.assignable_book,
69+
'promote_tags': [snippet.value.name for snippet in book.promote_snippet],
70+
}
71+
except Exception as e:
72+
capture_exception(e)
73+
return None
74+
75+
3576
class VideoFacultyResource(models.Model):
3677
resource_heading = models.CharField(max_length=255)
3778
resource_description = RichTextField(blank=True, null=True)
@@ -1101,42 +1142,9 @@ def books(self):
11011142
books = Book.objects.live().filter(locale=self.locale).exclude(book_state='unlisted').order_by('title')
11021143
book_data = []
11031144
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)
1145+
data = get_book_data(book)
1146+
if data:
1147+
book_data.append(data)
11401148
return book_data
11411149

11421150
content_panels = Page.content_panels + [

pages/custom_blocks.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from api.serializers import ImageSerializer
99
from openstax.functions import build_image_url, build_document_url
1010
from wagtail.rich_text import expand_db_html
11+
from books.models import get_book_data
1112

1213

1314
class APIRichTextBlock(blocks.RichTextBlock):
@@ -144,6 +145,7 @@ class QuoteBlock(StructBlock):
144145
name = blocks.CharBlock(help_text="The name of the person or entity to attribute the quote to.")
145146
title = blocks.CharBlock(requred=False, help_text="Additional title or label about the quotee.")
146147

148+
147149
class DividerBlock(StructBlock):
148150
image = APIImageChooserBlock()
149151
config = blocks.StreamBlock([
@@ -323,3 +325,14 @@ def get_api_representation(self, value, context=None):
323325
'cover': build_document_url(value['cover'].url),
324326
'title': value['title'],
325327
}
328+
329+
class BookListBlock(blocks.StreamBlock):
330+
books = blocks.PageChooserBlock(page_type=['books.Book'], required=False)
331+
332+
class Meta:
333+
icon = 'placeholder'
334+
label = "Book List"
335+
336+
def get_api_representation(self, value, context=None):
337+
# value is a StreamValue of blocks, each with .value as a Book page
338+
return [get_book_data(book.value) for book in value if get_book_data(book.value)]

0 commit comments

Comments
 (0)