8000 enable/disable by project · SnarkyPapi/server-client-python@dd8bad1 · GitHub
[go: up one dir, main page]

Skip to content

Commit dd8bad1

Browse files
author
bzhang
committed
enable/disable by project
1 parent 9de5063 commit dd8bad1

File tree

1 file changed

+98
-2
lines changed

1 file changed

+98
-2
lines changed

samples/materialize_workbooks.py

Lines changed: 98 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ def main():
1818
help='set to Default site by default')
1919
parser.add_argument('--logging-level', '-l', choices=['debug', 'info', 'error'], default='error',
2020
help='desired logging level (set to error by default)')
21-
parser.add_argument('--type', '-t', required=False, choices=['site', 'workbook'],
21+
parser.add_argument('--type', '-t', required=False, choices=['site', 'workbook', 'project_name',
22+
'project_id', 'project_path'],
2223
help='type of content you want to update materialized views settings on')
24+
parser.add_argument('--project-name', '-pn', required=False, help='name of the project')
25+
parser.add_argument('--project-id', '-pi', required=False, help="id of the project")
26+
parser.add_argument('--project-path', '-pp', required =False, help="path of the project")
2327

2428
args = parser.parse_args()
2529

@@ -36,7 +40,7 @@ def main():
3640
enable_materialized_views = args.mode == "enable"
3741

3842
if (args.type is None) != (args.mode is None):
39-
print("Use '--type <workbook/site> --mode <enable/disable>' to update materialized views settings.")
43+
print "Use '--type <content type> --mode <enable/disable>' to update materialized views settings."
4044
return
4145

4246
if args.type == 'site':
@@ -45,10 +49,42 @@ def main():
4549
elif args.type == 'workbook':
4650
update_workbook(args, enable_materialized_views, password, site_content_url)
4751

52+
elif args.type == 'project_name':
53+
update_project_by_name(args, enable_materialized_views, password, site_content_url)
54+
55+
elif args.type == 'project_id':
56+
update_project_by_id(args, enable_materialized_views, password, site_content_url)
57+
58+
elif args.type == 'project_path':
59+
update_project_by_path(args, enable_materialized_views, password, site_content_url)
60+
4861
if args.status:
4962
show_materialized_views_status(args, password, site_content_url)
5063

5164

65+
def find_project_path(project, all_projects, path):
66+
path = project.name + '/' + path
67+
if project.parent_id is None:
68+
return path
69+
else:
70+
find_project_path(all_projects[project.parent_id], all_projects, path)
71+
72+
73+
def get_project_paths(server, projects):
74+
# most likely user won't have too many projects so we store them in a dict to search
75+
all_projects = {project.id: project for project in TSC.Pager(server.projects)}
76+
77+
result = dict()
78+
for project in projects:
79+
result[find_project_path(project, all_projects, "")] = project
80+
return result
81+
82+
83+
def print_paths(paths):
84+
for path in paths.keys():
85+
print path
86+
87+
5288
def show_materialized_views_status(args, password, site_content_url):
5389
tableau_auth = TSC.TableauAuth(args.username, password, site_id=site_content_url)
5490
server = TSC.Server(args.server, use_server_version=True)
@@ -73,6 +109,66 @@ def show_materialized_views_status(args, password, site_content_url):
73109
print "Workbook:", workbook.name, "from site:", site.name
74110

75111

112+
def update_project_by_path(args, enable_materialized_views, password, site_content_url):
113+
if args.project_path is None:
114+
print "Use --project_path <project path> to specify the path of the project"
115+
return
116+
tableau_auth = TSC.TableauAuth(args.username, password, site_content_url)
117+
server = TSC.Server(args.server, use_server_version=True)
118+
project_name = args.project_path.split('/')
119+
with server.auth.sign_in(tableau_auth):
120+
projects = [project for project in TSC.Pager(server.projects) if project.name == project_name]
121+
122+
if len(projects) > 1:
123+
possible_paths = get_project_paths(server.projects)
124+
update_project(possible_paths[args.project_path], server, enable_materialized_views)
125+
else:
126+
update_project(projects[0], server, enable_materialized_views)
127+
128+
129+
def update_project_by_id(args, enable_materialized_views, password, site_content_url):
130+
if args.project_id is None:
131+
print "Use --project-id <project id> to specify the id of the project"
132+
return
133+
tableau_auth = TSC.TableauAuth(args.username, password, site_content_url)
134+
server = TSC.Server(args.server, use_server_version=True)
135+
with server.auth.sign_in(tableau_auth):
136+
for project in TSC.Pager(server.projects):
137+
if project.id == args.project_id:
138+
update_project(project, server, enable_materialized_views)
139+
break
140+
141+
142+
def update_project_by_name(args, enable_materialized_views, password, site_content_url):
143+
if args.project_name is None:
144+
print "Use --project-name <project name> to specify the name of the project"
145+
return
146+
tableau_auth = TSC.TableauAuth(args.username, password, site_content_url)
147+
server = TSC.Server(args.server, use_server_version=True)
148+
with server.auth.sign_in(tableau_auth):
149+
# get all projects with given name
150+
projects = [project for project in TSC.Pager(server.projects) if project.name == args.project_name]
151+
152+
if len(projects) > 1:
153+
possible_paths = get_project_paths(server, projects)
154+
print "Project name is not unique, use '--project_path <path>' or '--project-id <project id>'"
155+
print "Possible project paths:"
156+
print_paths(possible_paths)
157+
print '\n'
158+
return
159+
else:
160+
update_project(projects[0], server, enable_materialized_views)
161+
162+
163+
def update_project(project< 7A1C /span>, server, enable_materialized_views):
164+
for workbook in TSC.Pager(server.workbooks):
165+
if workbook.project_id == project.id:
166+
workbook.materialized_views_enabled = enable_materialized_views
167+
server.workbooks.update(workbook)
168+
print "Updated materialized views settings for project:", project.name
169+
print '\n'
170+
171+
76172
def update_workbook(args, enable_materialized_views, password, site_content_url):
77173
tableau_auth = TSC.TableauAuth(args.username, password, site_id=site_content_url)
78174
server = TSC.Server(args.server, use_server_version=True)

0 commit comments

Comments
 (0)
0