Fix: course creation - prevent comparison error for books with None for shelf_section - #1331
Conversation
There was a problem hiding this comment.
Pull request overview
This PR aims to prevent a crash on the course creation page when sorting book shelf_section values that may be None, by introducing a fallback section label.
Changes:
- Default missing
shelf_sectionvalues to"Misc"when building thesectionslist for the course creation page. - Apply the same
sectionsbehavior in both the GET handler and the error-rendering path of the POST handler.
Comments suppressed due to low confidence (1)
bases/rsptx/admin_server_api/routers/instructor.py:1450
- Same issue in the error-handling path:
sectionsuses the "Misc" fallback, butcourse_listretainsshelf_section=None, so those books won’t render under any section in the template (it comparescourse.shelf_section == section). Normalizeshelf_sectionincourse_listbefore computingsections.
course_list = [c.dict() for c in course_list if c.for_classes]
sections = sorted({c["shelf_section"] or "Misc" for c in course_list})
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| course_list = [c.dict() for c in course_list if c.for_classes] | ||
| sections = sorted({c["shelf_section"] for c in course_list}) | ||
| sections = sorted({c["shelf_section"] or "Misc" for c in course_list}) |
|
Seems like providing a default for the shelf_section and/or making a pass through Also we ought to file issues against those books that don't provide a shelf section so the author can remedy that. I must have "fixed" all those in production a while ago. |
|
Added a default value to create_library_book that will make sure you can't add books with None for shelf_section. Pretty sure the offending path was using addbookauthor to manually add books. |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.
Suppressed comments (3)
bases/rsptx/admin_server_api/routers/instructor.py:1323
- Only
sectionsis normalized to replaceNonewith "Misc";course_listitems still haveshelf_section=None. In the template, books are rendered under a section only whencourse.shelf_section == section, so books withNonewill not show up under the "Misc" header (or any header), even though the crash is avoided.
course_list = [c.dict() for c in course_list if c.for_classes]
sections = sorted({c["shelf_section"] or "Misc" for c in course_list})
bases/rsptx/admin_server_api/routers/instructor.py:1450
- Same issue as the GET handler: normalizing only the
sectionslist prevents the TypeError but still leavescourse_listentries withshelf_section=None, so those books won't render under the "Misc" section increate_course.html.
course_list = [c.dict() for c in course_list if c.for_classes]
sections = sorted({c["shelf_section"] or "Misc" for c in course_list})
components/rsptx/db/crud/library.py:69
book_vals = {"shelf_section": "Misc", **vals}does not actually preventshelf_section=Nonebecause an explicitNoneinvalswill override the default. This means new/updated callers that passshelf_section: Nonecan still write NULL and reintroduce the course creation sorting/display issue.
book_vals = {"shelf_section": "Misc", **vals}
new_book = Library(**book_vals, basecourse=bookid)
|
Updated first commit to restrict Select All button to cards view. |
|
I think your last comment belongs on the other PR. Otherwise this looks good. |
I have a bunch of books in my dev library where shelf_section is None. That crashes the sort code when creating a new course. This fixes the course creation page to handle those.