JulieRossi commented on issue #3083: Automatically create users from csv file
URL: 
https://github.com/apache/incubator-superset/issues/3083#issuecomment-437808161
 
 
   Hi @yqylovy, if it helps, here's how our code looks like : 
   Tool class to be able to use superset api (needs to be connected for some 
actions)
   ```
   class UseSupersetApi:
       def __init__(self, username=None, password=None):
           self.s = requests.Session()
           self.base_url = SUPERSET_URL
           self._csrf = get_csrf_token(self.s.get(self.url('login/')))
           self.headers = {'X-CSRFToken': self._csrf, 'Referer': 
self.url('login/')}
           # note: does not use headers because of flask_wtf.csrf.validate_csrf
           # if data is dict it is used as form and ends up empty but flask_wtf 
checks if data ...
           self.s.post(self.url('login/'),
                       data={'username': username, 'password': password, 
'csrf_token': self._csrf})
   
       def url(self, url_path):
           return self.base_url + url_path
   
       def get(self, url_path):
           return self.s.get(self.url(url_path), headers=self.headers)
   
       def post(self, url_path, data=None, json_data=None, **kwargs):
           kwargs.update({'url': self.url(url_path), 'headers': self.headers})
           if data:
               data['csrf_token'] = self._csrf
               kwargs['data'] = data
           if json_data:
               kwargs['json'] = json_data
           return self.s.post(**kwargs)
   ```
   And then we use it to create users : 
   ```
   superset = UseSupersetApi('admin', SUPERSET_ADMIN_MDP)
   
   for user in users:
       payload = {'first_name': user['first_name'],
                  'last_name': user['last_name'],
                  'username': user['username'],
                  'email': user['email'],
                  'active': True,
                  'conf_password': user['password'],
                  'password': user['password']}
       superset.post(url_path='users/api/create', json=payload)
   
   users_by_role = _group_users_by_role(users)
   for role in users_by_role:
       payload = {'users': users_by_role[role], 'role_name': role}
       superset.post(url_path='superset/update_role/', json=payload)
   ```
   Don't hesitate if you have any questions

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to