-
Notifications
You must be signed in to change notification settings - Fork 436
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
60258e7
Sample group filtering script
a8d013a
Added comments and specific error catching
b695db2
Renamed the file to follow the naming convention
440d8e8
commented on return type, example group creation, cleaned up code
1efe53a
Better way to call and catch return from rest api
c07cfba
Proper printing instructions
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): | ||
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() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.