<commit_before>from django.core.urlresolvers import reverse
from django.conf import settings
from django.contrib.auth import get_user_model
from django.utils.translation import ugettext_lazy as _

import logging
import requests
import urllib
import urlparse

GOOGLE, FACEBOOK = 'google', 'facebook'
JSON_MIME_TYPE = 'application/json'
logger = logging.getLogger('saleor.registration')
User = get_user_model()


def get_local_host(request):
    scheme = 'http' + ('s' if request.is_secure() else '')
    return url(scheme=scheme, host=request.get_host())


def url(scheme='', host='', path='', params='', query='', fragment=''):
    return urlparse.urlunparse((scheme, host, path, params, query, fragment))


def get_client_class_for_serivce(service):
    return {GOOGLE: GoogleClient, FACEBOOK: FacebookClient}[service]


def get_google_login_url(local_host):
    return get_client_class_for_serivce(GOOGLE)(local_host).get_login_uri()


def get_facebook_login_url(local_host):
    return get_client_class_for_serivce(FACEBOOK)(local_host).get_login_uri()


class OAuth2RequestAuthorizer(requests.auth.AuthBase):

    def __init__(self, access_token):
        self.access_token = access_token

    def __call__(self, request):
        request.headers['Authorization'] = 'Bearer %s' % (self.access_token,)
        return request


class OAuth2Client(object):

    service = None

    client_id = None
    client_secret = None

    auth_uri = None
    token_uri = None
    user_info_uri = None

    scope = None

    def __init__(self, local_host, code=None):
        self.local_host = local_host
        self.code = code
        self.access_token = None

    def get_redirect_uri(self):
        kwargs = {'service': self.service}
        path = reverse('registration:oauth_callback', kwargs=kwargs)
        return urlparse.urljoin(self.local_host, path)

    def get_login_uri(self):
        data = {'response_type': 'code',
                'scope': self.scope,
                'redirect_uri': self.get_redirect_uri(),
                'client_id': self.client_id}
        query = urllib.urlencode(data)
        return urlparse.urljoin(self.auth_uri, url(query=query))

    def get_user_info(self):
        return self.get(self.user_info_uri)

    def get_access_token(self, code):
        data = {'grant_type': 'authorization_code',
                'client_id': self.client_id,
                'client_secret': self.client_secret,
                'code': code,
                'redirect_uri': self.get_redirect_uri(),
                'scope': self.scope}
        response = self.post(self.token_uri, data=data, auth=False)
        return response['access_token']

    def get(self, address, params=None, auth=True):
        auth = self.get_authorizer() if auth else None
        response = requests.get(address, params=params, auth=auth)
        return self._handle_response(response)

    def post(self, address, data=None, auth=True):
        auth = self.get_authorizer() if auth else None
        response = requests.post(address, data=data, auth=auth)
        return self._handle_response(response)

    def get_authorizer(self):
        if not self.access_token:
            self.access_token = self.get_access_token(self.code)
        return OAuth2RequestAuthorizer(access_token=self.access_token)

    def _handle_response(self, response):
        response_content = self._parse_response(response)
        if response.status_code == requests.codes.ok:
            return response_content
        else:
            logger.error('[%s]: %s', response.status_code, response.text)
            error = self._extract_error_from_response(response_content)
            raise ValueError(error)

    def _parse_response(self, response):
        if JSON_MIME_TYPE in response.headers['Content-Type']:
            return response.json()
        else:
            content = urlparse.parse_qsl(response.text)
            content = dict((x, y[0] if len(y) == 1 else y) for x, y in content)
            return content

    def _extract_error_from_response(self, response_content):
        raise NotImplementedError()


class GoogleClient(OAuth2Client):

    service = GOOGLE

    client_id = settings.GOOGLE_CLIENT_ID
    client_secret = settings.GOOGLE_SECRET

    auth_uri = 'https://accounts.google.com/o/oauth2/auth'
    token_uri = 'https://accounts.google.com/o/oauth2/token'
    user_info_uri = 'https://www.googleapis.com/oauth2/v1/userinfo'

    scope = ' '.join(['https://www.googleapis.com/auth/userinfo.email',
                      'https://www.googleapis.com/auth/plus.me'])

    def get_user_info(self):
        response = super(GoogleClient, self).get_user_info()
        if response['verified_email']:
            return response
        else:
            raise ValueError(_('Google account not verified.'))

    def _extract_error_from_response(self, response_content):
        return response_content['error']


class FacebookClient(OAuth2Client):

    service = FACEBOOK

    client_id = settings.FACEBOOK_APP_ID
    client_secret = settings.FACEBOOK_SECRET

    auth_uri = 'https://www.facebook.com/dialog/oauth'
    token_uri = 'https://graph.facebook.com/oauth/access_token'
    user_info_uri = 'https://graph.facebook.com/me'

    scope = ','.join(['email'])

    def get_user_info(self):
        response = super(FacebookClient, self).get_user_info()
        if response['verified']:
            return response
        else:
            raise ValueError(_('Facebook account not verified.'))

    def _extract_error_from_response(self, response_content):
        return response_content['error']['message']
<commit_msg>Make obtaining access_code more verbose<commit_after>from django.core.urlresolvers import reverse
from django.conf import settings
from django.contrib.auth import get_user_model
from django.utils.translation import ugettext_lazy as _

