<commit_before>from django import forms
from django.contrib.auth.models import User
from django.core.validators import RegexValidator


class UsernameForm(forms.Form):

    """
    A form that asks for a username and email address.

    Used by social auth flows, which authenticate new users before they've had
    a chance to provide that info.
    """

    invalid_username_msg = (
        "Usernames may only contain alphanumeric characters, hyphens, and "
        "underscores, and must not begin or end with an a hyphen or "
        "underscore."
        )
    regex = r'^(?![\-\_])[\w\-\_]+(?<![\-\_])$'
    validate_username = RegexValidator(regex, invalid_username_msg)

    def validate_unique_username(value):
        try:
            User.objects.get(username=value.lower())
            raise forms.ValidationError(
                ('The username %(value)s is not available.'),
                params={'value': value},
            )
        except User.DoesNotExist:
            return True

    def validate_unique_email(value):
        try:
            User.objects.get(email=value.lower())
            raise forms.ValidationError(
                ('The email address %(value)s is in use by another account.'),
                params={'value': value},
            )
        except User.DoesNotExist:
            return True

    username = forms.CharField(
        label='DataHub Username',
        min_length=3,
        max_length=255,
        validators=[validate_username, validate_unique_username])
    email = forms.EmailField(<commit_msg>Validate username uniqueness in forms as well.<commit_after>from django import forms
from django.contrib.auth.models import User
from core.db.manager import DataHubManager
from django.core.validators import RegexValidator


class UsernameForm(forms.Form):

    """
    A form that asks for a username and email address.

    Used by social auth flows, which authenticate new users before they've had
    a chance to provide that info.
    """

    invalid_username_msg = (
        "Usernames may only contain alphanumeric characters, hyphens, and "
        "underscores, and must not begin or end with an a hyphen or "
        "underscore."
        )
    regex = r'^(?![\-\_])[\w\-\_]+(?<![\-\_])$'
    validate_username = RegexValidator(regex, invalid_username_msg)

    def validate_unique_username(value):
        username = value.lower()
        try:
            User.objects.get(username=username)
            raise forms.ValidationError(
                ('The username %(value)s is not available.'),
                params={'value': value},
            )
        except User.DoesNotExist:
            pass

        db_exists = DataHubManager.database_exists(username)
        user_exists = DataHubManager.user_exists(username)
        if db_exists or user_exists:
            raise forms.ValidationError(
                ('The username %(value)s is not available.'),
                params={'value': value},
            )
        return True


    def validate_unique_email(value):
        try:
            User.objects.get(email=value.lower())
            raise forms.ValidationError(
                ('The email address %(value)s is in use by another account.'),
                params={'value': value},
            )
        except User.DoesNotExist:
            return True

    username = forms.CharField(
        label='DataHub Username',
        min_length=3,
        max_length=255,
        validators=[validate_username, validate_unique_username])
    email = forms.EmailField(
{'fraction_non_alphanumeric': 0.09633802816901409, 'fraction_numerical': 0.0022535211267605635, 'mean_word_length': 2.2488563586459285, 'pattern_counts': {'":': 0, '<': 5, '<?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': '5768928', 'n_tokens_mistral': 963, 'n_tokens_neox': 930, 'n_words': 268}