forked from tableau/server-client-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter_sort_groups.py
114 lines (93 loc) · 4.05 KB
/
filter_sort_groups.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
####
# This script demonstrates how to filter and sort groups using the Tableau
# Server Client.
#
# To run the script, you must have installed Python 3.7 or later.
####
import argparse
import logging
import urllib.parse
import tableauserverclient as TSC
def create_example_group(group_name="Example Group", server=None):
new_group = TSC.GroupItem(group_name)
try:
new_group = server.groups.create(new_group)
print("Created a new project called: '%s'" % group_name)
print(new_group)
except TSC.ServerResponseError:
print("Group '%s' already existed" % group_name)
def main():
parser = argparse.ArgumentParser(description="Filter and sort groups.")
# Common options; please keep those in sync across all samples
parser.add_argument("--server", "-s", required=True, help="server address")
parser.add_argument("--site", "-S", help="site name")
parser.add_argument(
"--token-name", "-p", required=True, help="name of the personal access token used to sign into the server"
)
parser.add_argument(
"--token-value", "-v", required=True, 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)",
)
# Options specific to this sample
# This sample has no additional options, yet. If you add some, please add them here
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)
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):
group_name = "SALES NORTHWEST"
# Try to create a group named "SALES NORTHWEST"
create_example_group(group_name, server)
group_name = "SALES ROMANIA"
# Try to create a group named "SALES ROMANIA"
create_example_group(group_name, server)
# URL Encode the name of the group that we want to filter on
# i.e. turn spaces into plus signs
filter_group_name = urllib.parse.quote_plus(group_name)
options = TSC.RequestOptions()
options.filter.add(
TSC.Filter(TSC.RequestOptions.Field.Name, TSC.RequestOptions.Operator.Equals, filter_group_name)
)
filtered_groups, _ = server.groups.get(req_options=options)
# Result can either be a matching group or an empty list
if filtered_groups:
group_name = filtered_groups.pop().name
print(group_name)
else:
error = "No project named '{}' found".format(filter_group_name)
print(error)
# Or, try the above with the django style filtering
try:
group = server.groups.filter(name=filter_group_name)[0]
except IndexError:
print(f"No project named '{filter_group_name}' found")
else:
print(group.name)
options = TSC.RequestOptions()
options.filter.add(
TSC.Filter(
TSC.RequestOptions.Field.Name,
TSC.RequestOptions.Operator.In,
["SALES+NORTHWEST", "SALES+ROMANIA", "this_group"],
)
)
options.sort.add(TSC.Sort(TSC.RequestOptions.Field.Name, TSC.RequestOptions.Direction.Desc))
matching_groups, pagination_item = server.groups.get(req_options=options)
print("Filtered groups are:")
for group in matching_groups:
print(group.name)
# or, try the above with the django style filtering.
groups = ["SALES NORTHWEST", "SALES ROMANIA", "this_group"]
groups = [urllib.parse.quote_plus(group) for group in groups]
for group in server.groups.filter(name__in=groups).sort("-name"):
print(group.name)
if __name__ == "__main__":
main()