class TableauServerConnection: FILE_PATH = os.path.dirname(Tests.__file__) username = os.getenv('USERNAME') password = os.getenv('PASSWORD') server_url = os.getenv('SERVER_URL') tableau_auth = TSC.TableauAuth('mll25', 'BobRoss1125!') server = TSC.Server('https://casanalyticsp01.cas.org/') # Current server version : 3.3 server.use_server_version() req_option = TSC.RequestOptions(pagesize=1000) csv_req_option = TSC.CSVRequestOptions() image_req_option = TSC.ImageRequestOptions(imageresolution=TSC.ImageRequestOptions.Resolution.High) image_req_option.vf('Country', 'AUSTRALIA') csv_req_option.vf('Country','AUSTRALIA') with server.auth.sign_in(tableau_auth): all_workbooks, pagination_item = server.workbooks.get() wfound = [workbook for workbook in all_workbooks if workbook.name == 'Enterprise Success Dashboard LIVE'] # pprint(vars(wfound[0])) wconnect = server.workbooks.get_by_id('94d2abea-bced-46d1-831c-edce132b2864') server.workbooks.populate_views(wconnect) vfound = [view for view in wconnect.views if len(view.name) > 0] view_item = vfound[21] server.views.populate_csv(view_item, csv_req_option) with open(f'{FILE_PATH}/CCDAus.csv', 'wb') as f: if os.path.exists(f): os.remove(f) f.write(b''.join(view_item.csv))
def test_populate_csv(self): with open(POPULATE_CSV, 'rb') as f: response = f.read() with requests_mock.mock() as m: m.get(self.baseurl + '/d79634e1-6063-4ec9-95ff-50acbf609ff5/data?maxAge=1', content=response) single_view = TSC.ViewItem() single_view._id = 'd79634e1-6063-4ec9-95ff-50acbf609ff5' request_option = TSC.CSVRequestOptions(maxage=1) self.server.views.populate_csv(single_view, request_option) csv_file = b"".join(single_view.csv) self.assertEqual(response, csv_file)
def generate_png(logger, server, args): view = GetUrl.get_view_url(args.url) try: view_item: TSC.ViewItem = GetUrl.get_view_by_content_url( logger, server, view) logger.debug( _("content_type.view") + ": {}".format(view_item.name)) req_option_csv = TSC.CSVRequestOptions(maxage=1) server.views.populate_csv(view_item, req_option_csv) filename = GetUrl.filename_from_args(args.filename, view_item.name, "png") with open(filename, "wb") as f: f.write(view_item.png) logger.info(_("export.success").format(view_item.name, filename)) except TSC.ServerResponseError as e: GetUrl.exit_with_error( logger, _("publish.errors.unexpected_server_response"), e)
def test_populate_excel(self) -> None: self.server.version = "3.8" self.baseurl = self.server.views.baseurl with open(POPULATE_EXCEL, "rb") as f: response = f.read() with requests_mock.mock() as m: m.get( self.baseurl + "/d79634e1-6063-4ec9-95ff-50acbf609ff5/crosstab/excel?maxAge=1", content=response) single_view = TSC.ViewItem() single_view._id = "d79634e1-6063-4ec9-95ff-50acbf609ff5" request_option = TSC.CSVRequestOptions(maxage=1) self.server.views.populate_excel(single_view, request_option) excel_file = b"".join(single_view.excel) self.assertEqual(response, excel_file)
def download_csv(server, view_item): csv = TSC.CSVRequestOptions(maxage=1) server.views.populate_csv(view_item, csv) return view_item.csv