Fix CLI to work with organizations

This commit is contained in:
Arik Fraimovich 2016-01-06 15:14:09 +02:00
parent 224998c62a
commit 5e58818043
2 changed files with 38 additions and 30 deletions

View File

@ -6,6 +6,7 @@ from redash.query_runner import query_runners, validate_configuration
manager = Manager(help="Data sources management commands.") manager = Manager(help="Data sources management commands.")
@manager.command @manager.command
def list(): def list():
"""List currently configured data sources""" """List currently configured data sources"""
@ -27,6 +28,7 @@ def validate_data_source_options(type, options):
print "Error: invalid configuration." print "Error: invalid configuration."
exit() exit()
@manager.command @manager.command
def new(name=None, type=None, options=None): def new(name=None, type=None, options=None):
"""Create new data source""" """Create new data source"""
@ -82,7 +84,8 @@ def new(name=None, type=None, options=None):
data_source = models.DataSource.create(name=name, data_source = models.DataSource.create(name=name,
type=type, type=type,
options=options) options=options,
org=models.Organization.get_by_slug('default'))
print "Id: {}".format(data_source.id) print "Id: {}".format(data_source.id)

View File

@ -7,7 +7,7 @@ manager = Manager(help="Users management commands. This commands assume single o
@manager.option('email', help="email address of the user to grant admin to") @manager.option('email', help="email address of the user to grant admin to")
def grant_admin(email): def grant_admin(email):
try: try:
user = models.User.get_by_email(email) user = models.User.get_by_email_and_org(email, models.Organization.get_by_slug('default'))
user.groups.append('admin') user.groups.append('admin')
user.save() user.save()
@ -17,33 +17,38 @@ def grant_admin(email):
print "User [%s] not found." % email print "User [%s] not found." % email
# TODO(@arikfr): This needs to be updated to the new org/groups scheme to work. @manager.option('email', help="User's email")
# @manager.option('email', help="User's email") @manager.option('name', help="User's full name")
# @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('--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('--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('--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).")
# @manager.option('--groups', dest='groups', default=models.User.DEFAULT_GROUPS, help="Comma seperated list of groups (leave blank for default).") def create(email, name, groups, is_admin=False, google_auth=False, password=None):
# def create(email, name, groups, is_admin=False, google_auth=False, password=None): print "Creating user (%s, %s)..." % (email, name)
# print "Creating user (%s, %s)..." % (email, name) print "Admin: %r" % is_admin
# print "Admin: %r" % is_admin print "Login with Google Auth: %r\n" % google_auth
# print "Login with Google Auth: %r\n" % google_auth
# if isinstance(groups, basestring): org = models.Organization.get_by_slug('default')
# groups= groups.split(',') if isinstance(groups, basestring):
# groups.remove('') # in case it was empty string groups= groups.split(',')
# groups.remove('') # in case it was empty string
# if is_admin: groups = [int(g) for g in groups]
# groups += ['admin']
# if groups is None:
# user = models.User(email=email, name=name, groups=groups) groups = [models.Group.get(models.Group.name=="default", models.Group.org==org).id]
# if not google_auth:
# password = password or prompt_pass("Password") if is_admin:
# user.hash_password(password) groups += [models.Group.get(models.Group.name=="admin", models.Group.org==org).id]
#
# try: user = models.User(email=email, name=name, groups=groups)
# user.save() if not google_auth:
# except Exception, e: password = password or prompt_pass("Password")
# print "Failed creating user: %s" % e.message 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") @manager.option('email', help="email address of user to delete")
@ -56,7 +61,7 @@ def delete(email):
@manager.option('email', help="email address of the user to change password for") @manager.option('email', help="email address of the user to change password for")
def password(email, password): def password(email, password):
try: try:
user = models.User.get_by_email(email) user = models.User.get_by_email_and_org(email, models.Organization.get_by_slug('default'))
user.hash_password(password) user.hash_password(password)
user.save() user.save()