<commit_before>import json
import requests

from stagecraft.apps.datasets.models import OAuthUser
from stagecraft.libs.validation.validation import extract_bearer_token
from django.conf import settings
from django.http import (HttpResponseForbidden, HttpResponse)
from django.utils.cache import patch_response_headers
from statsd.defaults.django import statsd
from functools import wraps


@statsd.timer('get_user.timer')
def _get_user(access_token):
    user = None
    if access_token is not None:

        if settings.USE_DEVELOPMENT_USERS is True:
            try:
                user = settings.DEVELOPMENT_USERS[access_token]
            except KeyError:
                user = None
        else:
            user = _get_user_from_database(access_token)
            if user is None:
                user = _get_user_from_signon(access_token)
                if user is not None:
                    _set_user_to_database(access_token, user)

    return user


def _get_user_from_signon(access_token):
    response = requests.get(
        '{0}/user.json'.format(settings.SIGNON_URL),
        headers={'Authorization': 'Bearer {0}'.format(access_token)}
    )
    if response.status_code == 200:
        return response.json()['user']


def _get_user_from_database(access_token):
    oauth_user = OAuthUser.objects.get_by_access_token(access_token)
    if oauth_user:
        return oauth_user.serialize()


def _set_user_to_database(access_token, user):
    OAuthUser.objects.cache_user(access_token, user)


def check_permission(access_token, permission_requested):
    user = _get_user(access_token)
    has_permission = user is not None and \
        permission_requested in user['permissions']
    return (user, has_permission)


def unauthorized(message):
    response = HttpResponse(to_json({
        'status': 'error',
        'message': 'Unauthorizd: {}'.format(message),
    }), status=401)
    response['WWW-Authenticate'] = 'Bearer'
    return response


def forbidden(message):<commit_msg>Improve get_user timing in statsd<commit_after>import json
import requests

from stagecraft.apps.datasets.models import OAuthUser
from stagecraft.libs.validation.validation import extract_bearer_token
from django.conf import settings
from django.http import (HttpResponseForbidden, HttpResponse)
from statsd.defaults.django import statsd


@statsd.timer('get_user.both')
def _get_user(access_token):
    user = None
    if access_token is not None:

        if settings.USE_DEVELOPMENT_USERS is True:
            try:
                user = settings.DEVELOPMENT_USERS[access_token]
            except KeyError:
                user = None
        else:
            user = _get_user_from_database(access_token)
            if user is None:
                user = _get_user_from_signon(access_token)
                if user is not None:
                    _set_user_to_database(access_token, user)

    return user


@statsd.timer('get_user.signon')
def _get_user_from_signon(access_token):
    response = requests.get(
        '{0}/user.json'.format(settings.SIGNON_URL),
        headers={'Authorization': 'Bearer {0}'.format(access_token)}
    )
    if response.status_code == 200:
        return response.json()['user']


@statsd.timer('get_user.postgres')
def _get_user_from_database(access_token):
    oauth_user = OAuthUser.objects.get_by_access_token(access_token)
    if oauth_user:
        return oauth_user.serialize()


def _set_user_to_database(access_token, user):
    OAuthUser.objects.cache_user(access_token, user)


def check_permission(access_token, permission_requested):
    user = _get_user(access_token)
    has_permission = user is not None and \
        permission_requested in user['permissions']
    return (user, has_permission)


def unauthorized(message):
    response = HttpResponse(to_json({
        'status': 'error',
        'message': 'Unauthorizd: {}'.format(message),
    }), status=401)
    response['WWW-Authenticate'] = 'Bearer'
    return response


def forbidden(message):
{'fraction_non_alphanumeric': 0.08808161234137846, 'fraction_numerical': 0.003981089823339139, 'mean_word_length': 3.345945945945946, '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': '5309337', 'n_tokens_mistral': 1241, 'n_tokens_neox': 1198, 'n_words': 259}