From 60258e7a3045db43a44088b0697842526acb7c10 Mon Sep 17 00:00:00 2001 From: Alex Pana Date: Mon, 12 Jun 2017 14:29:10 -0700 Subject: [PATCH 1/6] Sample group filtering script --- samples/group_filtering.py | 82 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 samples/group_filtering.py diff --git a/samples/group_filtering.py b/samples/group_filtering.py new file mode 100644 index 000000000..e926162d7 --- /dev/null +++ b/samples/group_filtering.py @@ -0,0 +1,82 @@ +#### +# This script demonstrates how to filter groups using the Tableau +# Server Client. +# +# To run the script, you must have installed Python 2.7.9 or later. +#### + + +import argparse +import getpass +import logging + +import tableauserverclient as TSC + + +def main(): + + parser = argparse.ArgumentParser(description='Filter on groups') + parser.add_argument('--server', '-s', required=True, help='server address') + parser.add_argument('--username', '-u', required=True, help='username to sign into server') + parser.add_argument('--logging-level', '-l', choices=['debug', 'info', 'error'], default='error', + help='desired logging level (set to error by default)') + parser.add_argument('-p', default=None) + args = parser.parse_args() + + password = '' + if args.p is None: + password = getpass.getpass("Password: ") + else: + password = args.p + + # 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.TableauAuth(args.username, password) + server = TSC.Server(args.server) + + with server.auth.sign_in(tableau_auth): + server.version = '2.7' + group_name = 'SALES NORTHWEST' + # Try to create a group named "SALES NORTHWEST" + try: + group1 = TSC.GroupItem(group_name) + group1 = server.groups.create(group1) + print(group1) + except: + print('Group \'%s\' already existed' % group_name) + + group_name = 'SALES ROMANIA' + # Try to create a group named "SALES ROMANIA" + try: + group2 = TSC.GroupItem(group_name) + group2 = server.groups.create(group2) + print(group2) + except: + print('Group \'%s\' already existed' % group_name) + + group_name = 'SALES+ROMANIA' + options = TSC.RequestOptions() + options.filter.add(TSC.Filter(TSC.RequestOptions.Field.Name, + TSC.RequestOptions.Operator.Equals, + group_name)) + + filtered_group_paged = server.groups.get(req_options=options) + print(filtered_group_paged[0][0].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) + +if __name__ == '__main__': + main() From a8d013a76ba616888411b0410d9baefc35f5955d Mon Sep 17 00:00:00 2001 From: Alex Pana Date: Wed, 14 Jun 2017 14:52:14 -0700 Subject: [PATCH 2/6] Added comments and specific error catching --- samples/group_filtering.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/samples/group_filtering.py b/samples/group_filtering.py index e926162d7..b613dc832 100644 --- a/samples/group_filtering.py +++ b/samples/group_filtering.py @@ -44,8 +44,8 @@ def main(): group1 = TSC.GroupItem(group_name) group1 = server.groups.create(group1) print(group1) - except: - print('Group \'%s\' already existed' % group_name) + except TSC.ServerResponseError: + print('Group \'%s\' already existed' % group_name) group_name = 'SALES ROMANIA' # Try to create a group named "SALES ROMANIA" @@ -53,16 +53,20 @@ def main(): group2 = TSC.GroupItem(group_name) group2 = server.groups.create(group2) print(group2) - except: + except TSC.ServerResponseError: print('Group \'%s\' already existed' % group_name) - group_name = 'SALES+ROMANIA' + # URL Encode the name of the group that we want to filter on + # i.e. turn spaces into plus signs + filter_group_name = 'SALES+ROMANIA' options = TSC.RequestOptions() options.filter.add(TSC.Filter(TSC.RequestOptions.Field.Name, TSC.RequestOptions.Operator.Equals, - group_name)) + filter_group_name)) filtered_group_paged = server.groups.get(req_options=options) + + # Return type is a tuple with the first entry as a list of matching groups print(filtered_group_paged[0][0].name) options = TSC.RequestOptions() From b695db287e65570f4fc9e4090745e9e4f025340e Mon Sep 17 00:00:00 2001 From: Alex Pana Date: Wed, 14 Jun 2017 15:31:47 -0700 Subject: [PATCH 3/6] Renamed the file to follow the naming convention --- samples/{group_filtering.py => filter_sort_groups.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename samples/{group_filtering.py => filter_sort_groups.py} (100%) diff --git a/samples/group_filtering.py b/samples/filter_sort_groups.py similarity index 100% rename from samples/group_filtering.py rename to samples/filter_sort_groups.py From 440d8e8f0ebddec441ed69b012c5119ebc3cb1c9 Mon Sep 17 00:00:00 2001 From: Alex Pana Date: Wed, 21 Jun 2017 14:37:10 -0700 Subject: [PATCH 4/6] commented on return type, example group creation, cleaned up code --- samples/filter_sort_groups.py | 38 ++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/samples/filter_sort_groups.py b/samples/filter_sort_groups.py index b613dc832..fe2005a87 100644 --- a/samples/filter_sort_groups.py +++ b/samples/filter_sort_groups.py @@ -13,8 +13,17 @@ import tableauserverclient as TSC -def main(): +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 on groups') parser.add_argument('--server', '-s', required=True, help='server address') parser.add_argument('--username', '-u', required=True, help='username to sign into server') @@ -23,7 +32,6 @@ def main(): parser.add_argument('-p', default=None) args = parser.parse_args() - password = '' if args.p is None: password = getpass.getpass("Password: ") else: @@ -37,36 +45,30 @@ def main(): server = TSC.Server(args.server) with server.auth.sign_in(tableau_auth): - server.version = '2.7' + + # Determine and use the highest api version for the server + server.use_server_version() + group_name = 'SALES NORTHWEST' # Try to create a group named "SALES NORTHWEST" - try: - group1 = TSC.GroupItem(group_name) - group1 = server.groups.create(group1) - print(group1) - except TSC.ServerResponseError: - print('Group \'%s\' already existed' % group_name) + create_example_group(group_name, server) group_name = 'SALES ROMANIA' # Try to create a group named "SALES ROMANIA" - try: - group2 = TSC.GroupItem(group_name) - group2 = server.groups.create(group2) - print(group2) - except TSC.ServerResponseError: - print('Group \'%s\' already existed' % group_name) + 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 = 'SALES+ROMANIA' options = TSC.RequestOptions() options.filter.add(TSC.Filter(TSC.RequestOptions.Field.Name, - TSC.RequestOptions.Operator.Equals, - filter_group_name)) + TSC.RequestOptions.Operator.Equals, + filter_group_name)) + # Return type is a tuple with the first entry as a list of matching groups filtered_group_paged = server.groups.get(req_options=options) - # Return type is a tuple with the first entry as a list of matching groups + # Access the first list object of the first tuple return type print(filtered_group_paged[0][0].name) options = TSC.RequestOptions() From 1efe53a1fb2058f50502d01682cb0f4917207b15 Mon Sep 17 00:00:00 2001 From: Alex Pana Date: Thu, 22 Jun 2017 13:55:57 -0700 Subject: [PATCH 5/6] Better way to call and catch return from rest api --- samples/filter_sort_groups.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/samples/filter_sort_groups.py b/samples/filter_sort_groups.py index fe2005a87..12bfa44a1 100644 --- a/samples/filter_sort_groups.py +++ b/samples/filter_sort_groups.py @@ -65,11 +65,10 @@ def main(): TSC.RequestOptions.Operator.Equals, filter_group_name)) - # Return type is a tuple with the first entry as a list of matching groups - filtered_group_paged = server.groups.get(req_options=options) - - # Access the first list object of the first tuple return type - print(filtered_group_paged[0][0].name) + filtered_groups, _ = server.groups.get(req_options=options) + # Result can either be a matching group or none + group_name = filtered_groups.pop().name if len(filtered_groups) != 0 else 'No project named \'' + str(filter_group_name) + '\' found ' + print(group_name) options = TSC.RequestOptions() options.filter.add(TSC.Filter(TSC.RequestOptions.Field.Name, From c07cfba1925bbb452f4c13cffd556757a91096eb Mon Sep 17 00:00:00 2001 From: Alex Pana Date: Thu, 22 Jun 2017 17:54:48 -0700 Subject: [PATCH 6/6] Proper printing instructions --- samples/filter_sort_groups.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/samples/filter_sort_groups.py b/samples/filter_sort_groups.py index 12bfa44a1..6ed6fc773 100644 --- a/samples/filter_sort_groups.py +++ b/samples/filter_sort_groups.py @@ -66,9 +66,13 @@ def main(): filter_group_name)) filtered_groups, _ = server.groups.get(req_options=options) - # Result can either be a matching group or none - group_name = filtered_groups.pop().name if len(filtered_groups) != 0 else 'No project named \'' + str(filter_group_name) + '\' found ' - print(group_name) + # 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) options = TSC.RequestOptions() options.filter.add(TSC.Filter(TSC.RequestOptions.Field.Name,