-
Notifications
You must be signed in to change notification settings - Fork 52
Support categories for events #137
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
yammesicka
merged 8 commits into
PythonFreeCourse:develop
from
ivarshav:feature/supportCategories2
Feb 3, 2021
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d69c480
Support categories for events
ivarshav e04a716
Merge remote-tracking branch 'upstream/develop' into feature/supportC…
ivarshav 176fbb5
PR comments
ivarshav 39ca099
Merge remote-tracking branch 'upstream/develop' into feature/supportC…
ivarshav 78b23f3
Merge remote-tracking branch 'upstream/develop' into feature/supportC…
ivarshav 81590a2
PR comments
ivarshav bd02602
Merge branch 'develop' into feature/supportCategories2
yammesicka 269652f
Remove redundant
ivarshav 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
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,80 @@ | ||
| from typing import Dict, List, Any | ||
|
|
||
| from fastapi import APIRouter, Depends, HTTPException, Request | ||
| from pydantic import BaseModel | ||
| from sqlalchemy.exc import IntegrityError, SQLAlchemyError | ||
| from sqlalchemy.orm import Session | ||
| from starlette import status | ||
| from starlette.datastructures import ImmutableMultiDict | ||
|
|
||
| from app.database.database import get_db | ||
| from app.database.models import Category | ||
|
|
||
| router = APIRouter( | ||
| prefix="/categories", | ||
| tags=["categories"], | ||
| ) | ||
|
|
||
|
|
||
| class CategoryModel(BaseModel): | ||
| name: str | ||
| color: str | ||
| user_id: int | ||
|
|
||
|
|
||
| # TODO(issue#29): get current user_id from session | ||
| @router.get("/") | ||
| def get_categories(request: Request, | ||
| db_session: Session = Depends(get_db)) -> List[Category]: | ||
| if validate_request_params(request.query_params): | ||
| return get_user_categories(db_session, **request.query_params) | ||
| else: | ||
| raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, | ||
| detail=f"Request {request.query_params} contains " | ||
| f"unallowed params.") | ||
|
|
||
|
|
||
| # TODO(issue#29): get current user_id from session | ||
| @router.post("/") | ||
| async def set_category(category: CategoryModel, | ||
| db_sess: Session = Depends(get_db)) -> Dict[str, Any]: | ||
| try: | ||
| cat = Category.create(db_sess, | ||
| name=category.name, | ||
| color=category.color, | ||
| user_id=category.user_id) | ||
| except IntegrityError: | ||
| db_sess.rollback() | ||
| raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, | ||
| detail=f"category is already exists for " | ||
| f"user {category.user_id}.") | ||
| else: | ||
| return {"category": cat.to_dict()} | ||
|
|
||
|
|
||
| def validate_request_params(query_params: ImmutableMultiDict) -> bool: | ||
| """ | ||
| request.query_params contains not more than user_id, name, color | ||
| and not less than user_id: | ||
| Intersection must contain at least user_id. | ||
| Union must not contain fields other than user_id, name, color. | ||
| """ | ||
| all_fields = set(CategoryModel.schema()["required"]) | ||
| request_params = set(query_params) | ||
| union_set = request_params.union(all_fields) | ||
| intersection_set = request_params.intersection(all_fields) | ||
| return union_set == all_fields and "user_id" in intersection_set | ||
|
|
||
|
|
||
| def get_user_categories(db_session: Session, | ||
| user_id: int, **params) -> List[Category]: | ||
| """ | ||
| Returns user's categories, filtered by params. | ||
| """ | ||
| try: | ||
| categories = db_session.query(Category).filter_by( | ||
| user_id=user_id).filter_by(**params).all() | ||
| except SQLAlchemyError: | ||
| return [] | ||
| else: | ||
| return categories |
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,13 @@ | ||
| import pytest | ||
| from sqlalchemy.orm import Session | ||
|
|
||
| from app.database.models import User, Category | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def category(session: Session, user: User) -> Category: | ||
| category = Category.create(session, name="Guitar Lesson", color="121212", | ||
| user_id=user.id) | ||
| yield category | ||
| session.delete(category) | ||
| session.commit() |
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.
Uh oh!
There was an error while loading. Please reload this page.