From 18dbaeee45e0d98221ca02e704339872c54d6dca Mon Sep 17 00:00:00 2001 From: PengfeiLi0218 Date: Mon, 28 Jan 2019 20:16:40 +0800 Subject: [PATCH] Error handling for KPI Dashboard of API Manager if CanGetConsumers, CanGetAnyUser is not present #65 --- apimanager/metrics/views.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/apimanager/metrics/views.py b/apimanager/metrics/views.py index cba4a7ee..c9ae87e2 100644 --- a/apimanager/metrics/views.py +++ b/apimanager/metrics/views.py @@ -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 @@ -563,6 +563,8 @@ 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' @@ -570,18 +572,16 @@ def get_users_cansearchwarehouse(self): 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)