Skip to content
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

Apply decorator to validate request on route functions. #5947

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions backend/models/dtos/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from functools import wraps
from flask import request
from schematics.exceptions import DataError

from werkzeug.exceptions import BadRequest as WerkzeugBadRequest

from backend.exceptions import BadRequest

Expand Down Expand Up @@ -32,20 +32,36 @@ def wrapper(*args, **kwargs):

try:
dto = dto_class()
try:
body = request.json if request.is_json else {}
except (
WerkzeugBadRequest
): # If request body does not contain valid JSON then BadRequest is raised by Flask
body = {}

# Set attribute values from request body, query parameters, and path parameters
for attr in dto.__class__._fields:
if request.is_json and attr in request.json:
setattr(dto, attr, request.json[attr])
elif attr in request.args:
setattr(dto, attr, request.args.get(attr))
elif attr in kwargs:
setattr(dto, attr, kwargs[attr])
# Get serialized name of attr if exists otherwise use attr name
field = dto.__class__._fields[attr]
attr_name = field.serialized_name if field.serialized_name else attr

# Set attribute value from request body, query parameters, or path parameters
if attr_name in body:
setattr(dto, attr, body[attr_name])
elif attr_name in request.args:
setattr(dto, attr, request.args.get(attr_name))
elif attr_name in kwargs:
setattr(dto, attr, kwargs[attr_name])

# Set authenticated user id if user_id is a field in the DTO
if "user_id" in dto.__class__._fields:
dto.user_id = token_auth.current_user()

# Get accepted language from request header
if "preferred_locale" in dto.__class__._fields:
dto.preferred_locale = request.environ.get(
"HTTP_ACCEPT_LANGUAGE", "en"
)

dto.validate()
request.validated_dto = (
dto # Set validated DTO on request object for use in view function
Expand Down