def fetch_registration():
    session = dps_session.get_state()
    reg_obj = None
    if 'registration' in session:
        reg_obj = session['registration']

    return session, reg_obj
def populate_session(dto, prefix=None):
    session = dps_session.get_state()
    for name, value in dto._asdict().items():
        full_name = name if prefix is None else (prefix + name)
        actual_value = '' if value is None else value
        session['registration'][full_name] = actual_value
    return session
def check_agreement(dataset_id):
    if g.user:
        session = dps_session.get_state()
        dataset = session['user']['datasets'].get(dataset_id)
        if dataset:
            return dataset['valid_licence']

    return False
def complete_sign_in():
    session = dps_session.get_state()

    # Redirect user to page before signing in if set
    referer = current_app.config.get('SITE_URL') + '/'
    if request.cookies.get('Referer') is not None:
        referer = request.cookies.get('Referer')

    return redirect(referer)
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 reset_api_key():
    try:
        session = dps_session.get_state()
        ulapd_api = UlapdAPI()

        user_id = session['user']['user_details']['user_details_id']
        ulapd_api.update_api_key(user_id)
        return redirect('/#my-api-key')
    except ApplicationError as e:
        raise ApplicationError(
            'Something went wrong when resetting API key - error: {}'.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 decorated(*args, **kwargs):
        try:
            if g.user:
                ulapd_api = UlapdAPI()
                session = dps_session.get_state()

                user_details = ulapd_api.get_user_details('email', g.user)
                session['user'].update(user_details)
                dps_session.commit()

            return f(*args, **kwargs)
        except Exception as e:
            raise ApplicationError(
                'Something went wrong when refreshing user session: {}'.format(
                    e))
def accept_licence(dataset_id):
    session = dps_session.get_state()
    try:
        ulapd_api = UlapdAPI()
        user_details = session['user']['user_details']

        send_metric(dataset_id, 'licence agreed', user_details['user_details_id'], user_details, None)

        data = {
            'user_details_id': user_details['user_details_id'],
            'licence_id': dataset_id
        }

        ulapd_api.create_licence_agreement(data)

    except Exception as e:
        raise ApplicationError('Error accepting licence: {}'.format(str(e)))
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 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))
def amend_session_user_type(user_type):
    session = dps_session.get_state()
    session['registration']['user_type'] = user_type
    return session