-
Notifications
You must be signed in to change notification settings - Fork 1
Sprint3/frontend backend api integration 3.2 #12
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
10 commits
Select commit
Hold shift + click to select a range
7363a0d
add connection to study pack
1000jae f1badc6
frontend api test
1000jae 51254c5
update tests
1000jae 92cdd5b
update page.tsx
1000jae 2decf76
remove duplicate backend
1000jae ca5a93e
update comments
1000jae e700b92
add test documentation
1000jae b428fa5
fix lib/api.ts error
1000jae 55e5232
fix backend and frontend
1000jae 641ba13
update backend
1000jae 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
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -21,6 +21,11 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| allow_headers=["*"], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def check_empty_text(v: str) -> str: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if not v or not v.strip(): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| raise ValueError("text must not be empty") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return v | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # ============================================ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # REQUEST/RESPONSE SCHEMAS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -37,11 +42,28 @@ class GenerateRequest(BaseModel): | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @field_validator("text") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @classmethod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def text_must_not_be_empty(cls, v: str) -> str: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if not v or not v.strip(): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| raise ValueError("text must not be empty") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return check_empty_text(v) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class StudyPackRequest(GenerateRequest): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Request body for POST /generate-study-pack | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - text: The user's study notes to process | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @field_validator("text") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @classmethod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def text_length_constraint(cls, v: str) -> str: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| v = check_empty_text(v) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| stripped = v.strip() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # validate length | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if len(stripped) < 10: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| raise ValueError(f"text must not be less than 10 characters") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if len(stripped) > 10000: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| raise ValueError("text must not be more than 10000 characters") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return v | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+48
to
+63
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Child validator overrides parent's empty check In Pydantic v2,
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class QuizQuestion(BaseModel): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """A single quiz question with multiple choice options""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| question: str | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -59,32 +81,6 @@ class GenerateResponse(BaseModel): | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| quiz: list[QuizQuestion] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # ============================================ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # STUDY PACK REQUESTS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # ============================================ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class StudyPackRequest(BaseModel): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Request body for POST /generate-study-pack | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - text: The user's study notes to process | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| text: str | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @field_validator('text') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @classmethod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def validate_text(cls, v: str) -> str: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # do not include whitespace | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| stripped = v.strip() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # validate emptiness | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if not v or not stripped: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| raise ValueError("text must not be empty") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # validate length | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if len(stripped) < 10: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| raise ValueError(f"text must not be less than 10 characters") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if len(stripped) > 10000: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| raise ValueError("text must not be more than 10000 characters") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return v | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # ============================================ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # STUDY PACK HELPER FUNCTIONS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -280,7 +276,7 @@ async def generate_study_pack(request: StudyPackRequest): | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if response is None: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| raise HTTPException( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| status_code=500, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| detail="Gemini API is unavailable. Please ensure GEMINI_API_KEY is set in .env file." | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| detail="Gemini unavailable. Please try again." | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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
Oops, something went wrong.
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.
Length constraints now apply to
/api/v1/generatetooBefore this PR,
GenerateRequestonly validated that text was non-empty. Addingtext_length_constrainthere means the/api/v1/generateendpoint now also enforces min 10 / max 10,000 character limits — a breaking change for any existing callers that send short notes (< 10 chars) or very long notes (> 10,000 chars).If this is intentional, the docstring for
/api/v1/generate(line 141) should document the new constraints. If unintentional, consider keeping the length validator only in a subclass or applying it selectively to the/generate-study-packroute.