Skip to content

Commit f983a60

Browse files
committed
fix issue #59 | improve docs styling | increase waiting time for unittest mb setup
1 parent 1c719cf commit f983a60

File tree

6 files changed

+109
-132
lines changed

6 files changed

+109
-132
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 3.4.5
2+
### Changed
3+
- Fix [#59](https://github.com/vvaezian/metabase_api_python/issues/59)
4+
- Improve docs styling
5+
16
## 3.4.4
27
### Changed
38
- Make sure the provided API key is correct.

metabase_api/copy_methods.py

Lines changed: 53 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@ def copy_card(self, source_card_name=None, source_card_id=None,
77
"""
88
Copy the card with the given name/id to the given destination collection.
99
10-
Keyword arguments:
11-
source_card_name -- name of the card to copy (default None)
12-
source_card_id -- id of the card to copy (default None)
13-
source_collection_name -- name of the collection the source card is located in (default None)
14-
source_collection_id -- id of the collection the source card is located in (default None)
15-
destination_card_name -- name used for the card in destination (default None).
10+
Parameters
11+
----------
12+
source_card_name : name of the card to copy (default None)
13+
source_card_id : id of the card to copy (default None)
14+
source_collection_name : name of the collection the source card is located in (default None)
15+
source_collection_id : id of the collection the source card is located in (default None)
16+
destination_card_name : name used for the card in destination (default None).
1617
If None, it will use the name of the source card + postfix.
17-
destination_collection_name -- name of the collection to copy the card to (default None)
18-
destination_collection_id -- id of the collection to copy the card to (default None)
19-
postfix -- if destination_card_name is None, adds this string to the end of source_card_name
18+
destination_collection_name : name of the collection to copy the card to (default None)
19+
destination_collection_id : id of the collection to copy the card to (default None)
20+
postfix : if destination_card_name is None, adds this string to the end of source_card_name
2021
to make destination_card_name
2122
"""
2223
### Making sure we have the data that we need
@@ -66,16 +67,17 @@ def copy_pulse(self, source_pulse_name=None, source_pulse_id=None,
6667
"""
6768
Copy the pulse with the given name/id to the given destination collection.
6869
69-
Keyword arguments:
70-
source_pulse_name -- name of the pulse to copy (default None)
71-
source_pulse_id -- id of the pulse to copy (default None)
72-
source_collection_name -- name of the collection the source card is located in (default None)
73-
source_collection_id -- id of the collection the source card is located in (default None)
74-
destination_pulse_name -- name used for the pulse in destination (default None).
70+
Parameters
71+
----------
72+
source_pulse_name : name of the pulse to copy (default None)
73+
source_pulse_id : id of the pulse to copy (default None)
74+
source_collection_name : name of the collection the source card is located in (default None)
75+
source_collection_id : id of the collection the source card is located in (default None)
76+
destination_pulse_name : name used for the pulse in destination (default None).
7577
If None, it will use the name of the source pulse + postfix.
76-
destination_collection_name -- name of the collection to copy the pulse to (default None)
77-
destination_collection_id -- id of the collection to copy the pulse to (default None)
78-
postfix -- if destination_pulse_name is None, adds this string to the end of source_pulse_name
78+
destination_collection_name : name of the collection to copy the pulse to (default None)
79+
destination_collection_id : id of the collection to copy the pulse to (default None)
80+
postfix : if destination_pulse_name is None, adds this string to the end of source_pulse_name
7981
to make destination_pulse_name
8082
"""
8183
### Making sure we have the data that we need
@@ -115,24 +117,23 @@ def copy_dashboard(self, source_dashboard_name=None, source_dashboard_id=None,
115117
source_collection_name=None, source_collection_id=None,
116118
destination_dashboard_name=None,
117119
destination_collection_name=None, destination_collection_id=None,
118-
deepcopy=False, postfix=''):
120+
deepcopy=False, postfix='', collection_position=1, description=''):
119121
"""
120122
Copy the dashboard with the given name/id to the given destination collection.
121123
122-
Keyword arguments:
123-
source_dashboard_name -- name of the dashboard to copy (default None)
124-
source_dashboard_id -- id of the dashboard to copy (default None)
125-
source_collection_name -- name of the collection the source dashboard is located in (default None)
126-
source_collection_id -- id of the collection the source dashboard is located in (default None)
127-
destination_dashboard_name -- name used for the dashboard in destination (default None).
128-
If None, it will use the name of the source dashboard + postfix.
129-
destination_collection_name -- name of the collection to copy the dashboard to (default None)
130-
destination_collection_id -- id of the collection to copy the dashboard to (default None)
131-
deepcopy -- whether to duplicate the cards inside the dashboard (default False).
132-
If True, puts the duplicated cards in a collection called "[dashboard_name]'s cards"
133-
in the same path as the duplicated dashboard.
134-
postfix -- if destination_dashboard_name is None, adds this string to the end of source_dashboard_name
135-
to make destination_dashboard_name
124+
Parameters
125+
----------
126+
source_dashboard_name : name of the dashboard to copy (default None)
127+
source_dashboard_id : id of the dashboard to copy (default None)
128+
source_collection_name : name of the collection the source dashboard is located in (default None)
129+
source_collection_id : id of the collection the source dashboard is located in (default None)
130+
destination_dashboard_name : name used for the dashboard in destination (default None).
131+
If None, it will use the name of the source dashboard + postfix.
132+
destination_collection_name : name of the collection to copy the dashboard to (default None)
133+
destination_collection_id : id of the collection to copy the dashboard to (default None)
134+
deepcopy : whether to duplicate the cards inside the dashboard (default False).
135+
postfix : if destination_dashboard_name is None, adds this string to the end of source_dashboard_name
136+
to make destination_dashboard_name
136137
"""
137138
### making sure we have the data that we need
138139
if not source_dashboard_id:
@@ -154,52 +155,16 @@ def copy_dashboard(self, source_dashboard_name=None, source_dashboard_id=None,
154155
source_dashboard_name = self.get_item_name(item_type='dashboard', item_id=source_dashboard_id)
155156
destination_dashboard_name = source_dashboard_name + postfix
156157

157-
### shallow-copy
158-
shallow_copy_json = {'collection_id':destination_collection_id, 'name':destination_dashboard_name}
159-
res = self.post('/api/dashboard/{}/copy'.format(source_dashboard_id), json=shallow_copy_json)
158+
parameters = {
159+
'collection_id':destination_collection_id,
160+
'name':destination_dashboard_name,
161+
'is_deep_copy':deepcopy,
162+
'collection_position': collection_position,
163+
'description': description
164+
}
165+
res = self.post('/api/dashboard/{}/copy'.format(source_dashboard_id), json=parameters)
160166
dup_dashboard_id = res['id']
161167

162-
### deepcopy
163-
if deepcopy:
164-
# get the source dashboard info
165-
source_dashboard = self.get('/api/dashboard/{}'.format(source_dashboard_id))
166-
167-
# create an empty collection to copy the cards into it
168-
res = self.post('/api/collection/',
169-
json={'name':destination_dashboard_name + "'s cards",
170-
'color':'#509EE3',
171-
'parent_id':destination_collection_id})
172-
cards_collection_id = res['id']
173-
174-
# duplicate cards and put them in the created collection and make a card_id mapping
175-
source_dashboard_card_IDs = [ i['card_id'] for i in source_dashboard['dashcards'] if i['card_id'] is not None ]
176-
card_id_mapping = {}
177-
for card_id in source_dashboard_card_IDs:
178-
dup_card_id = self.copy_card(source_card_id=card_id, destination_collection_id=cards_collection_id)
179-
card_id_mapping[card_id] = dup_card_id
180-
181-
# replace cards in the duplicated dashboard with duplicated cards
182-
dup_dashboard = self.get('/api/dashboard/{}'.format(dup_dashboard_id))
183-
for card in dup_dashboard['dashcards']:
184-
185-
# ignore text boxes. These get copied in the shallow-copy stage.
186-
if card['card_id'] is None:
187-
continue
188-
189-
# prepare a json to be used for replacing the cards in the duplicated dashboard
190-
new_card_id = card_id_mapping[card['card_id']]
191-
card_json = {}
192-
card_json['cardId'] = new_card_id
193-
for prop in ['visualization_settings', 'col', 'row', 'size_x', 'size_y', 'series', 'parameter_mappings']:
194-
card_json[prop] = card[prop]
195-
for item in card_json['parameter_mappings']:
196-
item['card_id'] = new_card_id
197-
# remove the card from the duplicated dashboard
198-
dash_card_id = card['id'] # This is id of the card in the dashboard (different from id of the card itself)
199-
self.delete('/api/dashboard/{}/cards'.format(dup_dashboard_id), params={'dashcardId':dash_card_id})
200-
# add the new card to the duplicated dashboard
201-
self.post('/api/dashboard/{}/cards'.format(dup_dashboard_id), json=card_json)
202-
203168
return dup_dashboard_id
204169

205170

@@ -211,22 +176,23 @@ def copy_collection(self, source_collection_name=None, source_collection_id=None
211176
"""
212177
Copy the collection with the given name/id into the given destination parent collection.
213178
214-
Keyword arguments:
215-
source_collection_name -- name of the collection to copy (default None)
216-
source_collection_id -- id of the collection to copy (default None)
217-
destination_collection_name -- the name to be used for the collection in the destination (default None).
179+
Parameters
180+
----------
181+
source_collection_name : name of the collection to copy (default None)
182+
source_collection_id : id of the collection to copy (default None)
183+
destination_collection_name : the name to be used for the collection in the destination (default None).
218184
If None, it will use the name of the source collection + postfix.
219-
destination_parent_collection_name -- name of the destination parent collection (default None).
185+
destination_parent_collection_name : name of the destination parent collection (default None).
220186
This is the collection that would have the copied collection as a child.
221187
use 'Root' for the root collection.
222-
destination_parent_collection_id -- id of the destination parent collection (default None).
188+
destination_parent_collection_id : id of the destination parent collection (default None).
223189
This is the collection that would have the copied collection as a child.
224-
deepcopy_dashboards -- whether to duplicate the cards inside the dashboards (default False).
190+
deepcopy_dashboards : whether to duplicate the cards inside the dashboards (default False).
225191
If True, puts the duplicated cards in a collection called "[dashboard_name]'s duplicated cards"
226192
in the same path as the duplicated dashboard.
227-
postfix -- if destination_collection_name is None, adds this string to the end of source_collection_name to make destination_collection_name.
228-
child_items_postfix -- this string is added to the end of the child items' names, when saving them in the destination (default '').
229-
verbose -- prints extra information (default False)
193+
postfix : if destination_collection_name is None, adds this string to the end of source_collection_name to make destination_collection_name.
194+
child_items_postfix : this string is added to the end of the child items' names, when saving them in the destination (default '').
195+
verbose : prints extra information (default False)
230196
"""
231197
### making sure we have the data that we need
232198
if not source_collection_id:

metabase_api/create_methods.py

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,22 @@ def create_card(self, card_name=None, collection_name=None, collection_id=None,
66
Create a card using the given arguments utilizing the endpoint 'POST /api/card/'.
77
If collection is not given, the root collection is used.
88
9-
Keyword arguments:
10-
card_name -- the name used to create the card (default None)
11-
collection_name -- name of the collection to place the card (default None).
12-
collection_id -- id of the collection to place the card (default None)
13-
db_name -- name of the db that is used as the source of data (default None)
14-
db_id -- id of the db used as the source of data (default None)
15-
table_name -- name of the table used as the source of data (default None)
16-
table_id -- id of the table used as the source of data (default None)
17-
column_order -- order for showing columns. Accepted values are 'alphabetical', 'db_table_order' (default)
9+
Parameters
10+
----------
11+
card_name : the name used to create the card (default None)
12+
collection_name : name of the collection to place the card (default None).
13+
collection_id : id of the collection to place the card (default None)
14+
db_name : name of the db that is used as the source of data (default None)
15+
db_id : id of the db used as the source of data (default None)
16+
table_name : name of the table used as the source of data (default None)
17+
table_id : id of the table used as the source of data (default None)
18+
column_order : order for showing columns. Accepted values are 'alphabetical', 'db_table_order' (default)
1819
or a list of column names
19-
custom_json -- key-value pairs that can provide some or all the data needed for creating the card (default None).
20+
custom_json : key-value pairs that can provide some or all the data needed for creating the card (default None).
2021
If you are providing only this argument, the keys 'name', 'dataset_query' and 'display' are required
2122
(https://github.com/metabase/metabase/blob/master/docs/api-documentation.md#post-apicard).
22-
verbose -- whether to print extra information (default False)
23-
return_card -- whather to return the created card info (default False)
23+
verbose : whether to print extra information (default False)
24+
return_card : whather to return the created card info (default False)
2425
"""
2526
if custom_json:
2627
assert type(custom_json) == dict
@@ -178,11 +179,12 @@ def create_collection(self, collection_name, parent_collection_id=None, parent_c
178179
"""
179180
Create an empty collection, in the given location, utilizing the endpoint 'POST /api/collection/'.
180181
181-
Keyword arguments:
182-
collection_name -- the name used for the created collection.
183-
parent_collection_id -- id of the collection where the created collection resides in.
184-
parent_collection_name -- name of the collection where the created collection resides in (use 'Root' for the root collection).
185-
return_results -- whether to return the info of the created collection.
182+
Parameters
183+
----------
184+
collection_name : the name used for the created collection.
185+
parent_collection_id : id of the collection where the created collection resides in.
186+
parent_collection_name : name of the collection where the created collection resides in (use 'Root' for the root collection).
187+
return_results : whether to return the info of the created collection.
186188
"""
187189
# Making sure we have the data we need
188190
if not parent_collection_id:
@@ -204,16 +206,17 @@ def create_segment(self, segment_name, column_name, column_values, segment_descr
204206
"""
205207
Create a segment using the given arguments utilizing the endpoint 'POST /api/segment/'.
206208
207-
Keyword arguments:
208-
segment_name -- the name used for the created segment.
209-
column_name -- name of the column used for filtering.
210-
column_values -- list of values for filtering in the given column.
211-
segment_description -- description of the segment (default '')
212-
db_name -- name of the db that is used as the source of data (default None)
213-
db_id -- id of the db used as the source of data (default None)
214-
table_name -- name of the table used for creating the segmnet on it (default None)
215-
table_id -- id of the table used for creating the segmnet on it (default None)
216-
return_segment -- whather to return the created segment info (default False)
209+
Parameters
210+
----------
211+
segment_name : the name used for the created segment.
212+
column_name : name of the column used for filtering.
213+
column_values : list of values for filtering in the given column.
214+
segment_description : description of the segment (default '')
215+
db_name : name of the db that is used as the source of data (default None)
216+
db_id : id of the db used as the source of data (default None)
217+
table_name : name of the table used for creating the segmnet on it (default None)
218+
table_id : id of the table used for creating the segmnet on it (default None)
219+
return_segment : whather to return the created segment info (default False)
217220
"""
218221
# Making sure we have the data needed
219222
if not table_name and not table_id:

0 commit comments

Comments
 (0)