<commit_before># -*- coding: utf-8 -*-

import httplib as http

from flask import request
from modularodm import Q
from modularodm.storage.base import KeyExistsException

from framework.exceptions import HTTPError
from framework.auth.decorators import must_be_logged_in
from website.project import decorators
from website.util.sanitize import assert_clean
from website.addons.dataverse import client
from website.addons.dataverse.provider import DataverseProvider
from website.addons.dataverse.serializer import DataverseSerializer
from website.oauth.models import ExternalAccount


@must_be_logged_in
def dataverse_get_user_accounts(auth):
    """ Returns the list of all of the current user's authorized Dataverse accounts """

    return DataverseSerializer(
        user_settings=auth.user.get_addon('dataverse')
    ).serialized_user_settings


@must_be_logged_in
def dataverse_add_user_account(auth, **kwargs):
    """Verifies new external account credentials and adds to user's list"""
    user = auth.user
    provider = DataverseProvider()

    host = request.json.get('host').rstrip('/')
    api_token = request.json.get('api_token')

    # Verify that credentials are valid
    client.connect_or_401(host, api_token)

    # Note: `DataverseSerializer` expects display_name to be a URL
    try:
        provider.account = ExternalAccount(
            provider=provider.short_name,
            provider_name=provider.name,
            display_name=host,       # no username; show host
            oauth_key=host,          # hijacked; now host
            oauth_secret=api_token,  # hijacked; now api_token
            provider_id=api_token,   # Change to username if Dataverse allows
        )
        provider.account.save()
    except KeyExistsException:
        # ... or get the old one
        provider.account = ExternalAccount.find_one(
            Q('provider', 'eq', provider.short_name) &
            Q('provider_id', 'eq', api_token)
        )
        assert provider.account is not None

    if provider.account not in user.external_accounts:
        user.external_accounts.append(provider.account)
        user.save()

    return {}


@must_be_logged_in
@decorators.must_be_valid_project
@decorators.must_have_addon('dataverse', 'node')
def dataverse_get_config(node_addon, auth, **kwargs):
    """API that returns the serialized node settings."""
    result = DataverseSerializer(
        user_settings=auth.user.get_addon('dataverse'),
        node_settings=node_addon,
    ).serialized_node_settings
    return {'result': result}, http.OK


@decorators.must_have_permission('write')<commit_msg>Allow users to connect dataverse account<commit_after># -*- coding: utf-8 -*-

import httplib as http

from flask import request
from modularodm import Q
from modularodm.storage.base import KeyExistsException

from framework.exceptions import HTTPError
from framework.auth.decorators import must_be_logged_in
from website.project import decorators
from website.util.sanitize import assert_clean
from website.addons.dataverse import client
from website.addons.dataverse.provider import DataverseProvider
from website.addons.dataverse.serializer import DataverseSerializer
from website.oauth.models import ExternalAccount


@must_be_logged_in
def dataverse_get_user_accounts(auth):
    """ Returns the list of all of the current user's authorized Dataverse accounts """

    return DataverseSerializer(
        user_settings=auth.user.get_addon('dataverse')
    ).serialized_user_settings


@must_be_logged_in
def dataverse_add_user_account(auth, **kwargs):
    """Verifies new external account credentials and adds to user's list"""
    user = auth.user
    provider = DataverseProvider()

    host = request.json.get('host').rstrip('/')
    api_token = request.json.get('api_token')

    # Verify that credentials are valid
    client.connect_or_401(host, api_token)

    # Note: `DataverseSerializer` expects display_name to be a URL
    try:
        provider.account = ExternalAccount(
            provider=provider.short_name,
            provider_name=provider.name,
            display_name=host,       # no username; show host
            oauth_key=host,          # hijacked; now host
            oauth_secret=api_token,  # hijacked; now api_token
            provider_id=api_token,   # Change to username if Dataverse allows
        )
        provider.account.save()
    except KeyExistsException:
        # ... or get the old one
        provider.account = ExternalAccount.find_one(
            Q('provider', 'eq', provider.short_name) &
            Q('provider_id', 'eq', api_token)
        )
        assert provider.account is not None

    if provider.account not in user.external_accounts:
        user.external_accounts.append(provider.account)

    user_addon = auth.user.get_addon('dataverse')
    if not user_addon:
        user.add_addon('dataverse')
    user.save()

    return {}


@must_be_logged_in
@decorators.must_be_valid_project
@decorators.must_have_addon('dataverse', 'node')
def dataverse_get_config(node_addon, auth, **kwargs):
    """API that returns the serialized node settings."""
    result = DataverseSerializer(
        user_settings=auth.user.get_addon('dataverse'),
        node_settings=node_addon,
    ).serialized_node_settings
    return {'result': result}, http.OK


@decorators.must_have_permission('write')
{'fraction_non_alphanumeric': 0.08586328925311976, 'fraction_numerical': 0.001490035388340473, 'mean_word_length': 3.5858240819812126, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1873687', 'n_tokens_mistral': 1542, 'n_tokens_neox': 1508, 'n_words': 382}