-
Notifications
You must be signed in to change notification settings - Fork 1
Feature/flask smorest category #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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Empty file.
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,292 @@ | ||
| from flask.views import MethodView | ||
| from flask_jwt_extended import jwt_required | ||
| from flask_smorest import Blueprint, abort | ||
| from sqlalchemy import exists | ||
|
|
||
| from app import db | ||
| from app.models import ( | ||
| Category, | ||
| Product, | ||
| Subcategory, | ||
| category_subcategory, | ||
| subcategory_product, | ||
| ) | ||
| from app.schemas import ( | ||
| CategoriesOut, | ||
| CategoryIn, | ||
| CategoryOut, | ||
| PaginationArgs, | ||
| ProductsOut, | ||
| SubcategoriesOut, | ||
| ) | ||
|
|
||
| bp = Blueprint("category", __name__) | ||
|
|
||
|
|
||
| @bp.route("") | ||
| class CategoryCollection(MethodView): | ||
| init_every_request = False | ||
|
|
||
| @bp.response(200, CategoriesOut) | ||
| def get(self): | ||
| """ | ||
| Get All Categories | ||
| --- | ||
| tags: | ||
| - Category | ||
| description: Get all categories. | ||
| responses: | ||
| 200: | ||
| description: A list of categories. | ||
| """ | ||
| return {"categories": Category.query.all()} | ||
|
|
||
| @jwt_required() | ||
| @bp.arguments(CategoryIn) | ||
| @bp.response(201, CategoryOut) | ||
| def post(self, data): | ||
| """ | ||
| Create Category | ||
| --- | ||
| tags: | ||
| - Category | ||
| description: Create a new category. | ||
| security: | ||
| - access_token: [] | ||
| requestBody: | ||
| required: true | ||
| description: name - Name of the category <br> subcategories - Array of subcategory ids (optional) | ||
| content: | ||
| application/json: | ||
| schema: | ||
| type: object | ||
| required: | ||
| - name | ||
| properties: | ||
| name: | ||
| type: string | ||
| subcategories: | ||
| type: array | ||
| items: | ||
| type: integer | ||
| responses: | ||
| 201: | ||
| description: Category created successfully. | ||
| 400: | ||
| description: Invalid input. | ||
| 401: | ||
| description: Token expired, missing or invalid. | ||
| 500: | ||
| description: Error occurred. | ||
| """ | ||
| category = Category(name=data["name"]) | ||
|
|
||
| if sc_ids := data.get("subcategories"): | ||
| subcategories = Subcategory.query.filter(Subcategory.id.in_(sc_ids)).all() | ||
| if len(subcategories) != len(sc_ids): | ||
| abort(422, message="One or more subcategories not present") | ||
|
|
||
| category.subcategories = subcategories | ||
|
|
||
| db.session.add(category) | ||
| db.session.commit() | ||
|
|
||
| return category | ||
|
|
||
|
|
||
| @bp.route("/<int:id>") | ||
| class CategoryById(MethodView): | ||
| init_every_request = False | ||
|
|
||
| def _get(self, id): | ||
| return Category.query.get_or_404(id) | ||
|
|
||
| @bp.response(200, CategoryOut) | ||
| def get(self, id): | ||
| """ | ||
| Get Category | ||
| --- | ||
| tags: | ||
| - Category | ||
| description: Get a category by ID. | ||
| parameters: | ||
| - in: path | ||
| name: id | ||
| required: true | ||
| type: integer | ||
| description: Category ID | ||
| responses: | ||
| 200: | ||
| description: Category retrieved successfully. | ||
| 404: | ||
| description: Category not found. | ||
| """ | ||
| return self._get(id) | ||
|
|
||
| @jwt_required() | ||
| @bp.arguments(CategoryIn(partial=("name",))) | ||
| @bp.response(200, CategoryOut) | ||
| def put(self, data, id): | ||
| """ | ||
| Update Category | ||
| --- | ||
| tags: | ||
| - Category | ||
| description: Update an existing category. | ||
| security: | ||
| - access_token: [] | ||
| parameters: | ||
| - in: path | ||
| name: id | ||
| required: true | ||
| type: integer | ||
| description: Category ID | ||
| requestBody: | ||
| required: true | ||
| description: name - Name of the category (optional) <br> subcategories - Array of subcategory ids | ||
| content: | ||
| application/json: | ||
| schema: | ||
| type: object | ||
| properties: | ||
| name: | ||
| type: string | ||
| subcategories: | ||
| type: array | ||
| items: | ||
| type: integer | ||
| responses: | ||
| 201: | ||
| description: Category updated successfully. | ||
| 400: | ||
| description: Invalid input. | ||
| 404: | ||
| description: Category not found. | ||
| 500: | ||
| description: Error occurred. | ||
| """ | ||
| category = self._get(id) | ||
| if name := data.get("name"): | ||
| category.name = name | ||
|
|
||
| if sc_ids := data.get("subcategories"): | ||
| subcategories = Subcategory.query.filter(Subcategory.id.in_(sc_ids)).all() | ||
| if len(subcategories) != len(sc_ids): | ||
| abort(422, message="One or more subcategories not present") | ||
|
|
||
| category.subcategories.extend(subcategories) | ||
|
|
||
| db.session.commit() | ||
piyush-jaiswal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return category | ||
|
|
||
| @jwt_required() | ||
| @bp.response(204) | ||
| def delete(self, id): | ||
| """ | ||
| Delete Category | ||
| --- | ||
| tags: | ||
| - Category | ||
| description: Delete a category by ID. | ||
| security: | ||
| - access_token: [] | ||
| parameters: | ||
| - in: path | ||
| name: id | ||
| required: true | ||
| type: integer | ||
| description: Category ID | ||
| responses: | ||
| 200: | ||
| description: Category deleted successfully. | ||
| 404: | ||
| description: Category not found. | ||
| 500: | ||
| description: Error occurred. | ||
| """ | ||
| category = self._get(id) | ||
| db.session.delete(category) | ||
| db.session.commit() | ||
|
|
||
|
|
||
| @bp.route("/<int:id>/subcategories") | ||
| class CategorySubcategories(MethodView): | ||
| init_every_request = False | ||
|
|
||
| @bp.response(200, SubcategoriesOut) | ||
| def get(self, id): | ||
| """ | ||
| Get Subcategories within a Category. | ||
| --- | ||
| tags: | ||
| - Category | ||
| description: Get Subcategories within a Category. | ||
| parameters: | ||
| - in: path | ||
| name: id | ||
| required: true | ||
| type: integer | ||
| description: Category ID | ||
| responses: | ||
| 200: | ||
| description: Subcategories retrieved successfully. | ||
| 404: | ||
| description: Category not found. | ||
| 500: | ||
| description: Error occurred. | ||
| """ | ||
| category = Category.query.get_or_404(id) | ||
| return {"subcategories": category.subcategories} | ||
|
|
||
|
|
||
| @bp.route("/<int:id>/products") | ||
| class CategoryProducts(MethodView): | ||
| init_every_request = False | ||
| _PER_PAGE = 10 | ||
|
|
||
| @bp.arguments(PaginationArgs, location="query", as_kwargs=True) | ||
| @bp.response(200, ProductsOut) | ||
| def get(self, id, page): | ||
| """ | ||
| Get Products within a Category. | ||
| --- | ||
| tags: | ||
| - Category | ||
| description: Get Products for a Category. | ||
| parameters: | ||
| - in: path | ||
| name: id | ||
| required: true | ||
| type: integer | ||
| description: Category ID | ||
| - in: query | ||
| name: page | ||
| type: integer | ||
| default: 1 | ||
| description: Page number | ||
| responses: | ||
| 200: | ||
| description: Products retrieved successfully. | ||
| 404: | ||
| description: Category not found. | ||
| 500: | ||
| description: Error occurred. | ||
| """ | ||
| category_exists = db.session.query(exists().where(Category.id == id)).scalar() | ||
| if not category_exists: | ||
| abort(404) | ||
|
|
||
| products = ( | ||
| Product.query.join(subcategory_product) | ||
| .join( | ||
| category_subcategory, | ||
| onclause=subcategory_product.c.subcategory_id | ||
| == category_subcategory.c.subcategory_id, | ||
| ) | ||
| .filter(category_subcategory.c.category_id == id) | ||
| .distinct() | ||
| .order_by(Product.id.asc()) | ||
| .paginate(page=page, per_page=CategoryProducts._PER_PAGE, error_out=False) | ||
| ) | ||
|
|
||
| return {"products": products} | ||
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.