dashboard_import_export_util.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. # pylint: disable=C,R,W
  2. from __future__ import absolute_import
  3. from __future__ import division
  4. from __future__ import print_function
  5. from __future__ import unicode_literals
  6. import json
  7. import logging
  8. import time
  9. from superset import utils
  10. from superset.models.core import Dashboard
  11. def import_dashboards(session, data_stream, import_time=None):
  12. """Imports dashboards from a stream to databases"""
  13. current_tt = int(time.time())
  14. import_time = current_tt if import_time is None else import_time
  15. data = json.loads(data_stream.read(), object_hook=utils.decode_dashboards)
  16. # TODO: import DRUID datasources
  17. for table in data['datasources']:
  18. type(table).import_obj(table, import_time=import_time)
  19. session.commit()
  20. for dashboard in data['dashboards']:
  21. Dashboard.import_obj(
  22. dashboard, import_time=import_time)
  23. session.commit()
  24. def export_dashboards(session):
  25. """Returns all dashboards metadata as a json dump"""
  26. logging.info('Starting export')
  27. dashboards = session.query(Dashboard)
  28. dashboard_ids = []
  29. for dashboard in dashboards:
  30. dashboard_ids.append(dashboard.id)
  31. data = Dashboard.export_dashboards(dashboard_ids)
  32. return data