I have some code in my apps.py's ready()
method that loads some Oauth providers using the authlib module. Basically I am doing this (apps.py):
from django.apps import AppConfig
from authlib.integrations.django_client import OAuth
from django.core.cache import caches
class ApiConfig(AppConfig):
name = 'api'
def ready(self):
import api.receivers
defaultcache = caches['default']
from .modules.oauth import fetch_token
from .models import OauthProvider, OAuth2Token
oauth = OAuth(fetch_token=fetch_token)
print('settings oauthproviders')
allproviders = OauthProvider.objects.all()
for provider in allproviders:
oauth.register(
name=provider.name,
client_id=provider.client_id,
client_secret=provider.client_secret_decrypted,
access_token_url=provider.access_token_url,
access_token_params=provider.access_token_params,
authorize_url=provider.authorize_url,
authorize_params=provider.authorize_params,
api_base_url=provider.api_base_url,
client_kwargs=eval(provider.client_kwargs),
)
print(OAuth2Token.objects.get(name='enelogic'))
defaultcache.set('oauth', oauth)
#defaultcache.get('oauth') # <--- this line gives error
Error:
Fatal Python error: Cannot recover from stack overflow.
Python runtime state: initialized
Thread 0x00001d4c (most recent call first):
File "c:\program files (x86)\python38-32\lib\threading.py", line 306 in wait
File "c:\program files (x86)\python38-32\lib\threading.py", line 558 in wait
File "c:\Users\ericj\.vscode\extensions\ms-python.python-2020.9.112786\pythonFiles\lib\python\debugpy\_vendored\pydevd\pydevd.py", line 230 in _on_run
File "c:\Users\ericj\.vscode\extensions\ms-python.python-2020.9.112786\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_daemon_thread.py", line 46 in run
File "c:\program files (x86)\python38-32\lib\threading.py", line 932 in _bootstrap_inner
File "c:\program files (x86)\python38-32\lib\threading.py", line 890 in _bootstrap
File "c:\Users\ericj\.vscode\extensions\ms-python.python-2020.9.112786\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydev_bundle\pydev_monkey.py", line 823 in __call__
Thread 0x00002b80 (most recent call first):
File "c:\program files (x86)\python38-32\lib\threading.py", line 306 in wait
File "c:\program files (x86)\python38-32\lib\threading.py", line 558 in wait
File "c:\Users\ericj\.vscode\extensions\ms-python.python-2020.9.112786\pythonFiles\lib\python\debugpy\_vendored\pydevd\pydevd.py", line 184 in _on_run
File "c:\Users\ericj\.vscode\extensions\ms-python.python-2020.9.112786\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_daemon_thread.py", line 46 in run
File "c:\program files (x86)\python38-32\lib\threading.py", line 932 in _bootstrap_inner
File "c:\program files (x86)\python38-32\lib\threading.py", line 890 in _bootstrap
File "c:\Users\ericj\.vscode\extensions\ms-python.python-2020.9.112786\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydev_bundle\pydev_monkey.py", line 823 in __call__
Thread 0x0000609c (most recent call first):
File "c:\program files (x86)\python38-32\lib\threading.py", line 306 in wait
File "c:\program files (x86)\python38-32\lib\threading.py", line 558 in wait
File "c:\Users\ericj\.vscode\extensions\ms-python.python-2020.9.112786\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_timeout.py", line 43 in _on_run
File "c:\Users\ericj\.vscode\extensions\ms-python.python-2020.9.112786\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_daemon_thread.py", line 46 in run
File "c:\program files (x86)\python38-32\lib\threading.py", line 932 in _bootstrap_inner
File "c:\program files (x86)\python38-32\lib\threading.py", line 890 in _bootstrap
Thread 0x00000ad4 (most recent call first):
File "c:\Users\ericj\.vscode\extensions\ms-python.python-2020.9.112786\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_comm.py", line 209 in _read_line
File "c:\Users\ericj\.vscode\extensions\ms-python.python-2020.9.112786\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_comm.py", line 227 in _on_run
File "c:\Users\ericj\.vscode\extensions\ms-python.python-2020.9.112786\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_daemon_thread.py", line 46 in run
File "c:\program files (x86)\python38-32\lib\threading.py", line 932 in _bootstrap_inner
File "c:\program files (x86)\python38-32\lib\threading.py", line 890 in _bootstrap
Thread 0x00005cf8 (most recent call first):
File "c:\Users\ericj\.vscode\extensions\ms-python.python-2020.9.112786\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_net_command.py", line 111 in send
File "c:\Users\ericj\.vscode\extensions\ms-python.python-2020.9.112786\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_comm.py", line 370 in _on_run
File "c:\Users\ericj\.vscode\extensions\ms-python.python-2020.9.112786\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_daemon_thread.py", line 46 in run
File "c:\program files (x86)\python38-32\lib\threading.py", line 932 in _bootstrap_inner
File "c:\program files (x86)\python38-32\lib\threading.py", line 890 in _bootstrap
Current thread 0x00005924 (most recent call first):
File "c:\program files (x86)\python38-32\lib\traceback.py", line 476 in __init__
File "c:\program files (x86)\python38-32\lib\traceback.py", line 493 in __init__
File "c:\program files (x86)\python38-32\lib\traceback.py", line 493 in __init__
File "c:\program files (x86)\python38-32\lib\traceback.py", line 493 in __init__
File "c:\program files (x86)\python38-32\lib\traceback.py", line 493 in __init__
File "c:\program files (x86)\python38-32\lib\traceback.py", line 493 in __init__
File "c:\program files (x86)\python38-32\lib\traceback.py", line 493 in __init__
File "c:\program files (x86)\python38-32\lib\traceback.py", line 493 in __init__
File "c:\program files (x86)\python38-32\lib\traceback.py", line 493 in __init__
File "c:\program files (x86)\python38-32\lib\traceback.py", line 493 in __init__
File "c:\program files (x86)\python38-32\lib\traceback.py", line 493 in __init__
File "c:\program files (x86)\python38-32\lib\traceback.py", line 493 in __init__
File "c:\program files (x86)\python38-32\lib\traceback.py", line 493 in __init__
File "c:\program files (x86)\python38-32\lib\traceback.py", line 493 in __init__
File "c:\program files (x86)\python38-32\lib\traceback.py", line 493 in __init__
File "c:\program files (x86)\python38-32\lib\traceback.py", line 493 in __init__
File "c:\program files (x86)\python38-32\lib\traceback.py", line 493 in __init__
File "c:\program files (x86)\python38-32\lib\traceback.py", line 493 in __init__
File "c:\program files (x86)\python38-32\lib\traceback.py", line 493 in __init__
File "c:\program files (x86)\python38-32\lib\traceback.py", line 493 in __init__
File "c:\program files (x86)\python38-32\lib\traceback.py", line 493 in __init__
File "c:\program files (x86)\python38-32\lib\traceback.py", line 493 in __init__
File "c:\program files (x86)\python38-32\lib\traceback.py", line 493 in __init__
File "c:\program files (x86)\python38-32\lib\traceback.py", line 103 in print_exception
File "c:\program files (x86)\python38-32\lib\traceback.py", line 163 in print_exc
File "c:\Users\ericj\.vscode\extensions\ms-python.python-2020.9.112786\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydev_bundle\pydev_log.py", line 130 in _pydevd_log_exception
File "c:\Users\ericj\.vscode\extensions\ms-python.python-2020.9.112786\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydev_bundle\pydev_log.py", line 160 in exception
File "c:\Users\ericj\.vscode\extensions\ms-python.python-2020.9.112786\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_trace_dispatch_regular.py", line 462 in __call__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 101 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
This registers some oauth credentials (info) that can be used using the oauth
variable (for example oauth.get_authorization_url()
which gets me the authorize URL for that Oauth Provider.
Now, I have a lot of different files (I split my views.py into multiple files). How can I access this oauth variable in those files? I have tried with django caches, but then I get a weird overflow error when I try to access the variable.
How would you recommend?
The oauth class is working fine locally in the file, I just need to transfer it to some other file
Why not extracting this in a separate python file (but still inside of your django app).
And now you can use it in your views by simple import
Yes, there are some caveats it will be a global variable (so don't try to change, I don't think you'll need to) and don't try to import the file in your
models.py
since this should be imported after the app isready()
.