Skip to content

Commit e3f1e22

Browse files
authored
Adding support for thumbnail related options in workbook publish (#1542)
* Adding support for thumbnail related options in workbook publish
1 parent 9d4e43e commit e3f1e22

File tree

4 files changed

+102
-8
lines changed

4 files changed

+102
-8
lines changed

Diff for: samples/publish_workbook.py

+31-7
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,16 @@ def main():
3636
help="desired logging level (set to error by default)",
3737
)
3838
# Options specific to this sample
39+
group = parser.add_mutually_exclusive_group(required=False)
40+
group.add_argument("--thumbnails-user-id", "-u", help="User ID to use for thumbnails")
41+
group.add_argument("--thumbnails-group-id", "-g", help="Group ID to use for thumbnails")
42+
43+
parser.add_argument("--workbook-name", "-n", help="Name with which to publish the workbook")
3944
parser.add_argument("--file", "-f", help="local filepath of the workbook to publish")
4045
parser.add_argument("--as-job", "-a", help="Publishing asynchronously", action="store_true")
4146
parser.add_argument("--skip-connection-check", "-c", help="Skip live connection check", action="store_true")
47+
parser.add_argument("--project", help="Project within which to publish the workbook")
48+
parser.add_argument("--show-tabs", help="Publish workbooks with tabs displayed", action="store_true")
4249

4350
args = parser.parse_args()
4451

@@ -50,9 +57,20 @@ def main():
5057
tableau_auth = TSC.PersonalAccessTokenAuth(args.token_name, args.token_value, site_id=args.site)
5158
server = TSC.Server(args.server, use_server_version=True)
5259
with server.auth.sign_in(tableau_auth):
53-
# Step 2: Get all the projects on server, then look for the default one.
54-
all_projects, pagination_item = server.projects.get()
55-
default_project = next((project for project in all_projects if project.is_default()), None)
60+
# Step2: Retrieve the project id, if a project name was passed
61+
if args.project is not None:
62+
req_options = TSC.RequestOptions()
63+
req_options.filter.add(
64+
TSC.Filter(TSC.RequestOptions.Field.Name, TSC.RequestOptions.Operator.Equals, args.project)
65+
)
66+
projects = list(TSC.Pager(server.projects, req_options))
67+
if len(projects) > 1:
68+
raise ValueError("The project name is not unique")
69+
project_id = projects[0].id
70+
else:
71+
# Get all the projects on server, then look for the default one.
72+
all_projects, pagination_item = server.projects.get()
73+
project_id = next((project for project in all_projects if project.is_default()), None).id
5674

5775
connection1 = ConnectionItem()
5876
connection1.server_address = "mssql.test.com"
@@ -67,10 +85,16 @@ def main():
6785
all_connections.append(connection1)
6886
all_connections.append(connection2)
6987

70-
# Step 3: If default project is found, form a new workbook item and publish.
88+
# Step 3: Form a new workbook item and publish.
7189
overwrite_true = TSC.Server.PublishMode.Overwrite
72-
if default_project is not None:
73-
new_workbook = TSC.WorkbookItem(default_project.id)
90+
if project_id is not None:
91+
new_workbook = TSC.WorkbookItem(
92+
project_id=project_id,
93+
name=args.workbook_name,
94+
show_tabs=args.show_tabs,
95+
thumbnails_user_id=args.thumbnails_user_id,
96+
thumbnails_group_id=args.thumbnails_group_id,
97+
)
7498
if args.as_job:
7599
new_job = server.workbooks.publish(
76100
new_workbook,
@@ -92,7 +116,7 @@ def main():
92116
)
93117
print(f"Workbook published. ID: {new_workbook.id}")
94118
else:
95-
error = "The default project could not be found."
119+
error = "The destination project could not be found."
96120
raise LookupError(error)
97121

98122

Diff for: tableauserverclient/models/workbook_item.py

+26-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,14 @@ class as arguments. The workbook item specifies the project.
9999
>>> new_workbook = TSC.WorkbookItem('3a8b6148-493c-11e6-a621-6f3499394a39')
100100
"""
101101

102-
def __init__(self, project_id: Optional[str] = None, name: Optional[str] = None, show_tabs: bool = False) -> None:
102+
def __init__(
103+
self,
104+
project_id: Optional[str] = None,
105+
name: Optional[str] = None,
106+
show_tabs: bool = False,
107+
thumbnails_user_id: Optional[str] = None,
108+
thumbnails_group_id: Optional[str] = None,
109+
) -> None:
103110
self._connections = None
104111
self._content_url = None
105112
self._webpage_url = None
@@ -130,6 +137,8 @@ def __init__(self, project_id: Optional[str] = None, name: Optional[str] = None,
130137
}
131138
self.data_freshness_policy = None
132139
self._permissions = None
140+
self.thumbnails_user_id = thumbnails_user_id
141+
self.thumbnails_group_id = thumbnails_group_id
133142

134143
return None
135144

@@ -275,6 +284,22 @@ def revisions(self) -> list[RevisionItem]:
275284
raise UnpopulatedPropertyError(error)
276285
return self._revisions()
277286

287+
@property
288+
def thumbnails_user_id(self) -> Optional[str]:
289+
return self._thumbnails_user_id
290+
291+
@thumbnails_user_id.setter
292+
def thumbnails_user_id(self, value: str):
293+
self._thumbnails_user_id = value
294+
295+
@property
296+
def thumbnails_group_id(self) -> Optional[str]:
297+
return self._thumbnails_group_id
298+
299+
@thumbnails_group_id.setter
300+
def thumbnails_group_id(self, value: str):
301+
self._thumbnails_group_id = value
302+
278303
def _set_connections(self, connections):
279304
self._connections = connections
280305

Diff for: tableauserverclient/server/request_factory.py

+6
Original file line numberDiff line numberDiff line change
@@ -958,6 +958,12 @@ def _generate_xml(
958958
views_element = ET.SubElement(workbook_element, "views")
959959
for view_name in workbook_item.hidden_views:
960960
_add_hiddenview_element(views_element, view_name)
961+
962+
if workbook_item.thumbnails_user_id is not None:
963+
workbook_element.attrib["thumbnailsUserId"] = workbook_item.thumbnails_user_id
964+
elif workbook_item.thumbnails_group_id is not None:
965+
workbook_element.attrib["thumbnailsGroupId"] = workbook_item.thumbnails_group_id
966+
961967
return ET.tostring(xml_request)
962968

963969
def update_req(self, workbook_item, parent_srv: Optional["Server"] = None):

Diff for: test/test_workbook.py

+39
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,45 @@ def test_publish_with_hidden_views_on_workbook(self) -> None:
624624
self.assertTrue(re.search(rb"<views><view.*?hidden=\"true\".*?\/><\/views>", request_body))
625625
self.assertTrue(re.search(rb"<views><view.*?name=\"GDP per capita\".*?\/><\/views>", request_body))
626626

627+
def test_publish_with_thumbnails_user_id(self) -> None:
628+
with open(PUBLISH_XML, "rb") as f:
629+
response_xml = f.read().decode("utf-8")
630+
with requests_mock.mock() as m:
631+
m.post(self.baseurl, text=response_xml)
632+
633+
new_workbook = TSC.WorkbookItem(
634+
name="Sample",
635+
show_tabs=False,
636+
project_id="ee8c6e70-43b6-11e6-af4f-f7b0d8e20760",
637+
thumbnails_user_id="ee8c6e70-43b6-11e6-af4f-f7b0d8e20761",
638+
)
639+
640+
sample_workbook = os.path.join(TEST_ASSET_DIR, "SampleWB.twbx")
641+
publish_mode = self.server.PublishMode.CreateNew
642+
new_workbook = self.server.workbooks.publish(new_workbook, sample_workbook, publish_mode)
643+
request_body = m._adapter.request_history[0]._request.body
644+
# order of attributes in xml is unspecified
645+
self.assertTrue(re.search(rb"thumbnailsUserId=\"ee8c6e70-43b6-11e6-af4f-f7b0d8e20761\"", request_body))
646+
647+
def test_publish_with_thumbnails_group_id(self) -> None:
648+
with open(PUBLISH_XML, "rb") as f:
649+
response_xml = f.read().decode("utf-8")
650+
with requests_mock.mock() as m:
651+
m.post(self.baseurl, text=response_xml)
652+
653+
new_workbook = TSC.WorkbookItem(
654+
name="Sample",
655+
show_tabs=False,
656+
project_id="ee8c6e70-43b6-11e6-af4f-f7b0d8e20760",
657+
thumbnails_group_id="ee8c6e70-43b6-11e6-af4f-f7b0d8e20762",
658+
)
659+
660+
sample_workbook = os.path.join(TEST_ASSET_DIR, "SampleWB.twbx")
661+
publish_mode = self.server.PublishMode.CreateNew
662+
new_workbook = self.server.workbooks.publish(new_workbook, sample_workbook, publish_mode)
663+
request_body = m._adapter.request_history[0]._request.body
664+
self.assertTrue(re.search(rb"thumbnailsGroupId=\"ee8c6e70-43b6-11e6-af4f-f7b0d8e20762\"", request_body))
665+
627666
@pytest.mark.filterwarnings("ignore:'as_job' not available")
628667
def test_publish_with_query_params(self) -> None:
629668
with open(PUBLISH_ASYNC_XML, "rb") as f:

0 commit comments

Comments
 (0)