Created
October 13, 2018 16:01
-
-
Save aleksb86/b199a0eaf99475a3b8602ec0d7b94b9d to your computer and use it in GitHub Desktop.
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
def validate_request_data(fields, model_name): | |
def decorator_method(fn): | |
def decorated(*args, **kwargs): | |
# args[0] == GenericView Object | |
error_messages = [] | |
for field in fields: | |
if not args[0].request.data.get(field, None): | |
error_messages.append("Field '{}' is required for {}".format(field, model_name)) | |
return Response( | |
data={ | |
"message": ', '.join(error_messages) | |
}, | |
status=status.HTTP_400_BAD_REQUEST | |
) | |
return fn(*args, **kwargs) | |
return decorated | |
return decorator_method |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment