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 datastore, in case username created outside Karaage.

    count = Account.objects.filter(
        username__exact=username,
        person=person,
        date_deleted__isnull=True).count()
    if count == 0 and account_exists(username):
        raise UsernameTaken(
            six.u('Username is already in external account datastore.'))
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.