-
Notifications
You must be signed in to change notification settings - Fork 1
Feature/flask smorest product #23
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 3 commits
8e9ab0a
10aa701
c1acd3e
33caec4
f20532f
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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,311 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from flask.views import MethodView | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from flask_jwt_extended import jwt_required | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from flask_smorest import Blueprint, abort | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from psycopg2.errors import UniqueViolation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from sqlalchemy import UniqueConstraint | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from sqlalchemy.exc import IntegrityError | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from app import db | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from app.models import ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Product, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Subcategory, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| subcategory_product, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from app.schemas import ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| NameArgs, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| PaginationArgs, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ProductIn, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ProductOut, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ProductsOut, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| SubcategoriesOut, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| bp = Blueprint("product", __name__) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @bp.route("") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class ProductCollection(MethodView): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| init_every_request = False | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _PER_PAGE = 10 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @staticmethod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def _get_name_unique_constraint(): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| name_col = Product.__table__.c.name | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return next( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| con | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for con in Product.__table__.constraints | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if isinstance(con, UniqueConstraint) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| and len(con.columns) == 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| and con.columns.contains_column(name_col) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _NAME_UNIQUE_CONSTRAINT = _get_name_unique_constraint() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def _get_by_name(self, name): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return Product.query.filter(Product.name == name) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @bp.arguments(NameArgs, location="query", as_kwargs=True) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @bp.arguments(PaginationArgs, location="query", as_kwargs=True) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @bp.response(200, ProductsOut) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def get(self, name, page): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Get All Products | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| --- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tags: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - Product | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| description: Get all products. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| parameters: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - in: query | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| name: page | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type: integer | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| default: 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| description: Page number | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - in: query | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| name: name | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type: string | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| description: Name | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| responses: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| description: Product by name or a paginated list of all products. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if name is not None: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| products = self._get_by_name(name) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| products = Product.query.order_by(Product.id.asc()).paginate( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| page=page, per_page=ProductCollection._PER_PAGE, error_out=False | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return {"products": products} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @jwt_required() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @bp.arguments(ProductIn) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @bp.response(201, ProductOut) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def post(self, data): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Create Product | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| --- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tags: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - Product | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| description: Create a new product. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| security: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - access_token: [] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| requestBody: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| required: true | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| description: name - Name of the product <br> description - Description of the product (optional) <br> subcategories - Array of subcategory ids (optional) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| content: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| application/json: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| schema: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type: object | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| required: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - name | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| properties: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| name: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type: string | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| description: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type: string | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| subcategories: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type: array | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| items: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type: integer | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| responses: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| description: Product created successfully. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 400: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| description: Invalid input. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 401: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| description: Token expired, missing or invalid. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 500: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| description: Error occurred. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| product = Product(name=data["name"], description=data.get("description")) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| product.subcategories = subcategories | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| db.session.add(product) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| db.session.commit() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| except IntegrityError as ie: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| db.session.rollback() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| isinstance(ie.orig, UniqueViolation) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| and ie.orig.diag.constraint_name | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| == ProductCollection._NAME_UNIQUE_CONSTRAINT.name | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| abort(409, message="Product with this name already exists") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| raise | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return product | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @bp.route("/<int:id>") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class ProductById(MethodView): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| init_every_request = False | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def _get(self, id): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return Product.query.get_or_404(id) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @bp.response(200, ProductOut) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def get(self, id): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Get Product | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| --- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tags: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - Product | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| description: Get a product by ID. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| parameters: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - in: path | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| name: id | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| required: true | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type: integer | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| description: Product ID | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| responses: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| description: Product retrieved successfully. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 404: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| description: Product not found. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return self._get(id) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @jwt_required() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @bp.arguments(ProductIn(partial=("name",))) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @bp.response(200, ProductOut) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def put(self, data, id): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Update Product | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| --- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tags: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - Product | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| description: Update an existing product. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| security: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - access_token: [] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| consumes: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - application/json | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| parameters: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - in: path | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| name: id | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| required: true | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type: integer | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| description: Product ID | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| requestBody: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| required: true | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| description: name - Name of the product (optional) <br> description = Description of the product (optional) <br> subcategories - Array of subcategory ids (optional) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| content: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| application/json: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| schema: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type: object | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| properties: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| name: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type: string | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| description: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type: string | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| subcategories: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type: array | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| items: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type: integer | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| responses: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| description: Product updated successfully. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 400: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| description: Invalid input. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 404: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| description: Product not found. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 500: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| description: Error occurred. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| product = self._get(id) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if name := data.get("name"): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| product.name = name | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if description := data.get("description"): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| product.description = description | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| with db.session.no_autoflush: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| product.subcategories.extend(subcategories) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
221
to
234
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. Let updates apply even when values are empty
- if name := data.get("name"):
- product.name = name
- if description := data.get("description"):
- product.description = description
+ if "name" in data:
+ product.name = data["name"]
+ if "description" in data:
+ product.description = data["description"](Validation already forbids empty names; description should accept empty/None.) 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| db.session.commit() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| except IntegrityError as ie: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| db.session.rollback() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| isinstance(ie.orig, UniqueViolation) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| and ie.orig.diag.constraint_name | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| == ProductCollection._NAME_UNIQUE_CONSTRAINT.name | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| abort(409, message="Product with this name already exists") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| isinstance(ie.orig, UniqueViolation) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| and ie.orig.diag.constraint_name == subcategory_product.primary_key.name | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| abort(409, message="Product and subcategory already linked") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| raise | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return product | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @jwt_required() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @bp.response(204) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def delete(self, id): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Delete Product | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| --- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tags: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - Product | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| description: Delete a product by ID. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| security: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - access_token: [] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| parameters: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - in: path | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| name: id | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| required: true | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type: integer | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| description: Product ID | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| responses: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| description: Product deleted successfully. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 404: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| description: Product not found. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 500: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| description: Error occurred. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| product = self._get(id) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| db.session.delete(product) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| db.session.commit() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @bp.route("/<int:id>/subcategories") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class ProductSubcategories(MethodView): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| init_every_request = False | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @bp.response(200, SubcategoriesOut) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def get(self, id): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Get Subcategories related to a Product. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| --- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tags: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - Product | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| description: Get Subcategories related to a Product. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| parameters: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - in: path | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| name: id | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| required: true | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type: integer | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| description: Product ID | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| responses: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| description: Subcategories retrieved successfully. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 404: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| description: Product not found. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 500: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| description: Error occurred. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| product = Product.query.get_or_404(id) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return {"subcategories": product.subcategories} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
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.
Return 409 on SQLite unique violations instead of leaking 500s
Both
postandputhandlers only translate unique violations whenie.origis a psycopg2UniqueViolation. Under the in-memory SQLite DB used in tests (and commonly in local runs),ie.origis asqlite3.IntegrityError, so these blocks fall through toraise. That bubbles the DB error, yielding a 500 instead of the intended 409 and forcing the tests topytest.raises(IntegrityError). Please broaden the handling to cover SQLite by inspectingie.orig(e.g.,isinstance(..., sqlite3.IntegrityError)plus message matching) before aborting with the appropriate 409 message for duplicate product names or duplicate product-subcategory links.Also applies to: 236-250
🤖 Prompt for AI Agents