def test_check_agreement_no_session(self, mock_g, mock_session): mock_session.get_state.return_value = data.session_data mock_g.user = None is_agreed = check_agreement('ocod') self.assertFalse(is_agreed)
def test_check_agreement_not_valid(self, mock_g, mock_session): mock_session.get_state.return_value = data.session_data mock_g.user = '******' is_agreed = check_agreement('ocod') self.assertFalse(is_agreed)
def get_agree_licence(dataset_id): try: ulapd_api = UlapdAPI() dataset_details = ulapd_api.get_dataset_by_name(dataset_id) accepted = check_agreement(dataset_id) if dataset_details['type'] == 'freemium': accepted = True session = dps_session.get_state() dataset = session['user']['datasets'].get(dataset_id) if not dataset: accepted = False if dataset: if 'Direct Use' not in dataset['licences']: accepted = False if len(dataset['licences']) == 1: if 'Direct' in dataset['licences'][ 0] and not dataset['valid_licence']: accepted = False if not accepted: return render_template("app/datasets/licence.html", agree=True, dataset_id=dataset_id, licence_type='direct') if accepted: current_app.logger.info( 'Redirecting to download page for dataset: {}'.format( dataset_id)) return redirect(url_for('.get_details', dataset_id=dataset_id)) licence_file = open( os.path.join( directory, '../documents/datasets/{}/licence.md').format(dataset_id), "r") md_licence = licence_file.read() md_renderer = markdown.Markdown() md_html = md_renderer.convert(md_licence) current_app.logger.info( 'Displaying agree licence page for dataset: {}'.format(dataset_id)) return render_template("app/datasets/licence.html", agree=True, dataset_id=dataset_id, md=md_html) except Exception as e: raise ApplicationError( 'Something went wrong when retrieving licence agree page: {}'. format(e))
def download(dataset_id, file_name, last_updated): try: ulapd_api = UlapdAPI() dataset = ulapd_api.get_dataset_by_name(dataset_id) # First check to see if its a public resource if last_updated is None: for resource in dataset['public_resources']: current_app.logger.info("Public: " + str(resource['file_name'])) if resource['file_name'] == file_name: current_app.logger.info( "Public file download, skipping checks") url = ulapd_api.get_download_link(dataset_id, resource['file_name']) return redirect(url['link']) if dataset['type'] != 'open': # Need the session to get infor about the dataset and user session = dps_session.get_state() user_id = session['user']['user_details']['user_details_id'] user_data = session['user']['user_details'] # 1. Check if user is authenticated if not dps_session.is_logged_in(): return '/sign-in' # 2. Check if user has signed the correct licence if check_agreement(dataset_id) is not True: current_app.logger.info("User has no access to dataset") return url_for('datasets.get_agree_licence', dataset_id=dataset_id) # 3. Generate link if last_updated: last_updated = historic_date_formatter( last_updated, dataset['update_frequency']) url = ulapd_api.get_history_download_link( dataset_id, file_name, last_updated) activity = 'history download' else: url = ulapd_api.get_download_link(dataset_id, file_name) activity = 'download' # 4. Track the download and return (create activity) ulapd_api.create_activity( session['user']['user_details']['user_details_id'], "download", request.remote_addr, False, file_name, dataset_id) send_metric(dataset_id, activity + " ui", user_id, user_data, file_name) else: # 1. Generate link if last_updated: last_updated = historic_date_formatter( last_updated, dataset['update_frequency']) url = ulapd_api.get_history_download_link( dataset_id, file_name, last_updated) else: url = ulapd_api.get_download_link(dataset_id, file_name) return redirect(url['link']) except Exception as e: raise ApplicationError( 'Tracking download has failed - error: {}'.format(e))
def get_details(dataset_id): try: ulapd_api = UlapdAPI() session = dps_session.get_state() if dataset_id == 'nps_sample': return redirect(url_for('.get_details', dataset_id='nps')) # Go get the individual dataset details dataset_details = ulapd_api.get_dataset_by_name(dataset_id) # Fail nicely if the dataset doesnt exist if dataset_id not in dataset_details['name']: raise ApplicationError( 'Unable to display dataset details: dataset does not exist', http_code=404) # Now get the example json and to our dataset list extras = build_dataset_details(dataset_id) # Add details to dataset dataset_details.update(extras) # get dataset for dataset_id to check private boolean, if false is non restricted, if true is restricted is_restricted = dataset_details['private'] licence_signed = check_agreement(dataset_id) if dataset_id == 'nps': licence_signed = { 'nps': check_agreement(dataset_id), 'nps_sample': check_agreement('nps_sample') } # Handle freemium licencing: # If a user has signed exploratory/commercial licences they should still be able to sign direct licence if dataset_details['type'] == 'freemium': if g.user: licence_signed = True dataset = session['user']['datasets'].get(dataset_id) if not dataset: licence_signed = False if dataset: if 'Direct Use' not in dataset['licences']: licence_signed = False if len(dataset['licences']) == 1: if 'Direct' in dataset['licences'][ 0] and not dataset['valid_licence']: licence_signed = False current_app.logger.info( 'Displaying details for user requested dataset: {}'.format( dataset_id)) breadcrumb_links = [{ "label": "Home", "href": "/" }, { "label": dataset_details['title'], "href": None }] return render_template( "app/datasets/{}/details.html".format(dataset_id), dataset_details=dataset_details, licence_signed=licence_signed, is_restricted=is_restricted, readable_date=dataset_details['last_updated'], breadcrumb_links=breadcrumb_links) except ApplicationError as e: raise ApplicationError( 'Something went wrong when retrieving dataset details: {}'.format( e))
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))