Skip to content

Commit 0aa0d63

Browse files
committed
fix issue #33
1 parent e75a62c commit 0aa0d63

3 files changed

Lines changed: 46 additions & 17 deletions

File tree

metabase_api/metabase_api.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,16 +1080,27 @@ def clone_card(self, card_id,
10801080

10811081
# simple/custom questions
10821082
elif card_info['dataset_query']['type'] == 'query':
1083-
filters_data = card_info['dataset_query']['query']
1083+
1084+
query_data = card_info['dataset_query']['query']
1085+
10841086
# change the underlying table for the card
1085-
filters_data['source-table'] = target_table_id
1086-
# change filters source
1087-
for index, item in enumerate(filters_data['filter']):
1088-
if type(item) == list:
1089-
column_id = item[1][1]
1090-
column_name = source_table_col_id_name_mapping[column_id]
1091-
target_col_id = target_table_col_name_id_mapping[column_name]
1092-
card_info['dataset_query']['query']['filter'][index][1][1] = target_col_id
1087+
query_data['source-table'] = target_table_id
1088+
1089+
# transform to string so it is easier to replace the column IDs
1090+
query_data_str = str(query_data)
1091+
1092+
# find column IDs
1093+
import re
1094+
res = re.findall("\['field', .*?\]", query_data_str)
1095+
source_column_IDs = [ eval(i)[1] for i in res ]
1096+
1097+
# replace column IDs from old table with the column IDs from new table
1098+
for source_col_id in source_column_IDs:
1099+
source_col_name = source_table_col_id_name_mapping[source_col_id]
1100+
target_col_id = target_table_col_name_id_mapping[source_col_name]
1101+
query_data_str = query_data_str.replace("['field', {}, ".format(source_col_id), "['field', {}, ".format(target_col_id))
1102+
1103+
card_info['dataset_query']['query'] = eval(query_data_str)
10931104

10941105
new_card_json = {}
10951106
for key in ['dataset_query', 'display', 'visualization_settings']:

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name="metabase-api",
8-
version="0.2.14.2",
8+
version="0.2.15",
99
author="Vahid Vaezian",
1010
author_email="vahid.vaezian@gmail.com",
1111
description="A Python Wrapper for Metabase API",

tests/test_metabase_api.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ def test_get_table_metadata(self):
146146
self.assertEqual(table_info['fields'][0]['name'], 'col1')
147147

148148

149+
149150
def test_get_columns_name_id(self):
150151
name_id_mapping = mb.get_columns_name_id(table_id=101)
151152
self.assertEqual(name_id_mapping['col1'], 490)
@@ -154,6 +155,7 @@ def test_get_columns_name_id(self):
154155
self.assertEqual(id_name_mapping[490], 'col1')
155156

156157

158+
157159
def test_friendly_names_is_disabled(self):
158160
res = mb.friendly_names_is_disabled()
159161
self.assertEqual(res, True)
@@ -215,6 +217,7 @@ def test_create_card(self):
215217
Metabase_API_Test.cleanup_objects['card'].extend([ res1['id'], res3['id'], res3['id'] ])
216218

217219

220+
218221
def test_create_collection(self):
219222
t = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
220223
res = mb.create_collection(f'test_create_collection {t}', parent_collection_id=28, return_results=True)
@@ -227,6 +230,7 @@ def test_create_collection(self):
227230
Metabase_API_Test.cleanup_objects['collection'].append(res['id'])
228231

229232

233+
230234
def test_create_segment(self):
231235
t = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
232236
res = mb.create_segment(segment_name='test_create_segment_{}'.format(t), table_name='test_table', column_name='col2', column_values=[1, 2], return_segment=True)
@@ -235,6 +239,7 @@ def test_create_segment(self):
235239
Metabase_API_Test.cleanup_objects['segment'].append(res['id'])
236240

237241

242+
238243
def test_copy_card(self):
239244
t = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
240245
newCard_id = mb.copy_card(source_card_id=166, destination_collection_id=29, destination_card_name='test_copy_card_{}'.format(t))
@@ -284,11 +289,13 @@ def test_search(self):
284289
def test_get_card_data(self):
285290
# json
286291
res = mb.get_card_data(card_id=166)
287-
json_data = [{'col1': 'row1 cell1', 'col2': 1},
288-
{'col1': None, 'col2': 2},
289-
{'col1': 'row3 cell1', 'col2': None},
290-
{'col1': None, 'col2': None},
291-
{'col1': 'row5 cell1', 'col2': 5}]
292+
json_data = [
293+
{'col1': 'row1 cell1', 'col2': 1},
294+
{'col1': None, 'col2': 2},
295+
{'col1': 'row3 cell1', 'col2': None},
296+
{'col1': None, 'col2': None},
297+
{'col1': 'row5 cell1', 'col2': 5}
298+
]
292299
self.assertEqual(res, json_data)
293300

294301
# csv
@@ -302,14 +309,25 @@ def test_get_card_data(self):
302309
self.assertEqual(res, filtered_data)
303310

304311

312+
305313
def test_clone_card(self):
306314
# native question
307315
res = mb.clone_card(273, 101, 103, new_card_name='test_clone_native', new_card_collection_id=29, return_card=True)
308316
# simple/custom question
309-
res2 = mb.clone_card(274, 101, 103, new_card_name='test_clone_simple', new_card_collection_id=29, return_card=True)
317+
res2 = mb.clone_card(274, 101, 103, new_card_name='test_clone_simple1', new_card_collection_id=29, return_card=True)
318+
res3 = mb.clone_card(491, 101, 103, new_card_name='test_clone_simple2', new_card_collection_id=29, return_card=True)
319+
expected_res3_query = { 'database': 5,
320+
'query': {'source-table': 103,
321+
'aggregation': [['avg', ['field', 493, None]]],
322+
'breakout': [['field', 494, None]],
323+
'order-by': [['desc', ['aggregation', 0, None]]]
324+
},
325+
'type': 'query'
326+
}
327+
self.assertEqual(res3['dataset_query'], expected_res3_query)
310328

311329
# add to cleanup list
312-
Metabase_API_Test.cleanup_objects['card'].extend([res['id'], res2['id']])
330+
Metabase_API_Test.cleanup_objects['card'].extend([res['id'], res2['id'], res3['id']])
313331

314332

315333

0 commit comments

Comments
 (0)