Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions samples/initialize_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import tableauserverclient as TSC



def main():
parser = argparse.ArgumentParser(description='Initialize a server with content.')
parser.add_argument('--server', '-s', required=True, help='server address')
Expand Down Expand Up @@ -68,7 +67,8 @@ def main():
################################################################################
# Step 4: Create the project we need only if it doesn't exist
################################################################################
import time; time.sleep(2) # sad panda...something about eventually consistent model
import time
time.sleep(2) # sad panda...something about eventually consistent model
all_projects = TSC.Pager(server_upload.projects)
project = next((p for p in all_projects if p.name.lower() == args.project.lower()), None)

Expand Down
6 changes: 6 additions & 0 deletions tableauserverclient/filesys_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
ALLOWED_SPECIAL = (' ', '.', '_', '-')


def to_filename(string_to_sanitize):
sanitized = (c for c in string_to_sanitize if c.isalnum() or c in ALLOWED_SPECIAL)
return "".join(sanitized)
3 changes: 2 additions & 1 deletion tableauserverclient/server/endpoint/datasources_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from .exceptions import MissingRequiredFieldError
from .fileuploads_endpoint import Fileuploads
from .. import RequestFactory, DatasourceItem, PaginationItem, ConnectionItem
from ...filesys_helpers import to_filename
import os
import logging
import copy
Expand Down Expand Up @@ -77,7 +78,7 @@ def download(self, datasource_id, filepath=None, no_extract=False):

with closing(self.get_request(url, parameters={'stream': True})) as server_response:
_, params = cgi.parse_header(server_response.headers['Content-Disposition'])
filename = os.path.basename(params['filename'])
filename = to_filename(os.path.basename(params['filename']))
if filepath is None:
filepath = filename
elif os.path.isdir(filepath):
Expand Down
3 changes: 2 additions & 1 deletion tableauserverclient/server/endpoint/workbooks_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from .fileuploads_endpoint import Fileuploads
from .. import RequestFactory, WorkbookItem, ConnectionItem, ViewItem, PaginationItem
from ...models.tag_item import TagItem
from ...filesys_helpers import to_filename
import os
import logging
import copy
Expand Down Expand Up @@ -112,7 +113,7 @@ def download(self, workbook_id, filepath=None, no_extract=False):

with closing(self.get_request(url, parameters={"stream": True})) as server_response:
_, params = cgi.parse_header(server_response.headers['Content-Disposition'])
filename = os.path.basename(params['filename'])
filename = to_filename(os.path.basename(params['filename']))
if filepath is None:
filepath = filename
elif os.path.isdir(filepath):
Expand Down
11 changes: 11 additions & 0 deletions test/test_datasource.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,17 @@ def test_download(self):
self.assertTrue(os.path.exists(file_path))
os.remove(file_path)

def test_download_sanitizes_name(self):
filename = "Name,With,Commas.tds"
disposition = 'name="tableau_workbook"; filename="{}"'.format(filename)
with requests_mock.mock() as m:
m.get(self.baseurl + '/1f951daf-4061-451a-9df1-69a8062664f2/content',
headers={'Content-Disposition': disposition})
file_path = self.server.datasources.download('1f951daf-4061-451a-9df1-69a8062664f2')
self.assertEqual(os.path.basename(file_path), "NameWithCommas.tds")
self.assertTrue(os.path.exists(file_path))
os.remove(file_path)

def test_download_extract_only(self):
# Pretend we're 2.5 for 'extract_only'
self.server.version = "2.5"
Expand Down
11 changes: 11 additions & 0 deletions test/test_workbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,17 @@ def test_download(self):
self.assertTrue(os.path.exists(file_path))
os.remove(file_path)

def test_download_sanitizes_name(self):
filename = "Name,With,Commas.twbx"
disposition = 'name="tableau_workbook"; filename="{}"'.format(filename)
with requests_mock.mock() as m:
m.get(self.baseurl + '/1f951daf-4061-451a-9df1-69a8062664f2/content',
headers={'Content-Disposition': disposition})
file_path = self.server.workbooks.download('1f951daf-4061-451a-9df1-69a8062664f2')
self.assertEqual(os.path.basename(file_path), "NameWithCommas.twbx")
self.assertTrue(os.path.exists(file_path))
os.remove(file_path)

def test_download_extract_only(self):
# Pretend we're 2.5 for 'extract_only'
self.server.version = "2.5"
Expand Down