forked from tableau/server-client-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding sample file for favorites, issue tableau#737 (tableau#1085)
* adding sample file for favorites, issue tableau#737 * add metrics to favorites Cleaned up the from_response method for several items because we can now initialize them empty Co-authored-by: Jac Fitzgerald <[email protected]> Co-authored-by: Nicole Arcolino <[email protected]>
- Loading branch information
1 parent
4fb6180
commit cc62a50
Showing
15 changed files
with
358 additions
and
266 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# This script demonstrates how to get all favorites, or add/delete a favorite. | ||
|
||
import argparse | ||
import logging | ||
import tableauserverclient as TSC | ||
from tableauserverclient import Resource | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser(description="Explore favoriting functions supported by the Server API.") | ||
# Common options; please keep those in sync across all samples | ||
parser.add_argument("--server", "-s", help="server address") | ||
parser.add_argument("--site", "-S", help="site name") | ||
parser.add_argument("--token-name", "-p", help="name of the personal access token used to sign into the server") | ||
parser.add_argument("--token-value", "-v", help="value of the personal access token used to sign into the server") | ||
parser.add_argument( | ||
"--logging-level", | ||
"-l", | ||
choices=["debug", "info", "error"], | ||
default="error", | ||
help="desired logging level (set to error by default)", | ||
) | ||
|
||
args = parser.parse_args() | ||
|
||
# Set logging level based on user input, or error by default | ||
logging_level = getattr(logging, args.logging_level.upper()) | ||
logging.basicConfig(level=logging_level) | ||
|
||
# SIGN IN | ||
tableau_auth = TSC.PersonalAccessTokenAuth(args.token_name, args.token_value, site_id=args.site) | ||
server = TSC.Server(args.server, use_server_version=True) | ||
with server.auth.sign_in(tableau_auth): | ||
print(server) | ||
my_workbook = None | ||
my_view = None | ||
my_datasource = None | ||
|
||
# get all favorites on site for the logged on user | ||
user: TSC.UserItem = TSC.UserItem() | ||
user.id = server.user_id | ||
print("Favorites for user: {}".format(user.id)) | ||
server.favorites.get(user) | ||
print(user.favorites) | ||
|
||
# get list of workbooks | ||
all_workbook_items, pagination_item = server.workbooks.get() | ||
if all_workbook_items is not None and len(all_workbook_items) > 0: | ||
my_workbook: TSC.WorkbookItem = all_workbook_items[0] | ||
server.favorites.add_favorite(server, user, Resource.Workbook.name(), all_workbook_items[0]) | ||
print( | ||
"Workbook added to favorites. Workbook Name: {}, Workbook ID: {}".format( | ||
my_workbook.name, my_workbook.id | ||
) | ||
) | ||
views = server.workbooks.populate_views(my_workbook) | ||
if views is not None and len(views) > 0: | ||
my_view = views[0] | ||
server.favorites.add_favorite_view(user, my_view) | ||
print("View added to favorites. View Name: {}, View ID: {}".format(my_view.name, my_view.id)) | ||
|
||
all_datasource_items, pagination_item = server.datasources.get() | ||
if all_datasource_items: | ||
my_datasource = all_datasource_items[0] | ||
server.favorites.add_favorite_datasource(user, my_datasource) | ||
print( | ||
"Datasource added to favorites. Datasource Name: {}, Datasource ID: {}".format( | ||
my_datasource.name, my_datasource.id | ||
) | ||
) | ||
|
||
server.favorites.delete_favorite_workbook(user, my_workbook) | ||
print( | ||
"Workbook deleted from favorites. Workbook Name: {}, Workbook ID: {}".format(my_workbook.name, my_workbook.id) | ||
) | ||
|
||
server.favorites.delete_favorite_view(user, my_view) | ||
print("View deleted from favorites. View Name: {}, View ID: {}".format(my_view.name, my_view.id)) | ||
|
||
server.favorites.delete_favorite_datasource(user, my_datasource) | ||
print( | ||
"Datasource deleted from favorites. Datasource Name: {}, Datasource ID: {}".format( | ||
my_datasource.name, my_datasource.id | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,90 @@ | ||
import logging | ||
|
||
from defusedxml.ElementTree import fromstring | ||
from .tableau_types import TableauItem | ||
|
||
from .datasource_item import DatasourceItem | ||
from .flow_item import FlowItem | ||
from .project_item import ProjectItem | ||
from .metric_item import MetricItem | ||
from .view_item import ViewItem | ||
from .workbook_item import WorkbookItem | ||
from typing import Dict, List | ||
|
||
logger = logging.getLogger("tableau.models.favorites_item") | ||
|
||
from typing import Dict, List, Union | ||
|
||
FavoriteType = Dict[ | ||
str, | ||
List[ | ||
Union[ | ||
DatasourceItem, | ||
ProjectItem, | ||
FlowItem, | ||
ViewItem, | ||
WorkbookItem, | ||
] | ||
], | ||
List[TableauItem], | ||
] | ||
|
||
|
||
class FavoriteItem: | ||
class Type: | ||
Workbook: str = "workbook" | ||
Datasource: str = "datasource" | ||
View: str = "view" | ||
Project: str = "project" | ||
Flow: str = "flow" | ||
|
||
@classmethod | ||
def from_response(cls, xml: str, namespace: Dict) -> FavoriteType: | ||
favorites: FavoriteType = { | ||
"datasources": [], | ||
"flows": [], | ||
"projects": [], | ||
"metrics": [], | ||
"views": [], | ||
"workbooks": [], | ||
} | ||
|
||
parsed_response = fromstring(xml) | ||
for workbook in parsed_response.findall(".//t:favorite/t:workbook", namespace): | ||
fav_workbook = WorkbookItem("") | ||
fav_workbook._set_values(*fav_workbook._parse_element(workbook, namespace)) | ||
if fav_workbook: | ||
favorites["workbooks"].append(fav_workbook) | ||
for view in parsed_response.findall(".//t:favorite[t:view]", namespace): | ||
fav_views = ViewItem.from_xml_element(view, namespace) | ||
if fav_views: | ||
for fav_view in fav_views: | ||
favorites["views"].append(fav_view) | ||
for datasource in parsed_response.findall(".//t:favorite/t:datasource", namespace): | ||
fav_datasource = DatasourceItem("") | ||
fav_datasource._set_values(*fav_datasource._parse_element(datasource, namespace)) | ||
|
||
datasources_xml = parsed_response.findall(".//t:favorite/t:datasource", namespace) | ||
flows_xml = parsed_response.findall(".//t:favorite/t:flow", namespace) | ||
metrics_xml = parsed_response.findall(".//t:favorite/t:metric", namespace) | ||
projects_xml = parsed_response.findall(".//t:favorite/t:project", namespace) | ||
views_xml = parsed_response.findall(".//t:favorite/t:view", namespace) | ||
workbooks_xml = parsed_response.findall(".//t:favorite/t:workbook", namespace) | ||
|
||
logger.debug( | ||
"ds: {}, flows: {}, metrics: {}, projects: {}, views: {}, wbs: {}".format( | ||
len(datasources_xml), | ||
len(flows_xml), | ||
len(metrics_xml), | ||
len(projects_xml), | ||
len(views_xml), | ||
len(workbooks_xml), | ||
) | ||
) | ||
for datasource in datasources_xml: | ||
fav_datasource = DatasourceItem.from_xml(datasource, namespace) | ||
if fav_datasource: | ||
logger.debug(fav_datasource) | ||
favorites["datasources"].append(fav_datasource) | ||
for project in parsed_response.findall(".//t:favorite/t:project", namespace): | ||
fav_project = ProjectItem("p") | ||
fav_project._set_values(*fav_project._parse_element(project)) | ||
if fav_project: | ||
favorites["projects"].append(fav_project) | ||
for flow in parsed_response.findall(".//t:favorite/t:flow", namespace): | ||
fav_flow = FlowItem("flows") | ||
fav_flow._set_values(*fav_flow._parse_element(flow, namespace)) | ||
|
||
for flow in flows_xml: | ||
fav_flow = FlowItem.from_xml(flow, namespace) | ||
if fav_flow: | ||
logger.debug(fav_flow) | ||
favorites["flows"].append(fav_flow) | ||
|
||
for metric in metrics_xml: | ||
fav_metric = MetricItem.from_xml(metric, namespace) | ||
if fav_metric: | ||
logger.debug(fav_metric) | ||
favorites["metrics"].append(fav_metric) | ||
|
||
for project in projects_xml: | ||
fav_project = ProjectItem.from_xml(project, namespace) | ||
if fav_project: | ||
logger.debug(fav_project) | ||
favorites["projects"].append(fav_project) | ||
|
||
for view in views_xml: | ||
fav_view = ViewItem.from_xml(view, namespace) | ||
if fav_view: | ||
logger.debug(fav_view) | ||
favorites["views"].append(fav_view) | ||
|
||
for workbook in workbooks_xml: | ||
fav_workbook = WorkbookItem.from_xml(workbook, namespace) | ||
if fav_workbook: | ||
logger.debug(fav_workbook) | ||
favorites["workbooks"].append(fav_workbook) | ||
|
||
logger.debug(favorites) | ||
return favorites |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.