<commit_before>from django.utils.encoding import python_2_unicode_compatible

from allauth.socialaccount import app_settings
from allauth.account.models import EmailAddress

from ..models import SocialApp, SocialAccount, SocialLogin
from ..adapter import get_adapter


class AuthProcess(object):
    LOGIN = 'login'
    CONNECT = 'connect'
    REDIRECT = 'redirect'


class AuthAction(object):
    AUTHENTICATE = 'authenticate'
    REAUTHENTICATE = 'reauthenticate'


class AuthError(object):
    UNKNOWN = 'unknown'
    CANCELLED = 'cancelled'  # Cancelled on request of user
    DENIED = 'denied'  # Denied by server


class Provider(object):
    def get_login_url(self, request, next=None, **kwargs):
        """
        Builds the URL to redirect to when initiating a login for this
        provider.
        """
        raise NotImplementedError("get_login_url() for " + self.name)

    def get_app(self, request):
        return SocialApp.objects.get_current(self.id)

    def media_js(self, request):
        """
        Some providers may require extra scripts (e.g. a Facebook connect)
        """
        return ''

    def wrap_account(self, social_account):
        return self.account_class(social_account)

    def get_settings(self):
        return app_settings.PROVIDERS.get(self.id, {})

    def sociallogin_from_response(self, request, response):
        adapter = get_adapter()
        uid = self.extract_uid(response)
        extra_data = self.extract_extra_data(response)
        common_fields = self.extract_common_fields(response)
        socialaccount = SocialAccount(extra_data=extra_data,
                                      uid=uid,
                                      provider=self.id)
        email_addresses = self.extract_email_addresses(response)
        self.cleanup_email_addresses(common_fields.get('email'),
                                     email_addresses)
        sociallogin = SocialLogin(account=socialaccount,
                                  email_addresses=email_addresses)
        user = sociallogin.user = adapter.new_user(request, sociallogin)
        user.set_unusable_password()
        adapter.populate_user(request, sociallogin, common_fields)
        return sociallogin

    def extract_extra_data(self, data):
        return data

    def extract_basic_socialaccount_data(self, data):
        """
        Returns a tuple of basic/common social account data.
        For example: ('123', {'first_name': 'John'})
        """
        raise NotImplementedError

    def extract_common_fields(self, data):
        """
        For example:
<commit_msg>Provider: added base `extract_uid()` method<commit_after>from django.utils.encoding import python_2_unicode_compatible

from allauth.socialaccount import app_settings
from allauth.account.models import EmailAddress

from ..models import SocialApp, SocialAccount, SocialLogin
from ..adapter import get_adapter


class AuthProcess(object):
    LOGIN = 'login'
    CONNECT = 'connect'
    REDIRECT = 'redirect'


class AuthAction(object):
    AUTHENTICATE = 'authenticate'
    REAUTHENTICATE = 'reauthenticate'


class AuthError(object):
    UNKNOWN = 'unknown'
    CANCELLED = 'cancelled'  # Cancelled on request of user
    DENIED = 'denied'  # Denied by server


class Provider(object):
    def get_login_url(self, request, next=None, **kwargs):
        """
        Builds the URL to redirect to when initiating a login for this
        provider.
        """
        raise NotImplementedError("get_login_url() for " + self.name)

    def get_app(self, request):
        return SocialApp.objects.get_current(self.id)

    def media_js(self, request):
        """
        Some providers may require extra scripts (e.g. a Facebook connect)
        """
        return ''

    def wrap_account(self, social_account):
        return self.account_class(social_account)

    def get_settings(self):
        return app_settings.PROVIDERS.get(self.id, {})

    def sociallogin_from_response(self, request, response):
        adapter = get_adapter()
        uid = self.extract_uid(response)
        extra_data = self.extract_extra_data(response)
        common_fields = self.extract_common_fields(response)
        socialaccount = SocialAccount(extra_data=extra_data,
                                      uid=uid,
                                      provider=self.id)
        email_addresses = self.extract_email_addresses(response)
        self.cleanup_email_addresses(common_fields.get('email'),
                                     email_addresses)
        sociallogin = SocialLogin(account=socialaccount,
                                  email_addresses=email_addresses)
        user = sociallogin.user = adapter.new_user(request, sociallogin)
        user.set_unusable_password()
        adapter.populate_user(request, sociallogin, common_fields)
        return sociallogin

    def extract_uid(self, data):
        """
        Extracts the unique user ID from `data`
        """
        raise NotImplementedError(
            'The provider must implement the `extract_uid()` method'
        )

    def extract_extra_data(self, data):
        return data

    def extract_basic_socialaccount_data(self, data):
        """
        Returns a tuple of basic/common social account data.
        For example: ('123', {'first_name': 'John'})
        """
        raise NotImplementedError

    def extract_common_fields(self, data):
        """
        For example:

{'fraction_non_alphanumeric': 0.08473645680819912, 'fraction_numerical': 0.0014641288433382138, 'mean_word_length': 2.590670170827858, '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': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7305994', 'n_tokens_mistral': 1547, 'n_tokens_neox': 1463, 'n_words': 353}