<commit_before>"""Views fo the node settings page."""
# -*- coding: utf-8 -*-
import logging

from framework import request
from website.project.decorators import (must_have_addon,
    must_have_permission, must_not_be_registration,
    must_be_valid_project
)
from website.util import api_url_for

from website.addons.dropbox.client import get_client


logger = logging.getLogger(__name__)
debug = logger.debug

@must_be_valid_project
@must_have_addon('dropbox', 'node')
def dropbox_config_get(**kwargs):
    node = kwargs['node'] or kwargs['project']
    node_settings = kwargs['node_addon']
    urls = {
        'config': node.api_url_for('dropbox_config_put'),
        'deauthorize': node.api_url_for('dropbox_deauthorize'),
        'auth': api_url_for('dropbox_oauth_start', pid=kwargs['pid'],
            nid=kwargs.get('nid'))
    }
    if node_settings.has_auth:
        client = get_client(node_settings.user_settings.owner)
        # TODO: handle error
        metadata = client.metadata('/', list=True)
        # List each folder, including the root
        folders = ['/'] + [each['path'] for each in metadata['contents'] if each['is_dir']]
        result = {
            'folders': folders,
            'folder': node_settings.folder if node_settings.folder else '/',
            'ownerName': node_settings.user_settings.account_info['display_name'],
            'urls': urls,
            'nodeHasAuth': node_settings.has_auth,
            'userHasAuth': node_settings.user_settings.has_auth
        }
    else:
        if node_settings.user_settings:
            user_has_auth = node_settings.user_settings.has_auth
        else:
            user_has_auth = False
        result = {
            'nodeHasAuth': node_settings.has_auth,
            'userHasAuth': user_has_auth,
            'urls': urls
        }
    return {
        'result': result,
        'status': 200
    }, 200


@must_have_permission('write')
@must_not_be_registration
@must_have_addon('dropbox', 'node')
def dropbox_config_put(node_addon, **kwargs):
    folder = request.json.get('selected')
    node_addon.folder = folder
    node_addon.save()
    return {
        'result': {
            'folder': folder
        },
        'message': 'Successfully updated settings.',
        'status': 200
    }


@must_have_permission('write')
@must_have_addon('dropbox', 'node')
def dropbox_deauthorize(auth, node_addon, **kwargs):
    node_addon.deauthorize(auth=auth)
    node_addon.save()
    debug('DEAUTHORIZING----')
    debug(node_addon.has_auth)
    return {}
<commit_msg>Fix check for user auth<commit_after>"""Views fo the node settings page."""
# -*- coding: utf-8 -*-
import logging

from framework import request
from framework.auth import get_current_user
from website.project.decorators import (must_have_addon,
    must_have_permission, must_not_be_registration,
    must_be_valid_project
)
from website.util import api_url_for

from website.addons.dropbox.client import get_client


logger = logging.getLogger(__name__)
debug = logger.debug


@must_be_valid_project
@must_have_addon('dropbox', 'node')
def dropbox_config_get(**kwargs):
    user = get_current_user()
    user_settings = user.get_addon('dropbox')
    user_has_auth = user_settings is not None and user_settings.has_auth
    node = kwargs['node'] or kwargs['project']
    node_settings = kwargs['node_addon']
    urls = {
        'config': node.api_url_for('dropbox_config_put'),
        'deauthorize': node.api_url_for('dropbox_deauthorize'),
        'auth': api_url_for('dropbox_oauth_start', pid=kwargs['pid'],
            nid=kwargs.get('nid'))
    }
    if node_settings.has_auth:
        client = get_client(node_settings.user_settings.owner)
        # TODO: handle error
        metadata = client.metadata('/', list=True)
        # List each folder, including the root
        folders = ['/'] + [each['path'] for each in metadata['contents'] if each['is_dir']]
        result = {
            'folders': folders,
            'folder': node_settings.folder if node_settings.folder else '/',
            'ownerName': node_settings.user_settings.account_info['display_name'],
            'urls': urls,
            'nodeHasAuth': node_settings.has_auth,
            'userHasAuth': user_has_auth
        }
    else:
        result = {
            'nodeHasAuth': node_settings.has_auth,
            'userHasAuth': user_has_auth,
            'urls': urls
        }
    return {
        'result': result,
        'status': 200
    }, 200


@must_have_permission('write')
@must_not_be_registration
@must_have_addon('dropbox', 'node')
def dropbox_config_put(node_addon, **kwargs):
    folder = request.json.get('selected')
    node_addon.folder = folder
    node_addon.save()
    return {
        'result': {
            'folder': folder
        },
        'message': 'Successfully updated settings.',
        'status': 200
    }


@must_have_permission('write')
@must_have_addon('dropbox', 'node')
def dropbox_deauthorize(auth, node_addon, **kwargs):
    node_addon.deauthorize(auth=auth)
    node_addon.save()
    node = node_addon.owner
    return {}

{'fraction_non_alphanumeric': 0.12098668754894283, 'fraction_numerical': 0.003915426781519186, 'mean_word_length': 3.0872, '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': '3504968', 'n_tokens_mistral': 1612, 'n_tokens_neox': 1596, 'n_words': 308}