def get_or_404(model: t.Type[Y], object_id: t.Any) -> Y:
"""Get the specified object by primary key or raise an exception.
.. note::
``Y`` is bound to :py:class:`psef.models.Base`, so it should be a
SQLAlchemy model.
:param model: The object to get.
:param object_id: The primary key identifier for the given object.
:returns: The requested object.
:raises APIException: If no object with the given id could be found.
(OBJECT_ID_NOT_FOUND)
"""
obj: t.Optional[Y] = model.query.get(object_id)
if obj is None:
raise psef.errors.APIException(
f'The requested "{model.__name__}" was not found',
f'There is no "{model.__name__}" with primary key {object_id}',
psef.errors.APICodes.OBJECT_ID_NOT_FOUND, 404
)
return obj
评论列表
文章目录