def check_username_for_new_account(person, username, machine_category):
    """ Check the new username for a new account. If the username  is
    in use, raises :py:exc:`UsernameTaken`.

    :param person: Owner of new account.
    :param username: Username to validate.
    :param machine_category: Machine category for new account.
    """

    query = Account.objects.filter(
        username__exact=username,
        machine_category=machine_category,
        date_deleted__isnull=True)

    if query.count() > 0:
        raise UsernameTaken(
            six.u('Username already in use on machine category %s.')
            % machine_category)

    if machine_category_account_exists(username, machine_category):
        raise UsernameTaken(
            six.u('Username is already in datastore for machine category %s.')
            % machine_category)

    return username
def validate_username_for_rename_person(username, person):
    """ Validate the new username to rename a person. If the username is
    invalid or in use, raises :py:exc:`UsernameInvalid` or
    :py:exc:`UsernameTaken`.

    :param username: Username to validate.
    :param person: We exclude this person when checking for duplicates.
    """

    # is the username valid?

    validate_username(username)

    # Check for existing people

    count = Person.objects.filter(username__exact=username) \
        .exclude(pk=person.pk).count()
    if count >= 1:
        raise UsernameTaken(
            six.u('The username is already taken by an existing person.'))

    # Check for existing accounts not owned by person.
    # If there is a conflicting account owned by person it doesn't matter.

    count = Account.objects.filter(username__exact=username) \
        .exclude(person=person).count()
    if count >= 1:
        raise UsernameTaken(
            six.u('The username is already taken by an existing account.'))

    # Check person datastore, in case username created outside Karaage.

    if global_person_exists(username):
        raise UsernameTaken(
            six.u('Username is already in external personal datastore.'))

    # Check account datastore, in case username created outside Karaage.

    for mc in MachineCategory.objects.all():
        # If there is an active account on this MC and we own it, it doesn't
        # matter
        count = Account.objects.filter(
            username__exact=username,
            person=person,
            machine_category=mc,
            date_deleted__isnull=True).count()
        if count == 0 and machine_category_account_exists(username, mc):
            raise UsernameTaken(
                six.u('Username is already in external account datastore.'))
def validate_username_for_rename_person(username, person):
    """ Validate the new username to rename a person. If the username is
    invalid or in use, raises :py:exc:`UsernameInvalid` or
    :py:exc:`UsernameTaken`.

    :param username: Username to validate.
    :param person: We exclude this person when checking for duplicates.
    """

    # is the username valid?

    validate_username(username)

    # Check for existing people

    count = Person.objects.filter(username__exact=username) \
        .exclude(pk=person.pk).count()
    if count >= 1:
        raise UsernameTaken(
            six.u('The username is already taken by an existing person.'))

    # Check for existing accounts not owned by person.
    # If there is a conflicting account owned by person it doesn't matter.

    count = Account.objects.filter(username__exact=username) \
        .exclude(person=person).count()
    if count >= 1:
        raise UsernameTaken(
            six.u('The username is already taken by an existing account.'))

    # Check person datastore, in case username created outside Karaage.

    if global_person_exists(username):
        raise UsernameTaken(
            six.u('Username is already in external personal datastore.'))

    # Check account datastore, in case username created outside Karaage.

    for mc in MachineCategory.objects.all():
        # If there is an active account on this MC and we own it, it doesn't
        # matter
        count = Account.objects.filter(username__exact=username,
                                       person=person,
                                       machine_category=mc,
                                       date_deleted__isnull=True).count()
        if count == 0 and machine_category_account_exists(username, mc):
            raise UsernameTaken(
                six.u('Username is already in external account datastore.'))
def validate_username_for_new_person(username):
    """ Validate the new username for a new person. If the username is invalid
    or in use, raises :py:exc:`UsernameInvalid` or :py:exc:`UsernameTaken`.

    :param username: Username to validate.
    """

    # is the username valid?

    validate_username(username)

    # Check for existing people

    count = Person.objects.filter(username__exact=username).count()
    if count >= 1:
        raise UsernameTaken(six.u(
            'The username is already taken. Please choose another. '
            'If this was the name of your old account please email %s')
            % settings.ACCOUNTS_EMAIL)

    # Check for existing accounts

    count = Account.objects.filter(username__exact=username).count()
    if count >= 1:
        raise UsernameTaken(six.u(
            'The username is already taken. Please choose another. '
            'If this was the name of your old account please email %s')
            % settings.ACCOUNTS_EMAIL)

    # Check person datastore, in case username created outside Karaage.

    if global_person_exists(username):
        raise UsernameTaken(
            six.u('Username is already in external personal datastore.'))

    # Check account datastore, in case username created outside Karaage.

    for mc in MachineCategory.objects.all():
        if machine_category_account_exists(username, mc):
            raise UsernameTaken(
                six.u('Username is already in external account datastore.'))

    return username
def validate_username_for_new_person(username):
    """ Validate the new username for a new person. If the username is invalid
    or in use, raises :py:exc:`UsernameInvalid` or :py:exc:`UsernameTaken`.

    :param username: Username to validate.
    """

    # is the username valid?

    validate_username(username)

    # Check for existing people

    count = Person.objects.filter(username__exact=username).count()
    if count >= 1:
        raise UsernameTaken(
            six.u('The username is already taken. Please choose another. '
                  'If this was the name of your old account please email %s') %
            settings.ACCOUNTS_EMAIL)

    # Check for existing accounts

    count = Account.objects.filter(username__exact=username).count()
    if count >= 1:
        raise UsernameTaken(
            six.u('The username is already taken. Please choose another. '
                  'If this was the name of your old account please email %s') %
            settings.ACCOUNTS_EMAIL)

    # Check person datastore, in case username created outside Karaage.

    if global_person_exists(username):
        raise UsernameTaken(
            six.u('Username is already in external personal datastore.'))

    # Check account datastore, in case username created outside Karaage.

    for mc in MachineCategory.objects.all():
        if machine_category_account_exists(username, mc):
            raise UsernameTaken(
                six.u('Username is already in external account datastore.'))

    return username
def check_username_for_new_account(person, username, machine_category):
    """ Check the new username for a new account. If the username  is
    in use, raises :py:exc:`UsernameTaken`.

    :param person: Owner of new account.
    :param username: Username to validate.
    :param machine_category: Machine category for new account.
    """

    query = Account.objects.filter(username__exact=username,
                                   machine_category=machine_category,
                                   date_deleted__isnull=True)

    if query.count() > 0:
        raise UsernameTaken(
            six.u('Username already in use on machine category %s.') %
            machine_category)

    if machine_category_account_exists(username, machine_category):
        raise UsernameTaken(
            six.u('Username is already in datastore for machine category %s.')
            % machine_category)

    return username