def test_get_external_datasets(self, mock_get): mock_get.return_value.json.return_value = [{'name': 'hpi'}] mock_get.return_value.status_code = 200 ulapd_api = UlapdAPI() response = ulapd_api.get_external_datasets() self.assertEqual(response, [{'name': 'hpi'}])
def get_list(): try: ulapd_api = UlapdAPI() session = dps_session.get_state() internal_datasets = ulapd_api.get_datasets() external_datasets = ulapd_api.get_external_datasets() # Filter out sample internal_datasets = [ d for d in internal_datasets if '_sample' not in d['name'] ] # Add internal/external datasets together dataset_list = internal_datasets + external_datasets # Sort alphabetically putting datasets starting with numeric characters last dataset_list.sort( key=lambda d: 'z' if d['title'][0].isdigit() else d['title']) # User specific data user_access = {} api_key = '' user_has_activity = False if g.user: api_key = session['user']['user_details']['api_key'] # Dictionary of datasets user has access to user_access = {d: True for d in session['user']['datasets']} # Check if user has downloaded anything for 'agreed licence but not downloaded' state user_activity = ulapd_api.get_user_download_activity( session['user']['user_details']['user_details_id']) user_has_activity = bool(user_activity) # Get dataset history for agreed datasets agreed_dataset_list = [] for dataset in dataset_list: if check_agreement(dataset['name']): dataset['history'] = ulapd_api.get_dataset_history( dataset['name']) agreed_dataset_list.append(dataset) # Filter out confidential (e.g. DAD dataset) from listings page dataset_list = [d for d in dataset_list if 'confidential' != d['type']] freemium_licences = {} for dataset in user_access: if dataset == 'res_cov' or dataset == 'leases': dataset_licence = session['user']['datasets'][dataset][ 'licences'] if len(dataset_licence) == 1: licence_string = '{} licence'.format(dataset_licence[0]) freemium_licences[dataset] = licence_string else: start = ", ".join(dataset_licence[:-1]) licence_string = '{} and {} licences'.format( start, dataset_licence[-1]) freemium_licences[dataset] = licence_string return render_template('app/datasets/index.html', datasets_list=dataset_list, api_key=api_key, user_access=user_access, agreed_dataset_list=agreed_dataset_list, dps_session=session, user_has_activity=user_has_activity, freemium_licences=freemium_licences) except ApplicationError as e: raise ApplicationError( 'Something went wrong when retrieving the datasets - error: {}'. format(e))