8000 Sample group filtering by acpana · Pull Request #199 · tableau/server-client-python · GitHub
[go: up one dir, main page]

Skip to content

Sample group filtering #199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 23, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions samples/filter_sort_groups.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
####
# 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 create_example_group(group_name='Example Group', server=None):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None isn't actually a safe default, this should just be a standard required parameter.

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')
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()

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):

# 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"
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 = 'SALES+ROMANIA'
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)

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()
0