import logging
import requests
import urllib
import urlparse

GOOGLE, FACEBOOK = 'google', 'facebook'
JSON_MIME_TYPE = 'application/json'
logger = logging.getLogger('saleor.registration')
User = get_user_model()


def get_local_host(request):
    scheme = 'http' + ('s' if request.is_secure() else '')
    return url(scheme=scheme, host=request.get_host())


def url(scheme='', host='', path='', params='', query='', fragment=''):
    return urlparse.urlunparse((scheme, host, path, params, query, fragment))


def get_client_class_for_serivce(service):
    return {GOOGLE: GoogleClient, FACEBOOK: FacebookClient}[service]


def get_google_login_url(local_host):
    return get_client_class_for_serivce(GOOGLE)(local_host).get_login_uri()


def get_facebook_login_url(local_host):
    return get_client_class_for_serivce(FACEBOOK)(local_host).get_login_uri()


class OAuth2RequestAuthorizer(requests.auth.AuthBase):

    def __init__(self, access_token):
        self.access_token = access_token

    def __call__(self, request):
        request.headers['Authorization'] = 'Bearer %s' % (self.access_token,)
        return request


class OAuth2Client(object):

    service = None

    client_id = None
    client_secret = None

    auth_uri = None
    token_uri = None
    user_info_uri = None

    scope = None

    def __init__(self, local_host, code=None):
        self.local_host = local_host
        if code:
            access_token = self.get_access_token(code)
            self.authorizer = OAuth2RequestAuthorizer(access_token=access_token)

    def get_redirect_uri(self):
        kwargs = {'service': self.service}
        path = reverse('registration:oauth_callback', kwargs=kwargs)
        return urlparse.urljoin(self.local_host, path)

    def get_login_uri(self):
        data = {'response_type': 'code',
                'scope': self.scope,
                'redirect_uri': self.get_redirect_uri(),
                'client_id': self.client_id}
        query = urllib.urlencode(data)
        return urlparse.urljoin(self.auth_uri, url(query=query))

    def get_access_token(self, code):
        data = {'grant_type': 'authorization_code',
                'client_id': self.client_id,
                'client_secret': self.client_secret,
                'code': code,
                'redirect_uri': self.get_redirect_uri(),
                'scope': self.scope}
        response = self.post(self.token_uri, data=data, authorize=False)
        return response['access_token']

    def get_user_info(self):
        return self.get(self.user_info_uri)

    def get(self, address, params=None, authorize=True):
        auth = self.authorizer if authorize else None
        response = requests.get(address, params=params, auth=auth)
        return self.handle_response(response)

    def post(self, address, data=None, authorize=True):
        auth = self.authorizer if authorize else None
        response = requests.post(address, data=data, auth=auth)
        return self.handle_response(response)

    def handle_response(self, response):
        response_content = self.parse_response(response)
        if response.status_code == requests.codes.ok:
            return response_content
        else:
            logger.error('[%s]: %s', response.status_code, response.text)
            error = self.extract_error_from_response(response_content)
            raise ValueError(error)

    def parse_response(self, response):
        if JSON_MIME_TYPE in response.headers['Content-Type']:
            return response.json()
        else:
            content = urlparse.parse_qsl(response.text)
            content = dict((x, y[0] if len(y) == 1 else y) for x, y in content)
            return content

    def extract_error_from_response(self, response_content):
        raise NotImplementedError()


class GoogleClient(OAuth2Client):

    service = GOOGLE

    client_id = settings.GOOGLE_CLIENT_ID
    client_secret = settings.GOOGLE_SECRET

    auth_uri = 'https://accounts.google.com/o/oauth2/auth'
    token_uri = 'https://accounts.google.com/o/oauth2/token'
    user_info_uri = 'https://www.googleapis.com/oauth2/v1/userinfo'

    scope = ' '.join(['https://www.googleapis.com/auth/userinfo.email',
                      'https://www.googleapis.com/auth/plus.me'])

    def get_user_info(self):
        response = super(GoogleClient, self).get_user_info()
        if response['verified_email']:
            return response
        else:
            raise ValueError(_('Google account not verified.'))

    def extract_error_from_response(self, response_content):
        return response_content['error']


class FacebookClient(OAuth2Client):

    service = FACEBOOK

    client_id = settings.FACEBOOK_APP_ID
    client_secret = settings.FACEBOOK_SECRET

    auth_uri = 'https://www.facebook.com/dialog/oauth'
    token_uri = 'https://graph.facebook.com/oauth/access_token'
    user_info_uri = 'https://graph.facebook.com/me'

    scope = ','.join(['email'])

    def get_user_info(self):
        response = super(FacebookClient, self).get_user_info()
        if response['verified']:
            return response
        else:
            raise ValueError(_('Facebook account not verified.'))

    def extract_error_from_response(self, response_content):
        return response_content['error']['message']

{'fraction_non_alphanumeric': 0.10822162645218945, 'fraction_numerical': 0.001966041108132261, 'mean_word_length': 3.174188735546438, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 16, 'lorem ipsum': 0, 'www.': 8, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '474057', 'n_tokens_mistral': 3418, 'n_tokens_neox': 3296, 'n_words': 671}