<commit_before>from uuid import uuid4

from django.conf import settings

from social_auth.models import User
from social_auth.backends.pipeline import USERNAME, USERNAME_MAX_LENGTH, warn_setting
from social_auth.signals import socialauth_not_registered, \
                                socialauth_registered, \
                                pre_update


def get_username(details, user=None, *args, **kwargs):
    """Return an username for new user. Return current user username
    if user was given.
    """
    if user:
        return {'username': user.username}

    warn_setting('SOCIAL_AUTH_FORCE_RANDOM_USERNAME', 'get_username')
    warn_setting('SOCIAL_AUTH_DEFAULT_USERNAME', 'get_username')
    warn_setting('SOCIAL_AUTH_UUID_LENGTH', 'get_username')
    warn_setting('SOCIAL_AUTH_USERNAME_FIXER', 'get_username')

    if getattr(settings, 'SOCIAL_AUTH_FORCE_RANDOM_USERNAME', False):
        username = uuid4().get_hex()
    elif details.get(USERNAME):
        username = details[USERNAME]
    elif settings.hasattr('SOCIAL_AUTH_DEFAULT_USERNAME'):
        username = settings.SOCIAL_AUTH_DEFAULT_USERNAME
        if callable(username):
            username = username()
    else:
        username = uuid4().get_hex()

    uuid_lenght = getattr(settings, 'SOCIAL_AUTH_UUID_LENGTH', 16)
    username_fixer = getattr(settings, 'SOCIAL_AUTH_USERNAME_FIXER',
                             lambda u: u)

    short_username = username[:USERNAME_MAX_LENGTH - uuid_lenght]
    final_username = None

    while True:
        final_username = username_fixer(username)[:USERNAME_MAX_LENGTH]

        try:
            User.objects.get(username=final_username)
        except User.DoesNotExist:
            break
        else:
            # User with same username already exists, generate a unique
            # username for current user using username as base but adding
            # a unique hash at the end. Original username is cut to avoid
            # the field max_length.
            username = short_username + uuid4().get_hex()[:uuid_lenght]
    return {'username': final_username}


def create_user(backend, details, response, uid, username, user=None, *args,
                **kwargs):
    """Create user. Depends on get_username pipeline."""
    if user:
        return {'user': user}
    if not username:
        return None
    import pdb; pdb.set_trace()
    warn_setting('SOCIAL_AUTH_CREATE_USERS', 'create_user')
    if not getattr(settings, 'SOCIAL_AUTH_CREATE_USERS', True):
        #Added by brianmckinney to allow us to only invite manly users
        invite_key = kwargs['request'].session.get('invite', False)
        from invite.models import Invitation
        import pdb; pdb.set_trace()
        if invite_key:
           try:
              invite = Invitation.objects.get(key=invite_key)              
              email = details.get('email')
              return {
                 'user': User.objects.create_user(username=username, email=email),
                 'is_new': True
              }
           except ObjectDoesNotExist:
              pass                                        
        # Send signal for cases where tracking failed registering is useful.<commit_msg>Removing debug statements<commit_after>from uuid import uuid4

from django.conf import settings

from social_auth.models import User
from social_auth.backends.pipeline import USERNAME, USERNAME_MAX_LENGTH, warn_setting
from social_auth.signals import socialauth_not_registered, \
                                socialauth_registered, \
                                pre_update


def get_username(details, user=None, *args, **kwargs):
    """Return an username for new user. Return current user username
    if user was given.
    """
    if user:
        return {'username': user.username}

    warn_setting('SOCIAL_AUTH_FORCE_RANDOM_USERNAME', 'get_username')
    warn_setting('SOCIAL_AUTH_DEFAULT_USERNAME', 'get_username')
    warn_setting('SOCIAL_AUTH_UUID_LENGTH', 'get_username')
    warn_setting('SOCIAL_AUTH_USERNAME_FIXER', 'get_username')

    if getattr(settings, 'SOCIAL_AUTH_FORCE_RANDOM_USERNAME', False):
        username = uuid4().get_hex()
    elif details.get(USERNAME):
        username = details[USERNAME]
    elif settings.hasattr('SOCIAL_AUTH_DEFAULT_USERNAME'):
        username = settings.SOCIAL_AUTH_DEFAULT_USERNAME
        if callable(username):
            username = username()
    else:
        username = uuid4().get_hex()

    uuid_lenght = getattr(settings, 'SOCIAL_AUTH_UUID_LENGTH', 16)
    username_fixer = getattr(settings, 'SOCIAL_AUTH_USERNAME_FIXER',
                             lambda u: u)

    short_username = username[:USERNAME_MAX_LENGTH - uuid_lenght]
    final_username = None

    while True:
        final_username = username_fixer(username)[:USERNAME_MAX_LENGTH]

        try:
            User.objects.get(username=final_username)
        except User.DoesNotExist:
            break
        else:
            # User with same username already exists, generate a unique
            # username for current user using username as base but adding
            # a unique hash at the end. Original username is cut to avoid
            # the field max_length.
            username = short_username + uuid4().get_hex()[:uuid_lenght]
    return {'username': final_username}


def create_user(backend, details, response, uid, username, user=None, *args,
                **kwargs):
    """Create user. Depends on get_username pipeline."""
    if user:
        return {'user': user}
    if not username:
        return None

    warn_setting('SOCIAL_AUTH_CREATE_USERS', 'create_user')
    if not getattr(settings, 'SOCIAL_AUTH_CREATE_USERS', True):
        ################################################################
        #Added by brianmckinney to allow us to only invite manly users
        ###############################################################
        from invite.models import Invitation

        invite_key = kwargs['request'].session.get('invite', False)
        
        if invite_key:
           try:
              invite = Invitation.objects.get(key=invite_key)              
              email = details.get('email')
              return {
                 'user': User.objects.create_user(username=username, email=email),
                 'is_new': True
              }
           except Invitation.DoesNotExist:
              pass                                        
        ############## End edit ###################################
        # Send signal for cases where tracking failed registering is useful.
{'fraction_non_alphanumeric': 0.10582891748675247, 'fraction_numerical': 0.0018168054504163512, 'mean_word_length': 2.538296732726299, '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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4538548', 'n_tokens_mistral': 1776, 'n_tokens_neox': 1760, 'n_words': 445}