I am not sure why following decorator[validate_request] doesn't work. What is correct way to write such validation decorator?
def validate_request(req_type):
if req_type is 'json' and not request.json:
abort(400)
def decorator(func):
@functools.wraps(func)
def wrapped_func(*args, **kwargs):
return func(*args, **kwargs)
return wrapped_func
return decorator
@app.route('/todo/api/v1.0/tasks/<int:task_id>', methods=['PUT'])
@validate_request('json')
@json
def update_task(task_id):
# task = filter(lambda t: t['id'] == task_id, tasks)
task = [task for task in tasks if task['id'] == task_id]
if len(task) == 0:
abort(404)
#update task
for field in ['title', 'description', 'done']:
task[0][field] = request.json.get(field, task[0][field])
Error :-
Traceback (most recent call last):
File "C:\AGR\Programming\LearningPython\FlaskLearning\flask_rest\app.py", line 156, in <module>
@validate_request('json')
File "C:\AGR\Programming\LearningPython\FlaskLearning\flask_rest\app.py", line 144, in validate_request
if req_type is 'json' and not request.json:
File "C:\Anaconda\lib\site-packages\werkzeug\local.py", line 338, in __getattr__
return getattr(self._get_current_object(), name)
File "C:\Anaconda\lib\site-packages\werkzeug\local.py", line 297, in _get_current_object
return self.__local()
File "C:\Anaconda\lib\site-packages\flask\globals.py", line 20, in _lookup_req_object
raise RuntimeError('working outside of request context')
RuntimeError: working outside of request context
How should this be done in a more idiomatic way???
It's kind of an old post, but I think that it may benefit from a little correction:
decorated_function
needs to returnf(*args, **kws)
Otherwise you will encounter
TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.
This is how your decorator should look like
and you will call it like this