from flask_script import Manager, prompt_pass
from redash import models

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

@manager.option('name', help="Group's name")
@manager.option('--permissions', dest='permissions', default=None, help="Comma seperated list of permissions ('create_dashboard', 'create_query', 'edit_dashboard', 'edit_query', 'view_query', 'view_source', 'execute_query', 'list_users', 'schedule_query', 'list_dashboards', 'list_alerts', 'list_data_sources') (leave blank for default).")
def create(name, permissions=None):
  print "Creating group (%s)..." % (name)

  org = models.Organization.get_by_slug('default')
  
  permissions = extract_permissions_string(permissions)
  
  print "permissions: [%s]" % ",".join(permissions)
    
  try:
    models.Group.create(name=name, org=org, permissions=permissions)
  except Exception, e:
    print "Failed create group: %s" % e.message  

@manager.option('id', help="Group's id")
@manager.option('--permissions', dest='permissions', default=None, help="Comma seperated list of permissions ('create_dashboard', 'create_query', 'edit_dashboard', 'edit_query', 'view_query', 'view_source', 'execute_query', 'list_users', 'schedule_query', 'list_dashboards', 'list_alerts', 'list_data_sources') (leave blank for default).")
def change_permissions(id, permissions=None):
  print "Change permissions of group %s ..." % id
  
  try:
    group = models.Group.get_by_id(id)
  except models.Group.DoesNotExist:
    print "User [%s] not found." % id
    return

  permissions = extract_permissions_string(permissions)    
  print "current permissions [%s] will be modify to [%s]" % (",".join(group.permissions), ",".join(permissions))
      
  group.permissions = permissions
  
  try:
    group.save()
  except Exception, e:
    print "Failed change permission: %s" % e.message  
    
    
def extract_permissions_string(permissions):
  if permissions is None:
    permissions = models.Group.DEFAULT_PERMISSIONS
  else:
    permissions = permissions.split(',')
    permissions = [p.strip() for p in permissions]
  return permissions
@@ -586,32 +586,34 @@
 missions=None):%0A
+  
   print %22Creatin
@@ -639,16 +639,18 @@
 (name)%0A%0A
+  
   org = 
@@ -692,19 +692,19 @@
 fault')%0A
+%0A
   
-%0A
   permis
@@ -751,19 +751,19 @@
 ssions)%0A
+%0A
   
-%0A
   print 
@@ -810,20 +810,22 @@
 ns)%0A
- 
+%0A
    
-%0A 
  try:%0A
+    
     
@@ -889,16 +889,18 @@
 ssions)%0A
+  
   except
@@ -906,32 +906,36 @@
 t Exception, e:%0A
+    
     print %22Faile
@@ -1396,16 +1396,18 @@
 =None):%0A
+  
   print 
@@ -1448,19 +1448,19 @@
 .%22 %25 id%0A
+%0A
   
-%0A
   try:%0A 
@@ -1458,24 +1458,28 @@
    try:%0A    
+ 
+   
 group = mode
@@ -1499,24 +1499,26 @@
 by_id(id)%0A  
+ 
+ 
 except model
@@ -1539,24 +1539,28 @@
 tExist:%0A    
+   
+ 
 print %22User 
@@ -1589,16 +1589,20 @@
     
+   
+ 
 return%0A%0A
   pe
@@ -1597,16 +1597,18 @@
 return%0A%0A
+  
   permis
@@ -1659,16 +1659,18 @@
 ns)    %0A
+  
   print 
@@ -1778,15 +1778,11 @@
 s))%0A
-    
+%0A
   
-%0A
   gr
@@ -1811,19 +1811,19 @@
 issions%0A
+%0A
   
-%0A
   try:%0A 
@@ -1821,24 +1821,28 @@
    try:%0A    
+   
+ 
 group.save()
@@ -1842,16 +1842,18 @@
 .save()%0A
+  
   except
@@ -1863,24 +1863,28 @@
 ception, e:%0A
+    
     print %22F
@@ -1926,25 +1926,17 @@
 ssage  %0A
-    %0A    
+%0A
 %0Adef ext
@@ -1973,16 +1973,18 @@
 sions):%0A
+  
   if per
@@ -2001,16 +2001,20 @@
 s None:%0A
+    
     perm
@@ -2056,16 +2056,18 @@
 ISSIONS%0A
+  
   else:%0A
@@ -2066,24 +2066,28 @@
   else:%0A    
+  
+  
 permissions 
@@ -2111,16 +2111,20 @@
 it(',')%0A
+    
     perm
@@ -2166,16 +2166,18 @@
 ssions%5D%0A
+  
   return

modify indent size