You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@superset.apache.org by GitBox <gi...@apache.org> on 2019/09/30 17:50:36 UTC

[GitHub] [incubator-superset] SWoto edited a comment on issue #3083: Automatically create users from csv file

SWoto edited a comment on issue #3083: Automatically create users from csv file
URL: https://github.com/apache/incubator-superset/issues/3083#issuecomment-536656071
 
 
   @JulieRossi, @yqylovy 
   Could you please help me?
   
   I'm trying to ryn the code bellow:
   ```
   import requests
   from bs4 import BeautifulSoup as bs
   from bs4 import Comment
   
   class UseSupersetApi:
       def __init__(self, username=None, password=None):
           self.s = requests.Session()
           self.base_url = "http://123.45.67.890:8088/"
           self._csrf = self.getCSRF(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)
   
       def getCSRF(self, url_path):
           response = s.get(base_url)
           soup = bs(response.content, "html.parser")
           for tag in soup.find_all('input', id='csrf_token'):
               csrf_token = tag['value']
           return csrf_token
   
   superset = UseSupersetApi('admin', 'passoword')
   users = [] #some user dicts inside
   
   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']}
       print(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)
   ```
   
   But i always get the same error. Doesn't matter if i run inside the container, the server or in my PC...
   
   ```
   RuntimeError: Working outside of application context.
   
   This typically means that you attempted to use functionality that needed
   to interface with the current application object in some way. To solve
   this, set up an application context with app.app_context().  See the
   documentation for more information.
   ```
   
   Tried to remove the get_csrf_token but than i get error 400.

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


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org