Skip to content

Commit

Permalink
feature: avoid having magic number for status codes
Browse files Browse the repository at this point in the history
Signed-off-by: F.N. Claessen <[email protected]>
  • Loading branch information
Flix6x committed Jun 5, 2024
1 parent 05064ae commit c7e58cb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
33 changes: 28 additions & 5 deletions flexmeasures/api/v3_0/assets.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

from datetime import datetime, timedelta
from http import HTTPStatus
import json

from flask import current_app
Expand Down Expand Up @@ -165,7 +166,12 @@ def post(self, asset_data: dict):

@route("/<id>", methods=["GET"])
@use_kwargs(
{"asset": AssetIdField(data_key="id", status_if_not_found=404)}, location="path"
{
"asset": AssetIdField(
data_key="id", status_if_not_found=HTTPStatus.NOT_FOUND
)
},
location="path",
)
@permission_required_for_context("read", ctx_arg_name="asset")
@as_json
Expand Down Expand Up @@ -203,7 +209,11 @@ def fetch_one(self, id, asset):
@route("/<id>", methods=["PATCH"])
@use_args(partial_asset_schema)
@use_kwargs(
{"db_asset": AssetIdField(data_key="id", status_if_not_found=404)},
{
"db_asset": AssetIdField(
data_key="id", status_if_not_found=HTTPStatus.NOT_FOUND
)
},
location="path",
)
@permission_required_for_context("update", ctx_arg_name="db_asset")
Expand Down Expand Up @@ -265,7 +275,12 @@ def patch(self, asset_data: dict, id: int, db_asset: GenericAsset):

@route("/<id>", methods=["DELETE"])
@use_kwargs(
{"asset": AssetIdField(data_key="id", status_if_not_found=404)}, location="path"
{
"asset": AssetIdField(
data_key="id", status_if_not_found=HTTPStatus.NOT_FOUND
)
},
location="path",
)
@permission_required_for_context("delete", ctx_arg_name="asset")
@as_json
Expand Down Expand Up @@ -293,7 +308,11 @@ def delete(self, id: int, asset: GenericAsset):

@route("/<id>/chart", strict_slashes=False) # strict on next version? see #1014
@use_kwargs(
{"asset": AssetIdField(data_key="id", status_if_not_found=404)},
{
"asset": AssetIdField(
data_key="id", status_if_not_found=HTTPStatus.NOT_FOUND
)
},
location="path",
)
@use_kwargs(
Expand Down Expand Up @@ -323,7 +342,11 @@ def get_chart(self, id: int, asset: GenericAsset, **kwargs):
"/<id>/chart_data", strict_slashes=False
) # strict on next version? see #1014
@use_kwargs(
{"asset": AssetIdField(data_key="id", status_if_not_found=404)},
{
"asset": AssetIdField(
data_key="id", status_if_not_found=HTTPStatus.NOT_FOUND
)
},
location="path",
)
@use_kwargs(
Expand Down
5 changes: 3 additions & 2 deletions flexmeasures/data/schemas/generic_assets.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import json
from http import HTTPStatus

from flask import abort
from marshmallow import validates, ValidationError, fields, validates_schema
Expand Down Expand Up @@ -151,7 +152,7 @@ class Meta:
class GenericAssetIdField(MarshmallowClickMixin, fields.Int):
"""Field that deserializes to a GenericAsset and serializes back to an integer."""

def __init__(self, status_if_not_found: int | None = None, *args, **kwargs):
def __init__(self, status_if_not_found: HTTPStatus | None = None, *args, **kwargs):
self.status_if_not_found = status_if_not_found
super().__init__(*args, **kwargs)

Expand All @@ -162,7 +163,7 @@ def _deserialize(self, value: int, attr, obj, **kwargs) -> GenericAsset:
).scalar_one_or_none()
if generic_asset is None:
message = f"No asset found with ID {value}."
if self.status_if_not_found == 404:
if self.status_if_not_found == HTTPStatus.NOT_FOUND:
raise abort(404, message)
else:
raise FMValidationError(message)
Expand Down

0 comments on commit c7e58cb

Please sign in to comment.