import json


import click
from flask.cli import FlaskGroup, run_command
from flask import current_app

from redash import create_app, settings, __version__
from redash.cli import users, groups, database, data_sources, organization
from redash.monitor import get_status


def create(group):
    app = current_app or create_app()
    group.app = app

    @app.shell_context_processor
    def shell_context():
        from redash import models
        return dict(models=models)

    return app


@click.group(cls=FlaskGroup, create_app=create)
def manager():
    """Management script for Redash"""


manager.add_command(database.manager, "database")
manager.add_command(users.manager, "users")
manager.add_command(groups.manager, "groups")
manager.add_command(data_sources.manager, "ds")
manager.add_command(organization.manager, "org")
manager.add_command(run_command, "runserver")


@manager.command()
def version():
    """Displays Redash version."""
    print __version__


@manager.command()
def status():
    print json.dumps(get_status(), indent=2)


@manager.command()
def check_settings():
    """Show the settings as Redash sees them (useful for debugging)."""
    for name, item in settings.all_settings().iteritems():
        print "{} = {}".format(name, item)


@manager.command()
@click.argument('email', default=settings.MAIL_DEFAULT_SENDER, required=False)
def send_test_mail(email=None):
    """
    Send test message to EMAIL (default: the address you defined in MAIL_DEFAULT_SENDER)
    """
    from redash import mail
    from flask_mail import Message

    if email is None:
        email = settings.MAIL_DEFAULT_SENDER

    mail.send(Message(subject="Test Message from Redash", recipients=[email],
                      body="Test message."))


@@ -1758,8 +1758,495 @@
 ge.%22))%0A%0A
+%0A@manager.command()%0Adef ipython():%0A    %22%22%22Starts IPython shell instead of the default Python shell.%22%22%22%0A    import sys%0A    import IPython%0A    from flask.globals import _app_ctx_stack%0A    app = _app_ctx_stack.top.app%0A%0A    banner = 'Python %25s on %25s%5CnIPython: %25s%5CnRedash version: %25s%5Cn' %25 (%0A        sys.version,%0A        sys.platform,%0A        IPython.__version__,%0A        __version__%0A    )%0A%0A    ctx = %7B%7D%0A    ctx.update(app.make_shell_context())%0A%0A    IPython.embed(banner1=banner, user_ns=ctx)%0A

Add CLI command to open IPython shell