-
Notifications
You must be signed in to change notification settings - Fork 196
/
Copy pathadd_contents_to_favorite.py
80 lines (53 loc) · 2.71 KB
/
add_contents_to_favorite.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"""
The purpose of this script is to add a specific Dashboard or Board to "Favorites"
for a list of users, which may help new users discover useful Looker contents quicker and easier.
The script contains two functions (add_boards_to_users and add_dashboards_to_users) that are similar
in logic and execution. Example function calls are placed at the end of the script.
Author: Lan
Last modified: June 16, 2021
"""
import looker_sdk
sdk = looker_sdk.init40()
def add_boards_to_users(board_id: int, users_id: list):
""" Add a specific board to the "Favorite" contents for a list of user
Args:
board_id (int): id of a Looker board (https://company.looker.com/boards/id)
users_id (list): a list of users id (int) in the form of a native Python list
Returns: "Successfully added!" (str)
Raises: N/A (does not explicitly raise an exception); Looker SDK will raise an error.
"""
content_metadata_id = sdk.board(board_id=board_id)['content_metadata_id']
"""An admin can not update the list of favorite contents for users,
so sdk.auth.login_user() and sdk.auth.logout() are called to sudo as each user to call `create_content_favorite()"""
for i in users_id:
sdk.auth.login_user(i)
params = {}
params["user_id"] = i
params["content_metadata_id"] = content_metadata_id
sdk.create_content_favorite(params)
sdk.auth.logout()
print("Successfully added!")
""" The logic for `add_dashboards_to_users` is the same, except that `dashboard_id` is a string (because LookML dashboard id is a string).
Also, we are using `sdk.dashboard()` to retrieve `content_metadata_id`"""
def add_dashboards_to_users(dashboard_id: str, users_id: list):
""" Add a specific dashboard to the list of favorite contents for a list of user
Args:
dashboard_id (str): id of a Looker dashboard (https://company.looker.com/dashboards/id)
users_id (list): a list of users id in the form of a native Python list
Returns: "Successfully added!" (str)
Raises: N/A (does not explicitly raise an exception); Looker SDK will raise an error.
"""
content_metadata_id = sdk.dashboard(dashboard_id=dashboard_id)['content_metadata_id']
"""An admin can not update the list of favorite contents for users,
sdk.auth.login_user() and `sdk.auth.logout()` are called to sudo as each user to call `create_content_favorite()"""
for i in users_id:
sdk.auth.login_user(i)
params = {}
params["user_id"] = i
params["content_metadata_id"] = content_metadata_id
sdk.create_content_favorite(params)
sdk.auth.logout()
print("Successfully added!")
# Call the functions
add_boards_to_users(board_id = 1, users_id = [1])
add_dashboards_to_users(dashboard_id = "string", users_id = [1])