Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_filter_equals(self):
with requests_mock.mock() as m:
m.get(requests_mock.ANY)
url = "http://test/api/2.3/sites/dad65087-b08b-4603-af4e-2887b8aafc67/workbooks"
opts = TSC.RequestOptions(pagesize=13, pagenumber=13)
opts.filter.add(TSC.Filter(TSC.RequestOptions.Field.Name,
TSC.RequestOptions.Operator.Equals,
'Superstore'))
resp = self.server.workbooks._make_request(requests.get,
url,
content=None,
request_object=opts,
auth_token='j80k54ll2lfMZ0tv97mlPvvSCRyD0DOM',
content_type='text/xml')
self.assertEqual(resp.request.query, 'pagenumber=13&pagesize=13&filter=name:eq:superstore')
def test_filter_combo(self):
with requests_mock.mock() as m:
m.get(requests_mock.ANY)
url = "http://test/api/2.3/sites/dad65087-b08b-4603-af4e-2887b8aafc67/users"
opts = TSC.RequestOptions(pagesize=13, pagenumber=13)
opts.filter.add(TSC.Filter(TSC.RequestOptions.Field.LastLogin,
TSC.RequestOptions.Operator.GreaterThanOrEqual,
'2017-01-15T00:00:00:00Z'))
opts.filter.add(TSC.Filter(TSC.RequestOptions.Field.SiteRole,
TSC.RequestOptions.Operator.Equals,
'Publisher'))
resp = self.server.workbooks._make_request(requests.get,
url,
content=None,
request_object=opts,
auth_token='j80k54ll2lfMZ0tv97mlPvvSCRyD0DOM',
content_type='text/xml')
m.get(self.baseurl + "?pageNumber=2&pageSize=1", text=page_2)
m.get(self.baseurl + "?pageNumber=3&pageSize=1", text=page_3)
m.get(self.baseurl + "?pageNumber=1&pageSize=3", text=page_1)
# Starting on page 2 should get 2 out of 3
opts = TSC.RequestOptions(2, 1)
workbooks = list(TSC.Pager(self.server.workbooks, opts))
self.assertTrue(len(workbooks) == 2)
# Check that the workbooks are the 2 we think they should be
wb2, wb3 = workbooks
self.assertEqual(wb2.name, 'Page2Workbook')
self.assertEqual(wb3.name, 'Page3Workbook')
# Starting on 1 with pagesize of 3 should get all 3
opts = TSC.RequestOptions(1, 3)
workbooks = list(TSC.Pager(self.server.workbooks, opts))
self.assertTrue(len(workbooks) == 3)
wb1, wb2, wb3 = workbooks
self.assertEqual(wb1.name, 'Page1Workbook')
self.assertEqual(wb2.name, 'Page2Workbook')
self.assertEqual(wb3.name, 'Page3Workbook')
# Starting on 3 with pagesize of 1 should get the last item
opts = TSC.RequestOptions(3, 1)
workbooks = list(TSC.Pager(self.server.workbooks, opts))
self.assertTrue(len(workbooks) == 1)
# Should have the last workbook
wb3 = workbooks.pop()
self.assertEqual(wb3.name, 'Page3Workbook')
def test_page_size(self):
with open(PAGE_SIZE_XML, 'rb') as f:
response_xml = f.read().decode('utf-8')
with requests_mock.mock() as m:
m.get(self.baseurl + '/views?pageSize=5', text=response_xml)
req_option = TSC.RequestOptions().page_size(5)
all_views, pagination_item = self.server.views.get(req_option)
self.assertEqual(1, pagination_item.page_number)
self.assertEqual(5, pagination_item.page_size)
self.assertEqual(33, pagination_item.total_available)
self.assertEqual(5, len(all_views))
password = getpass.getpass("Password: ")
# Set logging level based on user input, or error by default
logging_level = getattr(logging, args.logging_level.upper())
logging.basicConfig(level=logging_level)
# Step 1: Sign in to both sites on server
tableau_auth = TSC.TableauAuth(args.username, password)
source_server = TSC.Server(args.server)
dest_server = TSC.Server(args.server)
with source_server.auth.sign_in(tableau_auth):
# Step 2: Query workbook to move
req_option = TSC.RequestOptions()
req_option.filter.add(TSC.Filter(TSC.RequestOptions.Field.Name,
TSC.RequestOptions.Operator.Equals, args.workbook_name))
all_workbooks, pagination_item = source_server.workbooks.get(req_option)
# Step 3: Download workbook to a temp directory
if len(all_workbooks) == 0:
print('No workbook named {} found.'.format(args.workbook_name))
else:
tmpdir = tempfile.mkdtemp()
try:
workbook_path = source_server.workbooks.download(all_workbooks[0].id, tmpdir)
# Step 4: Check if destination site exists, then sign in to the site
pagination_info, all_sites = source_server.sites.get()
found_destination_site = any((True for site in all_sites if
args.destination_site.lower() == site.content_url.lower()))
parser.add_argument('--logging-level', '-l', choices=['debug', 'info', 'error'], default='error',
help='desired logging level (set to error by default)')
args = parser.parse_args()
password = getpass.getpass("Password: ")
# Set logging level based on user input, or error by default
logging_level = getattr(logging, args.logging_level.upper())
logging.basicConfig(level=logging_level)
# Step 1: Sign in to server
tableau_auth = TSC.TableauAuth(args.username, password)
server = TSC.Server(args.server)
with server.auth.sign_in(tableau_auth):
# Step 2: Query workbook to move
req_option = TSC.RequestOptions()
req_option.filter.add(TSC.Filter(TSC.RequestOptions.Field.Name,
TSC.RequestOptions.Operator.Equals, args.workbook_name))
all_workbooks, pagination_item = server.workbooks.get(req_option)
# Step 3: Find destination project
all_projects, pagination_item = server.projects.get()
dest_project = next((project for project in all_projects if project.name == args.destination_project), None)
if dest_project is not None:
# Step 4: Update workbook with new project id
if all_workbooks:
print("Old project: {}".format(all_workbooks[0].project_name))
all_workbooks[0].project_id = dest_project.id
target_workbook = server.workbooks.update(all_workbooks[0])
print("New project: {}".format(target_workbook.project_name))
else:
# Set logging level based on user input, or error by default
logging_level = getattr(logging, args.logging_level.upper())
logging.basicConfig(level=logging_level)
# Step 1: Sign in to server.
site_id = args.site_id
if not site_id:
site_id = ""
tableau_auth = TSC.TableauAuth(args.username, password, site_id=site_id)
server = TSC.Server(args.server)
# The new endpoint was introduced in Version 2.5
server.version = "2.5"
with server.auth.sign_in(tableau_auth):
# Step 2: Query for the view that we want an image of
req_option = TSC.RequestOptions()
req_option.filter.add(TSC.Filter(TSC.RequestOptions.Field.Name,
TSC.RequestOptions.Operator.Equals, args.view_name))
all_views, pagination_item = server.views.get(req_option)
if not all_views:
raise LookupError("View with the specified name was not found.")
view_item = all_views[0]
# Step 3: Query the image endpoint and save the image to the specified location
image_req_option = TSC.ImageRequestOptions(imageresolution=TSC.ImageRequestOptions.Resolution.High)
server.views.populate_image(view_item, image_req_option)
with open(args.filepath, "wb") as image_file:
image_file.write(view_item.image)
print("View image saved to {0}".format(args.filepath))
# now that we're inside a subcommand, ignore the first TWO argvs, ie the command (tabcmd) and the subcommand (delete)
args = parser.parse_args(sys.argv[2:])
# print('Running tabcmd {0}, type={1}, name={2}, project={3}'.format(sys._getframe().f_code.co_name, args.type, args.name, args.project))
server = self.set_server_from_session_file()
# Get the project item for the named project
our_proj = None
for p in TSC.Pager(server.projects):
if p.name == args.project:
our_proj = p
break
# Set-up our filter so we only get items with the name passed in
request_options = TSC.RequestOptions()
request_options.filter.add(TSC.Filter(TSC.RequestOptions.Field.Name, TSC.RequestOptions.Operator.Equals, args.name))
# Delete workbook
if args.type == 'w':
all_workbooks = list(TSC.Pager(server.workbooks, request_options))
for w in all_workbooks:
if w.project_id == our_proj.id:
server.workbooks.delete(w.id)
else:
all_datasources = list(TSC.Pager(server.datasources, request_options))
for d in all_datasources:
if d.project_id == our_proj.id:
server.datasources.delete(d.id)
def update_workbooks_by_names(name_list, server, materialized_views_config):
workbook_names = sanitize_workbook_list(name_list, "name")
for workbook_name in workbook_names:
req_option = TSC.RequestOptions()
req_option.filter.add(TSC.Filter(TSC.RequestOptions.Field.Name,
TSC.RequestOptions.Operator.Equals,
workbook_name.rstrip()))
workbooks = list(TSC.Pager(server.workbooks, req_option))
if len(workbooks) == 0:
print("Cannot find workbook name: {}, each line should only contain one workbook name"
.format(workbook_name))
for workbook in workbooks:
workbook.materialized_views_config = materialized_views_config
server.workbooks.update(workbook)
print("Updated materialized views settings for workbook: {}".format(workbook.name))
print('\n')