<commit_before>from django.core.exceptions import ValidationError
from django.core.validators import validate_email
from django.utils.translation import gettext as _

from corehq.apps.users.util import is_username_available
from corehq.apps.users.views.mobile import BAD_MOBILE_USERNAME_REGEX


def validate_mobile_username(username, domain, is_unique=True):
    """
    Raises a ValidationError if any issue with the complete username is encountered
    :param username: str, expects complete username ('<EMAIL>')
    :param domain: str, required
    :param is_unique: if true then username should not already exist.
    """
    if not username:
        raise ValidationError(_("Username is required."))

    _validate_complete_username(username, domain)

    if is_unique:
        if not is_username_available(username):
            raise ValidationError(_("Username '{}' is already taken or reserved.").format(username))


def _validate_complete_username(username, domain):
    """
    Raises a ValidationError if the username is invalid
    :param username: expects str formatted like '<EMAIL>'
    """
    try:
        validate_email(username)
    except ValidationError:
        raise ValidationError(_("Username '{}' must be a valid email address.").format(username))

    email_username, email_domain = username.split('@')
    if BAD_MOBILE_USERNAME_REGEX.search(email_username) is not None:
        raise ValidationError(
            _("The username component '{}' of '{}' may not contain special characters.").format(
                email_username, username)
        )

    expected_domain = f"{domain}.commcarehq.org"
    if email_domain != expected_domain:
        raise ValidationError(
            _("The username email domain '@{}' should be '@{}'.").format(email_domain, expected_domain))
<commit_msg>unstash<commit_after>from django.core.exceptions import ValidationError
from django.core.validators import validate_email
from django.utils.translation import gettext as _

from corehq.apps.users.util import is_username_available
from corehq.apps.users.views.mobile import BAD_MOBILE_USERNAME_REGEX


def validate_mobile_username(username, domain, is_unique=True):
    """
    Raises a ValidationError if any issue with the complete username is encountered
    :param username: str, expects complete username ('<EMAIL>')
    :param domain: str, required
    :param is_unique: if true then username should not already exist.
    """
    if not username:
        raise ValidationError(_("Username is required."))

    _validate_complete_username(username, domain)

    if is_unique and not is_username_available(username):
            raise ValidationError(_("Username '{}' is already taken or reserved.").format(username))


def _validate_complete_username(username, domain):
    """
    Raises a ValidationError if the username is invalid
    :param username: expects str formatted like '<EMAIL>'
    """
    try:
        validate_email(username)
    except ValidationError:
        raise ValidationError(_("Username '{}' must be a valid email address.").format(username))

    email_username, email_domain = username.split('@')
    if BAD_MOBILE_USERNAME_REGEX.search(email_username) is not None:
        raise ValidationError(
            _("The username component '{}' of '{}' may not contain special characters.").format(
                email_username, username)
        )

    expected_domain = f"{domain}.commcarehq.org"
    if email_domain != expected_domain:
        raise ValidationError(
            _("The username email domain '@{}' should be '@{}'.").format(email_domain, expected_domain))

{'fraction_non_alphanumeric': 0.0924903368304804, 'fraction_numerical': 0.0, 'mean_word_length': 3.8242343541944073, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '3721818', 'n_tokens_mistral': 983, 'n_tokens_neox': 941, 'n_words': 290}