Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue 14 user management via api (#25)
- open up user data via the API (no creation and deletion though) - enable customisation of password reset emails - some updates to the asset API with learning from this PR, e.g. use webargs/marshmallow - show how we can use webargs/marshmallow for the older parts of the API, which will become a new PR Co-authored-by: F.N. Claessen <felix@seita.nl> Co-authored-by: Felix Claessen <30658763+Flix6x@users.noreply.github.com>
- Loading branch information
1 parent
ec3d6f6
commit c85927f
Showing
33 changed files
with
931 additions
and
236 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,47 @@ | ||
from flask import jsonify | ||
from webargs.multidictproxy import MultiDictProxy | ||
from webargs import ValidationError | ||
from webargs.flaskparser import parser | ||
|
||
""" | ||
Utils for argument parsing (we use webargs) | ||
""" | ||
|
||
|
||
class FMValidationError(Exception): | ||
""" Custom validation error class """ | ||
|
||
def __init__(self, messages): | ||
self.result = "Rejected" | ||
self.status = "UNPROCESSABLE_ENTITY" | ||
self.messages = messages | ||
|
||
|
||
def validation_error_handler(error): | ||
"""Handles errors during parsing. Aborts the current HTTP request and | ||
responds with a 422 error. | ||
""" | ||
status_code = 422 | ||
response = jsonify(error.messages) | ||
response.status_code = status_code | ||
return response | ||
|
||
|
||
@parser.error_handler | ||
def handle_error(error, req, schema, *, error_status_code, error_headers): | ||
"""Replacing webargs's error parser, so we can throw custom Exceptions.""" | ||
if error.__class__ == ValidationError: | ||
raise FMValidationError(messages=error.messages) | ||
raise error | ||
|
||
|
||
@parser.location_loader("args_and_json") | ||
def load_data(request, schema): | ||
""" | ||
We allow parameters to come from either GET args or POST JSON, | ||
as validators can be attached to either. | ||
""" | ||
newdata = request.args.copy() | ||
if request.mimetype == "application/json" and request.method == "POST": | ||
newdata.update(request.get_json()) | ||
return MultiDictProxy(newdata, schema) |
This file contains 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 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 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 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
Oops, something went wrong.