コード例 #1
0
    def test_get_download_link(self, mock_get):
        mock_get.return_value.json.return_value = {'link': 'https://s3.com'}
        mock_get.return_value.status_code = 200

        ulapd_api = UlapdAPI()
        response = ulapd_api.get_download_link('ccod', 'ccod_full.csv')

        self.assertEqual(response, {'link': 'https://s3.com'})
コード例 #2
0
def get_api_download_link(dataset_name, file_name, date=None):
    try:
        ulapd_api = UlapdAPI()
        dataset_details = ulapd_api.get_dataset_by_name(dataset_name)

        # authenticate
        user_details = _authenticate(ulapd_api)

        # check agreement
        agreement = user_details['datasets'].get(dataset_name)
        if not agreement:
            if dataset_details['private'] is True:
                raise ApplicationError(*errors.get('ulapd_ui',
                                                   'NO_DATASET_ACCESS',
                                                   filler=dataset_name),
                                       http_code=403)

            raise ApplicationError(*errors.get('ulapd_ui',
                                               'NO_LICENCE_SIGNED',
                                               filler=dataset_name),
                                   http_code=403)
        if agreement['valid_licence'] is False:
            raise ApplicationError(*errors.get('ulapd_ui',
                                               'NO_LICENCE_SIGNED',
                                               filler=dataset_name),
                                   http_code=403)

        # check to see if the filename exists for history files
        if date:
            resource_exists = False
            history_details = ulapd_api.get_dataset_history(dataset_name)
            for history in history_details['dataset_history']:
                exist = any(
                    map(lambda resource: resource['file_name'] == file_name,
                        history['resource_list']))
                if exist:
                    resource_exists = True
                    break
        else:
            # check to see if the filename exists for latest files
            resource_exists = any(
                map(lambda resource: resource['file_name'] == file_name,
                    dataset_details['resources']))
        if not resource_exists:
            # check to see if the filename exists for public files
            if 'public_resources' in dataset_details:
                public_exists = any(
                    map(lambda public: public['file_name'] == file_name,
                        dataset_details['public_resources']))
                if not public_exists:
                    raise ApplicationError(*errors.get('ulapd_ui',
                                                       'FILE_DOES_NOT_EXIST',
                                                       filler=file_name),
                                           http_code=404)
            else:
                raise ApplicationError(*errors.get('ulapd_ui',
                                                   'FILE_DOES_NOT_EXIST',
                                                   filler=file_name),
                                       http_code=404)

        if date:
            link = ulapd_api.get_history_download_link(dataset_name, file_name,
                                                       date)
        else:
            link = ulapd_api.get_download_link(dataset_name, file_name)
        if link:
            response = {
                "success": True,
                "result": {
                    "resource": file_name,
                    "valid_for_seconds": 10,
                    "download_url": link["link"]
                }
            }

            # Activity create
            ulapd_api.create_activity(
                user_details['user_details']['user_details_id'], 'download',
                request.remote_addr, True, file_name, dataset_name)

            send_metric(dataset_name, 'download api',
                        user_details['user_details']['user_details_id'],
                        user_details['user_details'], file_name)
            return response
        else:
            current_app.logger.error(
                'There was a problem getting the resource: '.format(file_name))
            raise ApplicationError(*errors.get('ulapd_ui',
                                               'DATASET_NOT_FOUND',
                                               filler=dataset_name),
                                   http_code=404)
    except ApplicationError as error:
        raise error
    except Exception as e:
        raise e
コード例 #3
0
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))