-
Notifications
You must be signed in to change notification settings - Fork 0
Adjust /course/filter/values/{filter} endpoint to return names on top of just codes
#17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -220,14 +220,20 @@ def search_course( | |||||||||
|
|
||||||||||
|
|
||||||||||
| @router.get("/filter/values/{filter}") | ||||||||||
| def get_filter_values(session: SessionDep, filter: CourseFilter) -> list[str]: | ||||||||||
| column = None | ||||||||||
| def get_filter_values(session: SessionDep, filter: CourseFilter) -> dict[str, str]: | ||||||||||
| code_col, title_col = None, None | ||||||||||
| if filter is CourseFilter.subjects: | ||||||||||
| column = Subject.subj_code | ||||||||||
| code_col = Subject.subj_code | ||||||||||
| title_col = Subject.title | ||||||||||
| elif filter is CourseFilter.attributes: | ||||||||||
| column = Attribute.attr_code | ||||||||||
| code_col = Attribute.attr_code | ||||||||||
| title_col = Attribute.title | ||||||||||
| elif filter is CourseFilter.semesters: | ||||||||||
| column = Course_Offering.semester | ||||||||||
| result_scalars = ( | ||||||||||
| session.execute(select(Course_Offering.semester).distinct()).scalars().all() | ||||||||||
| ) | ||||||||||
| return {sem: sem.capitalize() for sem in result_scalars} | ||||||||||
| else: | ||||||||||
| return None | ||||||||||
| return session.execute(select(column).distinct()).scalars().all() | ||||||||||
| return {} | ||||||||||
| result_mappings = session.execute(select(code_col, title_col)).mappings().all() | ||||||||||
|
||||||||||
| result_mappings = session.execute(select(code_col, title_col)).mappings().all() | |
| result_mappings = session.execute(select(code_col, title_col).distinct()).mappings().all() |
Copilot
AI
Nov 18, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using SQLAlchemy column objects (code_col, title_col) as dictionary keys in row[code_col] may not work as expected with .mappings(). The mapping keys are typically the column names as strings, not the column objects themselves. Consider using the column names as strings or accessing the row values by index:
# Option 1: Access by column name strings
result_mappings = session.execute(select(code_col, title_col)).mappings().all()
code_key = code_col.key
title_key = title_col.key
return {row[code_key]: row[title_key] for row in result_mappings}
# Option 2: Use tuples and access by index
result_tuples = session.execute(select(code_col, title_col)).all()
return {row[0]: row[1] for row in result_tuples}| return {row[code_col]: row[title_col] for row in result_mappings} | |
| code_key = code_col.key | |
| title_key = title_col.key | |
| return {row[code_key]: row[title_key] for row in result_mappings} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
sem.capitalize()on semester codes may not produce meaningful names. For example, if semester codes are formatted like "202401" or "spring2024",capitalize()would return "202401" or "Spring2024" respectively. Consider implementing proper semester name mapping logic or documenting the expected semester code format. Example: