8000 removed update project by id, added update workbooks by list (txt) · gmiretti/server-client-python@749551c · GitHub
[go: up one dir, main page]

Skip to content

Commit 749551c

Browse files
author
bzhang
committed
removed update project by id, added update workbooks by list (txt)
1 parent dd8bad1 commit 749551c

File tree

2 files changed

+51
-35
lines changed

2 files changed

+51
-35
lines changed

samples/materialize_workbooks.py

Lines changed: 49 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import argparse
22
import getpass
33
import logging
4-
54
import tableauserverclient as TSC
5+
from collections import defaultdict
66

77

88
def main():
@@ -21,8 +21,8 @@ def main():
2121
parser.add_argument('--type', '-t', required=False, choices=['site', 'workbook', 'project_name',
2222
'project_id', 'project_path'],
2323
help='type of content you want to update materialized views settings on')
24+
parser.add_argument('--file-path', '-fp', required=False, help='path to a list of workbooks')
2425
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")
2626
parser.add_argument('--project-path', '-pp', required =False, help="path of the project")
2727

2828
args = parser.parse_args()
@@ -52,9 +52,6 @@ def main():
5252
elif args.type == 'project_name':
5353
update_project_by_name(args, enable_materialized_views, password, site_content_url)
5454

55-
elif args.type == 'project_id':
56-
update_project_by_id(args, enable_materialized_views, password, site_content_url)
57-
5855
elif args.type == 'project_path':
5956
update_project_by_path(args, enable_materialized_views, password, site_content_url)
6057

@@ -67,7 +64,7 @@ def find_project_path(project, all_projects, path):
6764
if project.parent_id is None:
6865
return path
6966
else:
70-
find_project_path(all_projects[project.parent_id], all_projects, path)
67+
return find_project_path(all_projects[project.parent_id], all_projects, path)
7168

7269

7370
def get_project_paths(server, projects):
@@ -76,7 +73,7 @@ def get_project_paths(server, projects):
7673

7774
result = dict()
7875
for project in projects:
79-
result[find_project_path(project, all_projects, "")] = project
76+
result[find_project_path(project, all_projects, "")[:-1]] = project
8077
return result
8178

8279

@@ -99,7 +96,7 @@ def show_materialized_views_status(args, password, site_content_url):
9996
print "Site name:", site.name
10097
print '\n'
10198

102-
print("Materialized views is enabled on workbooks:")
99+
print "Materialized views is enabled on workbooks:"
103100
# Individual workbooks can be enabled only when the sites they belong to are enabled too
104101
for site in enabled_sites:
105102
site_auth = TSC.TableauAuth(args.username, password, site.content_url)
@@ -115,30 +112,17 @@ def update_project_by_path(args, enable_materialized_views, password, site_conte
115112
return
116113
tableau_auth = TSC.TableauAuth(args.username, password, site_content_url)
117114
server = TSC.Server(args.server, use_server_version=True)
118-
project_name = args.project_path.split('/')
115+
project_name = args.project_path.split('/')[-1]
119116
with server.auth.sign_in(tableau_auth):
120117
projects = [project for project in TSC.Pager(server.projects) if project.name == project_name]
121118

122119
if len(projects) > 1:
123-
possible_paths = get_project_paths(server.projects)
120+
possible_paths = get_project_paths(server, projects)
124121
update_project(possible_paths[args.project_path], server, enable_materialized_views)
125122
else:
126123
update_project(projects[0], server, enable_materialized_views)
127124

128125

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-
142126
def update_project_by_name(args, enable_materialized_views, password, site_content_url):
143127
if args.project_name is None:
144128
print "Use --project-name <project name> to specify the name of the project"
@@ -151,7 +135,7 @@ def update_project_by_name(args, enable_materialized_views, password, site_conte
151135

152136
if len(projects) > 1:
153137
possible_paths = get_project_paths(server, projects)
154-
print "Project name is not unique, use '--project_path <path>' or '--project-id <project id>'"
138+
print "Project name is not unique 57AE , use '--project_path <path>'"
155139
print "Possible project paths:"
156140
print_paths(possible_paths)
157141
print '\n'
@@ -169,21 +153,51 @@ def update_project(project, server, enable_materialized_views):
169153
print '\n'
170154

171155

156+
def parse_workbook_path(file_path):
157+
workbook_paths = open(file_path, 'r')
158+
workbook_path_mapping = defaultdict(list)
159+
for workbook_path in workbook_paths:
160+
workbook_project = workbook_path.rstrip().split('/')
161+
workbook_path_mapping[workbook_project[-1]].append('/'.join(workbook_project[:-1]))
162+
return workbook_path_mapping
163+
164+
172165
def update_workbook(args, enable_materialized_views, password, site_content_url):
166+
if args.file_path is None:
167+
print "Use '--file-path <file path>' to specify the path of a list of workbooks"
168+
print "In the file, each line is a path to a workbook, for example:"
169+
print "project1/project2/workbook_name_1"
170+
print "project3/workbook_name_2"
171+
print '\n'
172+
return
173+
173174
tableau_auth = TSC.TableauAuth(args.username, password, site_id=site_content_url)
174175
server = TSC.Server(args.server, use_server_version=True)
175-
# Now it updates all the workbooks in the site
176-
# To update selected ones please use filter:
177-
# https://github.com/tableau/server-client-python/blob/master/docs/docs/filter-sort.md
178-
# This only updates the workbooks in the site you are signing into
179176
with server.auth.sign_in(tableau_auth):
180-
for workbook in TSC.Pager(server.workbooks):
181-
workbook.materialized_views_enabled = enable_materialized_views
182-
183-
server.workbooks.update(workbook)
184-
site = server.sites.get_by_content_url(site_content_url)
185-
print "Updated materialized views settings for workbook:", workbook.name, "from site:", site.name
186-
print '\n'
177+
workbook_path_mapping = parse_workbook_path(args.file_path)
178+
all_projects = {project.id: project for project in TSC.Pager(server.projects)}
179+
update_workbooks_by_paths(all_projects, enable_materialized_views, server, workbook_path_mapping)
180+
181+
182+
def update_workbooks_by_paths(all_projects, enable_materialized_views, server, workbook_path_mapping):
183+
for workbook_name, workbook_paths in workbook_path_mapping.items():
184+
req_option = TSC.RequestOptions()
185+
req_option.filter.add(TSC.Filter(TSC.RequestOptions.Field.Name,
186+
TSC.RequestOptions.Operator.Equals,
187+
workbook_name))
188+
workbooks = list(TSC.Pager(server.workbooks, req_option))
189+
if len(workbooks) == 1:
190+
workbooks[0].materialized_views_enabled = enable_materialized_views
191+
server.workbooks.update(workbooks[0])
192+
print "Updated materialized views settings for workbook:", workbooks[0].name
193+
else:
194+
for workbook in workbooks:
195+
path = find_project_path(all_projects[workbook.project_id], all_projects, "")[:-1]
196+
if path in workbook_paths:
197+
workbook.materialized_views_enabled = enable_materialized_views
198+
server.workbooks.update(workbook)
199+
print "Updated materialized views settings for workbook:", path + '/' + workbook.name
200+
print '\n'
187201

188202

189203
def update_site(args, enable_materialized_views, password, site_content_url):

samples/test.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
project1/project1/Book3
2+
project1/Book3

0 commit comments

Comments
 (0)
0