API-Manager/apimanager/base/context_processors.py

116 lines
4.0 KiB
Python
Raw Normal View History

2016-11-09 13:54:47 +00:00
# -*- coding: utf-8 -*-
2016-11-21 13:55:35 +00:00
"""
Context processors for base app
"""
2016-11-09 13:54:47 +00:00
from django.conf import settings
from django.contrib import messages
2016-11-09 13:54:47 +00:00
from obp.api import API, APIError, LOGGER
2020-10-21 12:55:33 +00:00
from django.core.cache import cache
2016-11-09 13:54:47 +00:00
2023-04-06 13:02:49 +00:00
USER_CURRENT = "/users/current"
2021-06-30 08:55:27 +00:00
2023-12-01 15:44:52 +00:00
def api_version_processor(request):
"""Returns the configured API_VERSION"""
return {'API_VERSION': settings.API_VERSION['v500']}
2016-11-09 13:54:47 +00:00
2016-11-21 13:55:35 +00:00
2021-06-30 08:55:27 +00:00
def portal_page(request):
"""Returns the configured API_PORTAL"""
if settings.API_PORTAL is None:
return {'API_PORTAL': settings.API_HOST}
else:
return {'API_PORTAL': settings.API_PORTAL}
def logo_url(request):
"""Returns the configured LOGO_URL"""
return {'logo_url': settings.LOGO_URL}
2021-07-30 10:53:17 +00:00
def override_css_url(request):
"""Returns the configured OVERRIDE_CSS_URL"""
return {'override_css_url': settings.OVERRIDE_CSS_URL}
2016-11-09 13:54:47 +00:00
def api_username(request):
"""Returns the API username/email of the logged-in user"""
nametodisplay = 'not authenticated'
get_current_user_api_url = USER_CURRENT
2023-04-18 20:30:36 +00:00
#Here we can not get the user from obp-api side, so we use the django auth user id here.
2020-10-21 12:55:33 +00:00
cache_key_django_user_id = request.session._session.get('_auth_user_id')
cache_key = '{},{},{}'.format('api_username',get_current_user_api_url, cache_key_django_user_id)
apicaches=None
try:
apicaches=cache.get(cache_key)
except Exception as err:
apicaches=None
2022-12-27 08:15:01 +00:00
messages.error(request, err)
2020-10-21 12:55:33 +00:00
if not apicaches is None:
return apicaches
else:
if request.user.is_authenticated:
nametodisplay = authenticated_name(request, get_current_user_api_url)
apicaches=cache.set(cache_key, {'API_USERNAME': nametodisplay})
LOGGER.warning('The cache setting api_user_name is: {}'.format(nametodisplay))
LOGGER.warning('The cache setting key is: {}'.format(cache_key))
return {'API_USERNAME': nametodisplay}
2017-10-30 14:05:31 +00:00
def authenticated_name(request, get_current_user_api_url):
try:
api = API(request.session.get('obp'))
data = api.get(get_current_user_api_url)
username = data['username']
email = data['email']
provider = data['provider']
if "google" in provider:
nametodisplay = email
elif "yahoo" in provider:
nametodisplay = email
elif "microsoft" in provider:
nametodisplay = email
else:
nametodisplay = username
2023-01-03 14:54:38 +00:00
return nametodisplay
except APIError as err:
messages.error(request, err)
except Exception as err:
messages.error(request, err)
2017-10-30 14:05:31 +00:00
def api_user_id(request):
"""Returns the API user id of the logged-in user"""
user_id = 'not authenticated'
get_current_user_api_url = USER_CURRENT
2020-10-21 12:55:33 +00:00
#Here we can not get the user from obp-api side, so we use the django auth user id here.
cache_key_django_user_id = request.session._session.get('_auth_user_id')
cache_key = '{},{},{}'.format('api_user_id',get_current_user_api_url, cache_key_django_user_id)
apicaches=None
try:
apicaches=cache.get(cache_key)
except Exception as err:
apicaches=None
if not apicaches is None:
return apicaches
else:
if request.user.is_authenticated:
try:
api = API(request.session.get('obp'))
data = api.get(USER_CURRENT)
2020-10-21 12:55:33 +00:00
user_id = data['user_id']
apicaches=cache.set(cache_key, {'API_USER_ID': user_id})
LOGGER.warning('The cache is setting try to api_user_id:')
LOGGER.warning('The cache is setting key is: {}'.format(cache_key))
2020-10-21 12:55:33 +00:00
except APIError as err:
messages.error(request, err)
except Exception as err:
messages.error(request, err)
return {'API_USER_ID': user_id}
2017-10-30 14:05:31 +00:00
def api_tester_url(request):
"""Returns the URL to the API Tester for the API instance"""
url = getattr(settings, 'API_TESTER_URL', None)
return {'API_TESTER_URL': url}
2022-09-06 14:01:35 +00:00