fix flask_restx error handler, update README.md, give up on fixing require_auth() on flask_restx

This commit is contained in:
Yusur 2025-07-01 14:48:58 +02:00
parent 96d25e0e85
commit f0a109983b
6 changed files with 19 additions and 9 deletions

View file

@ -2,7 +2,7 @@
## 0.3.4 ## 0.3.4
- Bug fixes in `.flask_sqlalchemy` and `.sqlalchemy``require_auth()` is unusable before this point! - Bug fixes in `.flask_restx` regarding error handling
- Fixed a bug in `.configparse` dealing with unset values from multiple sources - Fixed a bug in `.configparse` dealing with unset values from multiple sources
## 0.3.3 ## 0.3.3

View file

@ -2,7 +2,7 @@
Good morning, my brother! Welcome the SUOU (SIS Unified Object Underarmor), a library for the management of the storage of objects into a database. Good morning, my brother! Welcome the SUOU (SIS Unified Object Underarmor), a library for the management of the storage of objects into a database.
It provides utilities such as [SIQ](https://sakux.moe/protocols/siq.html), signing and generation of access tokens (on top of [ItsDangerous](https://github.com/pallets/itsdangerous)) and various utilities, including helpers for use in Flask and SQLAlchemy. It provides utilities such as [SIQ](https://yusur.moe/protocols/siq.html), signing and generation of access tokens (on top of [ItsDangerous](https://github.com/pallets/itsdangerous)) and various utilities, including helpers for use in Flask and SQLAlchemy.
**It is not an ORM** nor a replacement of it; it works along existing ORMs (currently only SQLAlchemy is supported lol). **It is not an ORM** nor a replacement of it; it works along existing ORMs (currently only SQLAlchemy is supported lol).

View file

@ -27,7 +27,7 @@ from .itertools import makelist, kwargs_prefix, ltuple, rtuple, additem
from .i18n import I18n, JsonI18n, TomlI18n from .i18n import I18n, JsonI18n, TomlI18n
from .snowflake import Snowflake, SnowflakeGen from .snowflake import Snowflake, SnowflakeGen
__version__ = "0.3.4.rc2" __version__ = "0.3.4"
__all__ = ( __all__ = (
'Siq', 'SiqCache', 'SiqType', 'SiqGen', 'StringCase', 'Siq', 'SiqCache', 'SiqType', 'SiqGen', 'StringCase',

View file

@ -227,7 +227,7 @@ def jsonencode(obj: dict, *, skipkeys: bool = True, separators: tuple[str, str]
''' '''
return json.dumps(obj, skipkeys=skipkeys, separators=separators, default=_json_default(default), **kwargs) return json.dumps(obj, skipkeys=skipkeys, separators=separators, default=_json_default(default), **kwargs)
jsondecode = deprecated('just use json.loads()')(json.loads) jsondecode: Callable[Any, dict] = deprecated('just use json.loads()')(json.loads)
def ssv_list(s: str, *, sep_chars = ',;') -> list[str]: def ssv_list(s: str, *, sep_chars = ',;') -> list[str]:
""" """

View file

@ -16,10 +16,10 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
from typing import Any, Mapping from typing import Any, Mapping
import warnings import warnings
from flask import current_app, make_response from flask import current_app, Response, make_response
from flask_restx import Api as _Api from flask_restx import Api as _Api
from .codecs import jsonencode from .codecs import jsondecode, jsonencode, want_bytes, want_str
def output_json(data, code, headers=None): def output_json(data, code, headers=None):
@ -54,13 +54,21 @@ class Api(_Api):
Notably, all JSON is whitespace-free and .message is remapped to .error Notably, all JSON is whitespace-free and .message is remapped to .error
""" """
def handle_error(self, e): def handle_error(self, e):
### XXX apparently this handle_error does not get called AT ALL. ### XXX in order for errors to get handled the correct way, import
print(e) ### suou.flask_restx.Api() NOT flask_restx.Api() !!!!
res = super().handle_error(e) res = super().handle_error(e)
print(res)
if isinstance(res, Mapping) and 'message' in res: if isinstance(res, Mapping) and 'message' in res:
res['error'] = res['message'] res['error'] = res['message']
del res['message'] del res['message']
elif isinstance(res, Response):
try:
body = want_str(res.response[0])
bodj = jsondecode(body)
if 'message' in bodj:
bodj['error'] = bodj.pop('message')
res.response = [want_bytes(jsonencode(bodj))]
except (IndexError, KeyError):
pass
return res return res
def __init__(self, *a, **ka): def __init__(self, *a, **ka):
super().__init__(*a, **ka) super().__init__(*a, **ka)

View file

@ -66,6 +66,8 @@ def require_auth(cls: type[DeclarativeBase], db: SQLAlchemy) -> Callable:
@auth_required(validators=[lambda x: x.is_administrator]) @auth_required(validators=[lambda x: x.is_administrator])
def super_secret_stuff(user): def super_secret_stuff(user):
pass pass
NOTE: require_auth() DOES NOT work with flask_restx.
""" """
def auth_required(**kwargs): def auth_required(**kwargs):
return require_auth_base(cls=cls, src=FlaskAuthSrc(db), **kwargs) return require_auth_base(cls=cls, src=FlaskAuthSrc(db), **kwargs)