def get_download_page(dataset_id):
    ulapd_api = UlapdAPI()

    if dataset_id == 'rfi':
        dataset, history = build_rfi_dataset_for_download(
            ulapd_api.get_dataset_by_name(dataset_id),
            ulapd_api.get_dataset_history(dataset_id))
    else:
        dataset = ulapd_api.get_dataset_by_name(dataset_id)
        history = ulapd_api.get_dataset_history(dataset_id)

    breadcrumb_links = [{
        "label": "Home",
        "href": "/"
    }, {
        "label": dataset['title'],
        "href": "/datasets/" + dataset['name']
    }, {
        "label": "Download dataset",
        "href": None
    }]

    return render_template('app/datasets/{}/download.html'.format(dataset_id),
                           dataset=dataset,
                           history=history,
                           breadcrumb_links=breadcrumb_links)
    def decorated(*args, **kwargs):
        try:
            ulapd_api = UlapdAPI()
            api_key = request.headers.get('Authorization')
            name = kwargs['dataset']
            dataset = ulapd_api.get_dataset_by_name(name)

            # Prevent 'open' datasets being accessed via API
            if dataset['type'] == 'open':
                raise ApplicationError(*errors.get('ulapd_ui',
                                                   'DATASET_NOT_FOUND',
                                                   filler=name),
                                       http_code=404)

            # Prevent 'confidential' datasets being available via API if user doesn't have access
            if dataset['type'] == 'confidential':
                try:
                    user_access = ulapd_api.get_user_details(
                        'api_key', api_key)['datasets']
                except ApplicationError:
                    raise ApplicationError(*errors.get('ulapd_ui',
                                                       'API_KEY_ERROR',
                                                       filler=api_key),
                                           http_code=404)

                if not user_access.get(name, False):
                    raise ApplicationError(*errors.get('ulapd_ui',
                                                       'DATASET_NOT_FOUND',
                                                       filler=name),
                                           http_code=404)

            return f(*args, **kwargs)
        except ApplicationError as e:
            return jsonify({'success': False, 'error': e.message}), e.http_code
    def test_get_dataset_by_name(self, mock_get):
        mock_get.return_value.json.return_value = {'name': 'ccod'}
        mock_get.return_value.status_code = 200

        ulapd_api = UlapdAPI()
        response = ulapd_api.get_dataset_by_name('ccod')

        self.assertEqual(response, {'name': 'ccod'})
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 post_agree_licence(dataset_id):
    try:
        ulapd_api = UlapdAPI()
        dataset_details = ulapd_api.get_dataset_by_name(dataset_id)

        if request.form.get('agree-licence') is None:
            is_freemium = dataset_details['type'] == 'freemium'

            md_html = ''

            # Until we convert licence MD files to HTML
            if not is_freemium:
                licence_file = open(
                    os.path.join(
                        directory,
                        f'../documents/datasets/{dataset_id}/licence.md'), "r")
                md_licence = licence_file.read()
                md_renderer = markdown.Markdown()
                md_html = md_renderer.convert(md_licence)

            current_app.logger.info(
                'Displaying licence page with errors for dataset: {}'.format(
                    dataset_id))
            error_msg = 'You need to agree to the terms and conditions to download data'
            return render_template(
                "app/datasets/licence.html",
                agree=True,
                dataset_id=dataset_id,
                licence_type='direct',
                md=md_html,
                error_title="There are errors on this page",
                fields={'agree-licence': {
                    'data': '',
                    'error': [error_msg]
                }})
        else:
            # Prevent users signing licences for nps/dad etc via the service
            if dataset_details['type'] not in ['confidential', 'restricted']:
                accept_licence(dataset_id)
                current_app.logger.info(
                    'Redirecting to download page for dataset: {}'.format(
                        dataset_id))
                session = dps_session.get_state()

                if dataset_id == 'nps_sample':
                    return redirect(url_for('.get_details', dataset_id='nps'))

            return redirect(url_for('general.get_list'))
    except Exception as e:
        raise ApplicationError(
            'Something went wrong when retrieving licence agree page: {}'.
            format(e))
def get_api_dataset_by_name(name):
    ulapd_api = UlapdAPI()

    # authenticate
    _authenticate(ulapd_api)

    dataset_details = ulapd_api.get_dataset_by_name(name)

    if dataset_details:
        response = {"success": True, "result": dataset_details}
        return response
    else:
        raise ApplicationError(*errors.get('ulapd_ui',
                                           'DATASET_NOT_FOUND',
                                           filler=name),
                               http_code=404)
def build_download_history():
    download_history = []
    session = dps_session.get_state()

    if g.user:
        try:
            ulapd_api = UlapdAPI()
            user_id = session['user']['user_details']['user_details_id']
            download_activities = ulapd_api.get_user_download_activity(user_id)
            download_activities = get_latest_download_activities(download_activities)

            if download_activities:
                for download_activity in download_activities:
                    dataset_id = download_activity['dataset_id']
                    package_details = ulapd_api.get_dataset_by_name(dataset_id)

                    download_datetime = datetime.strptime(download_activity['timestamp'], '%a, %d %b %Y %H:%M:%S %Z')
                    last_update_datetime = datetime.strptime(package_details['last_updated'], '%d %B %Y')
                    is_latest_download = False if download_datetime < last_update_datetime else True

                    licence_agree_string = None
                    license_exists = package_details.get('licence_id')
                    if license_exists:
                        licence_agree_date = session['user']['datasets'][dataset_id]['date_agreed']
                        licence_agree_datetime = datetime.strptime(licence_agree_date, '%a, %d %b %Y %H:%M:%S %Z')
                        licence_agree_string = datetime.strftime(licence_agree_datetime, '%d %B %Y')

                    download_history.append({
                        'dataset_id': dataset_id,
                        'dataset_title': package_details['title'],
                        'last_download_date': datetime.strftime(download_datetime, '%d %B %Y'),
                        'last_update_date': datetime.strftime(last_update_datetime, '%d %B %Y'),
                        'licence_exists': license_exists,
                        'is_latest_download': is_latest_download,
                        'licence_agree_date': licence_agree_string,
                        'is_licence_agreed': check_agreement(dataset_id),
                        'resources': package_details['resources']
                        })

        except Exception as e:
            raise ApplicationError('Error building download history: {}'.format(e))

    current_app.logger.info('Returning history of file downloads: {}'.format(download_history))
    return download_history
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
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))