I am trying to do something similar. I avoided your error by checking if there is a request context:
if has_request_context():
return current_user.get_id() if current_user.is_authenticated else "Anonymous"
return "System"
My implementation is with flask-login but the point is to use has_request_context()
:
from flask import has_request_context