Skip to content

Commit

Permalink
Error handling for KPI Dashboard of API Manager if CanGetConsumers, C…
Browse files Browse the repository at this point in the history
…anGetAnyUser is not present OpenBankProject#65
  • Loading branch information
PengfeiLi0218 committed Jan 28, 2019
1 parent aed1194 commit 18dbaee
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions apimanager/metrics/views.py
Expand Up @@ -9,7 +9,7 @@
from datetime import datetime, timedelta

from django.conf import settings
from apimanager.local_settings import API_HOST, EXCLUDE_APPS, EXCLUDE_FUNCTIONS, EXCLUDE_URL_PATTERN, API_EXPLORER_APP_NAME, API_DATEFORMAT
from apimanager.settings import API_HOST, EXCLUDE_APPS, EXCLUDE_FUNCTIONS, EXCLUDE_URL_PATTERN, API_EXPLORER_APP_NAME, API_DATEFORMAT
from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic import TemplateView
Expand Down Expand Up @@ -563,25 +563,25 @@ def plot_bar_chart(self, data):

def get_users_cansearchwarehouse(self):
users = []
users_with_cansearchwarehouse = []
email_with_cansearchwarehouse = []
api = API(self.request.session.get('obp'))
try:
urlpath = '/users'
users = api.get(urlpath)
except APIError as err:
error_once_only(self.request, err)

users_with_cansearchwarehouse = []
email_with_cansearchwarehouse = []

try:
for user in users['users']:
for entitlement in user['entitlements']['list']:
if 'CanSearchWarehouse' in entitlement['role_name']:
users_with_cansearchwarehouse.append(user["username"])
email_with_cansearchwarehouse.append(user["email"])
# fail gracefully in case API provides new structure
except KeyError as err:
messages.error(self.request, 'KeyError: {}'.format(err))
else:
try:
for user in users['users']:
for entitlement in user['entitlements']['list']:
if 'CanSearchWarehouse' in entitlement['role_name']:
users_with_cansearchwarehouse.append(user["username"])
email_with_cansearchwarehouse.append(user["email"])
# fail gracefully in case API provides new structure
except KeyError as err:
messages.error(self.request, 'KeyError: {}'.format(err))

user_email_cansearchwarehouse = dict(zip(users_with_cansearchwarehouse, email_with_cansearchwarehouse))
number_of_users_with_cansearchwarehouse = len(user_email_cansearchwarehouse)
Expand Down

0 comments on commit 18dbaee

Please sign in to comment.