|
| 1 | +#### |
| 2 | +# This script demonstrates how to filter groups using the Tableau |
| 3 | +# Server Client. |
| 4 | +# |
| 5 | +# To run the script, you must have installed Python 2.7.9 or later. |
| 6 | +#### |
| 7 | + |
| 8 | + |
| 9 | +import argparse |
| 10 | +import getpass |
| 11 | +import logging |
| 12 | + |
| 13 | +import tableauserverclient as TSC |
| 14 | + |
| 15 | + |
| 16 | +def create_example_group(group_name='Example Group', server=None): |
| 17 | + new_group = TSC.GroupItem(group_name) |
| 18 | + try: |
| 19 | + new_group = server.groups.create(new_group) |
| 20 | + print('Created a new project called: \'%s\'' % group_name) |
| 21 | + print(new_group) |
| 22 | + except TSC.ServerResponseError: |
| 23 | + print('Group \'%s\' already existed' % group_name) |
| 24 | + |
| 25 | + |
| 26 | +def main(): |
| 27 | + parser = argparse.ArgumentParser(description='Filter on groups') |
| 28 | + parser.add_argument('--server', '-s', required=True, help='server address') |
| 29 | + parser.add_argument('--username', '-u', required=True, help='username to sign into server') |
| 30 | + parser.add_argument('--logging-level', '-l', choices=['debug', 'info', 'error'], default='error', |
| 31 | + help='desired logging level (set to error by default)') |
| 32 | + parser.add_argument('-p', default=None) |
| 33 | + args = parser.parse_args() |
| 34 | + |
| 35 | + if args.p is None: |
| 36 | + password = getpass.getpass("Password: ") |
| 37 | + else: |
| 38 | + password = args.p |
| 39 | + |
| 40 | + # Set logging level based on user input, or error by default |
| 41 | + logging_level = getattr(logging, args.logging_level.upper()) |
| 42 | + logging.basicConfig(level=logging_level) |
| 43 | + |
| 44 | + tableau_auth = TSC.TableauAuth(args.username, password) |
| 45 | + server = TSC.Server(args.server) |
| 46 | + |
| 47 | + with server.auth.sign_in(tableau_auth): |
| 48 | + |
| 49 | + # Determine and use the highest api version for the server |
| 50 | + server.use_server_version() |
| 51 | + |
| 52 | + group_name = 'SALES NORTHWEST' |
| 53 | + # Try to create a group named "SALES NORTHWEST" |
| 54 | + create_example_group(group_name, server) |
| 55 | + |
| 56 | + group_name = 'SALES ROMANIA' |
| 57 | + # Try to create a group named "SALES ROMANIA" |
| 58 | + create_example_group(group_name, server) |
| 59 | + |
| 60 | + # URL Encode the name of the group that we want to filter on |
| 61 | + # i.e. turn spaces into plus signs |
| 62 | + filter_group_name = 'SALES+ROMANIA' |
| 63 | + options = TSC.RequestOptions() |
| 64 | + options.filter.add(TSC.Filter(TSC.RequestOptions.Field.Name, |
| 65 | + TSC.RequestOptions.Operator.Equals, |
| 66 | + filter_group_name)) |
| 67 | + |
| 68 | + filtered_groups, _ = server.groups.get(req_options=options) |
| 69 | + # Result can either be a matching group or an empty list |
| 70 | + if filtered_groups: |
| 71 | + group_name = filtered_groups.pop().name |
| 72 | + print(group_name) |
| 73 | + else: |
| 74 | + error = "No project named '{}' found".format(filter_group_name) |
| 75 | + print(error) |
| 76 | + |
| 77 | + options = TSC.RequestOptions() |
| 78 | + options.filter.add(TSC.Filter(TSC.RequestOptions.Field.Name, |
| 79 | + TSC.RequestOptions.Operator.In, |
| 80 | + ['SALES+NORTHWEST', 'SALES+ROMANIA', 'this_group'])) |
| 81 | + |
| 82 | + options.sort.add(TSC.Sort(TSC.RequestOptions.Field.Name, |
| 83 | + TSC.RequestOptions.Direction.Desc)) |
| 84 | + |
| 85 | + matching_groups, pagination_item = server.groups.get(req_options=options) |
| 86 | + print('Filtered groups are:') |
| 87 | + for group in matching_groups: |
| 88 | + print(group.name) |
| 89 | + |
| 90 | +if __name__ == '__main__': |
| 91 | + main() |
0 commit comments