<commit_before>from flask.ext.script import Manager, prompt_pass
from redash import models

manager = Manager(help="Users management commands. This commands assume single organization operation.")


@manager.option('email', help="email address of the user to grant admin to")
def grant_admin(email):
    try:
        user = models.User.get_by_email_and_org(email, models.Organization.get_by_slug('default'))

        user.groups.append('admin')
        user.save()

        print "User updated."
    except models.User.DoesNotExist:
        print "User [%s] not found." % email


@manager.option('email', help="User's email")
@manager.option('name', help="User's full name")
@manager.option('--admin', dest='is_admin', action="store_true", default=False, help="set user as admin")
@manager.option('--google', dest='google_auth', action="store_true", default=False, help="user uses Google Auth to login")
@manager.option('--password', dest='password', default=None, help="Password for users who don't use Google Auth (leave blank for prompt).")
@manager.option('--groups', dest='groups', default=None, help="Comma seperated list of groups (leave blank for default).")
def create(email, name, groups, is_admin=False, google_auth=False, password=None):
    print "Creating user (%s, %s)..." % (email, name)
    print "Admin: %r" % is_admin
    print "Login with Google Auth: %r\n" % google_auth

    org = models.Organization.get_by_slug('default')
    if isinstance(groups, basestring):
        groups= groups.split(',')
        groups.remove('') # in case it was empty string
        groups = [int(g) for g in groups]

    if groups is None:
        groups = [models.Group.get(models.Group.name=="default", models.Group.org==org).id]

    if is_admin:
        groups += [models.Group.get(models.Group.name=="admin", models.Group.org==org).id]

    user = models.User(email=email, name=name, groups=groups)
    if not google_auth:
        password = password or prompt_pass("Password")
        user.hash_password(password)

    try:
        user.save()
    except Exception, e:
        print "Failed creating user: %s" % e.message


@manager.option('email', help="email address of user to delete")
def delete(email):
    deleted_count = models.User.delete().where(models.User.email == email).execute()
    print "Deleted %d users." % deleted_count


@manager.option('password', help="<PASSWORD> the user")
@manager.option('email', help="email address of the user to change password for")
def password(email, password):
    try:
        user = models.User.get_by_email_and_org(email, models.Organization.get_by_slug('default'))

        user.hash_password(password)
        user.save()

        print "User updated."
    except models.User.DoesNotExist:
        print "User [%s] not found." % email


@manager.command
def list():
    """List all users"""
    for i, user in enumerate(models.User.select()):
        if i > 0:
            print "-"*20

        print "Id: {}\nName: {}\nEmail: {}".format(user.id, user.name.encode('utf-8'), user.email)
<commit_msg>Fix #745: when creating user from CLI, use default org.<commit_after>from flask.ext.script import Manager, prompt_pass
from redash import models

manager = Manager(help="Users management commands. This commands assume single organization operation.")


@manager.option('email', help="email address of the user to grant admin to")
def grant_admin(email):
    try:
        user = models.User.get_by_email_and_org(email, models.Organization.get_by_slug('default'))

        user.groups.append('admin')
        user.save()

        print "User updated."
    except models.User.DoesNotExist:
        print "User [%s] not found." % email


@manager.option('email', help="User's email")
@manager.option('name', help="User's full name")
@manager.option('--admin', dest='is_admin', action="store_true", default=False, help="set user as admin")
@manager.option('--google', dest='google_auth', action="store_true", default=False, help="user uses Google Auth to login")
@manager.option('--password', dest='password', default=None, help="Password for users who don't use Google Auth (leave blank for prompt).")
@manager.option('--groups', dest='groups', default=None, help="Comma seperated list of groups (leave blank for default).")
def create(email, name, groups, is_admin=False, google_auth=False, password=None):
    print "Creating user (%s, %s)..." % (email, name)
    print "Admin: %r" % is_admin
    print "Login with Google Auth: %r\n" % google_auth

    org = models.Organization.get_by_slug('default')
    if isinstance(groups, basestring):
        groups= groups.split(',')
        groups.remove('') # in case it was empty string
        groups = [int(g) for g in groups]

    if groups is None:
        groups = [models.Group.get(models.Group.name=="default", models.Group.org==org).id]

    if is_admin:
        groups += [models.Group.get(models.Group.name=="admin", models.Group.org==org).id]

    user = models.User(org=org, email=email, name=name, groups=groups)
    if not google_auth:
        password = password or prompt_pass("Password")
        user.hash_password(password)

    try:
        user.save()
    except Exception, e:
        print "Failed creating user: %s" % e.message


@manager.option('email', help="email address of user to delete")
def delete(email):
    deleted_count = models.User.delete().where(models.User.email == email).execute()
    print "Deleted %d users." % deleted_count


@manager.option('password', help="<PASSWORD>")
@manager.option('email', help="email address of the user to change password for")
def password(email, password):
    try:
        user = models.User.get_by_email_and_org(email, models.Organization.get_by_slug('default'))

        user.hash_password(password)
        user.save()

        print "User updated."
    except models.User.DoesNotExist:
        print "User [%s] not found." % email


@manager.command
def list():
    """List all users"""
    for i, user in enumerate(models.User.select()):
        if i > 0:
            print "-"*20

        print "Id: {}\nName: {}\nEmail: {}".format(user.id, user.name.encode('utf-8'), user.email)

{'fraction_non_alphanumeric': 0.14707792207792209, 'fraction_numerical': 0.0017857142857142857, 'mean_word_length': 3.768575851393189, 'pattern_counts': {'":': 0, '<': 5, '<?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': '2182162', 'n_tokens_mistral': 1867, 'n_tokens_neox': 1828, 'n_words': 516}