Skip to content

Commit

Permalink
Some dashboard import/export fixes. (#1340)
Browse files Browse the repository at this point in the history
  • Loading branch information
bkyryliuk authored Oct 13, 2016
1 parent 5bea398 commit 11a8e35
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 9 deletions.
4 changes: 2 additions & 2 deletions caravel/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ def import_obj(cls, slc_to_import, import_time=None):
slc.params_dict['remote_id'] == slc_to_import.id):
slc_to_override = slc

slc_to_import.id = None
slc_to_import = slc_to_import.copy()
params = slc_to_import.params_dict
slc_to_import.datasource_id = SourceRegistry.get_datasource_by_name(
session, slc_to_import.datasource_type, params['datasource_name'],
Expand Down Expand Up @@ -400,7 +400,7 @@ class Dashboard(Model, AuditMixinNullable, ImportMixin):
owners = relationship("User", secondary=dashboard_user)

export_fields = ('dashboard_title', 'position_json', 'json_metadata',
'description', 'css', 'slug', 'slices')
'description', 'css', 'slug')

def __repr__(self):
return self.dashboard_title
Expand Down
1 change: 1 addition & 0 deletions caravel/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ def init(caravel):
'UserDBModelView',
'SQL Lab',
'AccessRequestsModelView',
'Manage',
])

ADMIN_ONLY_PERMISSIONS = set([
Expand Down
6 changes: 1 addition & 5 deletions caravel/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1224,17 +1224,13 @@ def import_dashboards(self):
"""Overrides the dashboards using pickled instances from the file."""
f = request.files.get('file')
if request.method == 'POST' and f:
filename = secure_filename(f.filename)
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
f.save(filepath)
current_tt = int(time.time())
data = pickle.load(open(filepath, 'rb'))
data = pickle.load(f)
for table in data['datasources']:
models.SqlaTable.import_obj(table, import_time=current_tt)
for dashboard in data['dashboards']:
models.Dashboard.import_obj(
dashboard, import_time=current_tt)
os.remove(filepath)
db.session.commit()
return redirect('/dashboardmodelview/list/')
return self.render_template('caravel/import_dashboards.html')
Expand Down
8 changes: 6 additions & 2 deletions tests/import_export_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ def assert_table_equals(self, expected_ds, actual_ds):
set([m.metric_name for m in actual_ds.metrics]))

def assert_slice_equals(self, expected_slc, actual_slc):
self.assertEquals(actual_slc.datasource.perm, actual_slc.perm)
self.assertEquals(expected_slc.slice_name, actual_slc.slice_name)
self.assertEquals(
expected_slc.datasource_type, actual_slc.datasource_type)
Expand Down Expand Up @@ -201,7 +200,9 @@ def test_export_2_dashboards(self):
def test_import_1_slice(self):
expected_slice = self.create_slice('Import Me', id=10001);
slc_id = models.Slice.import_obj(expected_slice, import_time=1989)
self.assert_slice_equals(expected_slice, self.get_slice(slc_id))
slc = self.get_slice(slc_id)
self.assertEquals(slc.datasource.perm, slc.perm)
self.assert_slice_equals(expected_slice, slc)

table_id = self.get_table_by_name('wb_health_population').id
self.assertEquals(table_id, self.get_slice(slc_id).datasource_id)
Expand All @@ -218,9 +219,12 @@ def test_import_2_slices_for_same_table(self):
imported_slc_2 = self.get_slice(slc_id_2)
self.assertEquals(table_id, imported_slc_1.datasource_id)
self.assert_slice_equals(slc_1, imported_slc_1)
self.assertEquals(imported_slc_1.datasource.perm, imported_slc_1.perm)


self.assertEquals(table_id, imported_slc_2.datasource_id)
self.assert_slice_equals(slc_2, imported_slc_2)
self.assertEquals(imported_slc_2.datasource.perm, imported_slc_2.perm)

def test_import_slices_for_non_existent_table(self):
with self.assertRaises(IndexError):
Expand Down

0 comments on commit 11a8e35

Please sign in to comment.