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
|
2017-05-21 14:35:45 +00:00
|
|
|
from django.contrib import messages
|
2016-11-09 13:54:47 +00:00
|
|
|
|
2020-10-22 12:03:32 +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}
|
|
|
|
|
|
|
|
|
|
|
2021-07-29 14:45:23 +00:00
|
|
|
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):
|
2020-05-14 08:16:08 +00:00
|
|
|
"""Returns the API username/email of the logged-in user"""
|
|
|
|
|
nametodisplay = 'not authenticated'
|
2022-12-27 07:51:10 +00:00
|
|
|
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:
|
2022-12-27 07:51:10 +00:00
|
|
|
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))
|
2023-01-03 14:42:48 +00:00
|
|
|
return {'API_USERNAME': nametodisplay}
|
2017-10-30 14:05:31 +00:00
|
|
|
|
2022-12-27 07:51:10 +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
|
2022-12-27 07:51:10 +00:00
|
|
|
except APIError as err:
|
|
|
|
|
messages.error(request, err)
|
|
|
|
|
except Exception as err:
|
|
|
|
|
messages.error(request, err)
|
2017-10-30 14:05:31 +00:00
|
|
|
|
2018-01-03 14:18:43 +00:00
|
|
|
def api_user_id(request):
|
|
|
|
|
"""Returns the API user id of the logged-in user"""
|
|
|
|
|
user_id = 'not authenticated'
|
2022-12-27 07:51:10 +00:00
|
|
|
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'))
|
2022-12-27 07:51:10 +00:00
|
|
|
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})
|
2020-10-22 12:03:32 +00:00
|
|
|
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}
|
2018-01-03 14:18:43 +00:00
|
|
|
|
|
|
|
|
|
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
|
|
|
|