Skip to content

Commit

Permalink
adding sample file for favorites, issue tableau#737 (tableau#1085)
Browse files Browse the repository at this point in the history
* 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
3 people authored Apr 19, 2023
1 parent 4fb6180 commit cc62a50
Show file tree
Hide file tree
Showing 15 changed files with 358 additions and 266 deletions.
85 changes: 85 additions & 0 deletions samples/explore_favorites.py
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
)
)
48 changes: 7 additions & 41 deletions tableauserverclient/models/datasource_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,50 +305,16 @@ def from_response(cls, resp: str, ns: Dict) -> List["DatasourceItem"]:
all_datasource_xml = parsed_response.findall(".//t:datasource", namespaces=ns)

for datasource_xml in all_datasource_xml:
(
ask_data_enablement,
certified,
certification_note,
content_url,
created_at,
datasource_type,
description,
encrypt_extracts,
has_extracts,
id_,
name,
owner_id,
project_id,
project_name,
tags,
updated_at,
use_remote_query_agent,
webpage_url,
) = cls._parse_element(datasource_xml, ns)
datasource_item = cls(project_id)
datasource_item._set_values(
ask_data_enablement,
certified,
certification_note,
content_url,
created_at,
datasource_type,
description,
encrypt_extracts,
has_extracts,
id_,
name,
owner_id,
None,
project_name,
tags,
updated_at,
use_remote_query_agent,
webpage_url,
)
datasource_item = cls.from_xml(datasource_xml, ns)
all_datasource_items.append(datasource_item)
return all_datasource_items

@classmethod
def from_xml(cls, datasource_xml, ns):
datasource_item = cls()
datasource_item._set_values(*cls._parse_element(datasource_xml, ns))
return datasource_item

@staticmethod
def _parse_element(datasource_xml: ET.Element, ns: Dict) -> Tuple:
id_ = datasource_xml.get("id", None)
Expand Down
94 changes: 55 additions & 39 deletions tableauserverclient/models/favorites_item.py
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
55 changes: 30 additions & 25 deletions tableauserverclient/models/flow_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,34 +176,39 @@ def from_response(cls, resp, ns) -> List["FlowItem"]:
all_flow_xml = parsed_response.findall(".//t:flow", namespaces=ns)

for flow_xml in all_flow_xml:
(
id_,
name,
description,
webpage_url,
created_at,
updated_at,
tags,
project_id,
project_name,
owner_id,
) = cls._parse_element(flow_xml, ns)
flow_item = cls(project_id)
flow_item._set_values(
id_,
name,
description,
webpage_url,
created_at,
updated_at,
tags,
None,
project_name,
owner_id,
)
flow_item = cls.from_xml(flow_xml, ns)
all_flow_items.append(flow_item)
return all_flow_items

@classmethod
def from_xml(cls, flow_xml, ns) -> "FlowItem":
(
id_,
name,
description,
webpage_url,
created_at,
updated_at,
tags,
project_id,
project_name,
owner_id,
) = cls._parse_element(flow_xml, ns)
flow_item = cls(project_id)
flow_item._set_values(
id_,
name,
description,
webpage_url,
created_at,
updated_at,
tags,
None,
project_name,
owner_id,
)
return flow_item

@staticmethod
def _parse_element(flow_xml, ns):
id_ = flow_xml.get("id", None)
Expand Down
Loading

0 comments on commit cc62a50

Please sign in to comment.