-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Migrate Yearly Reading Goals to FastAPI #11816
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b6957b7
get_current_user works from fastapi
RayBB 06be5b2
migrate yearly reading goals to fastapi
RayBB 0e7532b
cleanup tests
RayBB 536bd35
add json to tests
RayBB 22b833d
fix tests
RayBB 8e2fbf4
format tests
RayBB 5f37738
remove integration test
RayBB 15e97a9
remove excessive docs
RayBB 59c7ed3
remove current that's not needed
RayBB File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| """ | ||
| FastAPI endpoints for yearly reading goals. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from datetime import datetime | ||
| from typing import Annotated | ||
|
|
||
| from fastapi import APIRouter, Depends, Form | ||
| from pydantic import BaseModel, Field, model_validator | ||
|
|
||
| from openlibrary.core.yearly_reading_goals import YearlyReadingGoals | ||
| from openlibrary.fastapi.auth import ( | ||
| AuthenticatedUser, | ||
| require_authenticated_user, | ||
| ) | ||
|
|
||
| router = APIRouter() | ||
|
|
||
| MAX_READING_GOAL = 10_000 | ||
|
|
||
|
|
||
| class ReadingGoalItem(BaseModel): | ||
| """A single reading goal entry.""" | ||
|
|
||
| year: int = Field(..., description="The year for this reading goal") | ||
| goal: int = Field(..., description="The target number of books to read") | ||
|
|
||
|
|
||
| class ReadingGoalsResponse(BaseModel): | ||
| """Response model for reading goals GET endpoint.""" | ||
|
|
||
| status: str = Field(default="ok", description="Response status") | ||
| goal: list[ReadingGoalItem] = Field( | ||
| default_factory=list, description="List of reading goals" | ||
| ) | ||
|
|
||
|
|
||
| class ReadingGoalUpdateResponse(BaseModel): | ||
| """Response model for reading goals POST endpoint.""" | ||
|
|
||
| status: str = Field(default="ok", description="Response status") | ||
|
|
||
|
|
||
| class ReadingGoalForm(BaseModel): | ||
| """Form data for creating or updating a reading goal. | ||
|
|
||
| Uses Pydantic model_validator to handle complex conditional validation | ||
| that depends on multiple fields (is_update flag affects goal validation). | ||
| """ | ||
|
|
||
| goal: int = Field( | ||
| default=0, | ||
| ge=0, | ||
| le=MAX_READING_GOAL, | ||
| description="Target number of books to read (0-10000). Use 0 with is_update to delete.", | ||
| ) | ||
| year: int | None = Field( | ||
| default=None, | ||
| description="Year for this reading goal. Defaults to current year for creates, required for updates.", | ||
| ) | ||
| is_update: str | None = Field( | ||
| default=None, | ||
| description="Set to any value to indicate this is an update operation (not create).", | ||
| ) | ||
|
|
||
| @model_validator(mode='after') | ||
| def validate_goal_logic(self) -> ReadingGoalForm: | ||
| """Validate goal based on whether this is an update or create operation. | ||
|
|
||
| - For creates: goal must be >= 1 | ||
| - For updates: goal must be >= 0, and year is required | ||
| """ | ||
| is_update = self.is_update is not None | ||
|
|
||
| if is_update: | ||
| if not self.year: | ||
| raise ValueError('Year required to update reading goals') | ||
| elif not self.goal: | ||
| raise ValueError('Reading goal must be a positive integer') | ||
|
|
||
| return self | ||
|
|
||
|
|
||
| @router.get("/reading-goal.json", response_model=ReadingGoalsResponse) | ||
| async def get_reading_goals_endpoint( | ||
| user: Annotated[AuthenticatedUser, Depends(require_authenticated_user)], | ||
| year: int | None = None, | ||
| ) -> ReadingGoalsResponse: | ||
| """Get reading goals for the authenticated user.""" | ||
| if year: | ||
| records = YearlyReadingGoals.select_by_username_and_year(user.username, year) | ||
| else: | ||
| records = YearlyReadingGoals.select_by_username(user.username) | ||
| goals = [ | ||
| ReadingGoalItem( | ||
| year=getattr(record, 'year', 0), | ||
| goal=getattr(record, 'target', 0), | ||
| ) | ||
| for record in records | ||
| ] | ||
|
|
||
| return ReadingGoalsResponse(status="ok", goal=goals) | ||
|
|
||
|
|
||
| @router.post("/reading-goal.json", response_model=ReadingGoalUpdateResponse) | ||
| async def update_reading_goal_endpoint( | ||
| user: Annotated[AuthenticatedUser, Depends(require_authenticated_user)], | ||
| form: Annotated[ReadingGoalForm, Form()], | ||
| ) -> ReadingGoalUpdateResponse: | ||
| """Create or update a reading goal for the authenticated user.""" | ||
| current_year = form.year or datetime.now().year | ||
| if form.is_update is not None: | ||
| # year is guaranteed to be not None here due to model_validator | ||
| assert form.year is not None | ||
| if form.goal == 0: | ||
| YearlyReadingGoals.delete_by_username_and_year(user.username, form.year) | ||
| else: | ||
| YearlyReadingGoals.update_target(user.username, form.year, form.goal) | ||
| else: | ||
| YearlyReadingGoals.create(user.username, current_year, form.goal) | ||
|
|
||
| return ReadingGoalUpdateResponse(status="ok") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This has caused the preference migration script to fail. Other scripts that call
get_current_userwill almost certainly also fail w/o additional set-up.