def set_login_view(app, login_view, blueprint=None):
    """
    Sets the login view for the app or blueprint. If a blueprint is passed,
    the login view is set for this blueprint on ``blueprint_login_views``.

    :param login_view: The user object to log in.
    :type login_view: str
    :param blueprint: The blueprint which this login view should be set on.
        Defaults to ``None``.
    :type blueprint: object
    """
    num_login_views = len(app.login_manager.blueprint_login_views)
    if blueprint is not None or num_login_views != 0:
        app.login_manager.blueprint_login_views[blueprint.name] = login_view
        if (app.login_manager.login_view is not None and None not in app.
            login_manager.blueprint_login_views):
            app.login_manager.blueprint_login_views[None
                ] = app.login_manager.login_view
        app.login_manager.login_view = None
    else:
        app.login_manager.login_view = login_view
