Skip to content
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

Jac/small things #1215

Merged
merged 6 commits into from
Apr 13, 2023
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
7 changes: 3 additions & 4 deletions tableauserverclient/models/datasource_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def __repr__(self):
self.project_id,
)

def __init__(self, project_id: str, name: Optional[str] = None) -> None:
def __init__(self, project_id: Optional[str] = None, name: Optional[str] = None) -> None:
self._ask_data_enablement = None
self._certified = None
self._certification_note = None
Expand Down Expand Up @@ -135,12 +135,11 @@ def id(self) -> Optional[str]:
return self._id

@property
def project_id(self) -> str:
def project_id(self) -> Optional[str]:
return self._project_id

@project_id.setter
@property_not_nullable
def project_id(self, value: str):
def project_id(self, value: Optional[str]):
self._project_id = value

@property
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 @@ -309,6 +309,7 @@ def publish(
as_job: bool = False,
hidden_views: Optional[Sequence[str]] = None,
skip_connection_check: bool = False,
parameters=None,
):
if connection_credentials is not None:
import warnings
Expand Down Expand Up @@ -412,7 +413,7 @@ def publish(

# Send the publishing request to server
try:
server_response = self.post_request(url, xml_request, content_type)
server_response = self.post_request(url, xml_request, content_type, parameters)
except InternalServerError as err:
if err.code == 504 and not as_job:
err.content = "Timeout error while publishing. Please use asynchronous publishing to avoid timeouts."
Expand Down
19 changes: 14 additions & 5 deletions tableauserverclient/server/request_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
if TYPE_CHECKING:
from tableauserverclient.server import Server

# this file could be largely replaced if we were willing to import the huge file from generateDS


def _add_multipart(parts: Dict) -> Tuple[Any, str]:
mime_multipart_parts = list()
Expand Down Expand Up @@ -146,21 +148,28 @@ def update_req(self, database_item):


class DatasourceRequest(object):
def _generate_xml(self, datasource_item, connection_credentials=None, connections=None):
def _generate_xml(self, datasource_item: DatasourceItem, connection_credentials=None, connections=None):
xml_request = ET.Element("tsRequest")
datasource_element = ET.SubElement(xml_request, "datasource")
datasource_element.attrib["name"] = datasource_item.name
if datasource_item.name:
datasource_element.attrib["name"] = datasource_item.name
if datasource_item.description:
datasource_element.attrib["description"] = str(datasource_item.description)
if datasource_item.use_remote_query_agent is not None:
datasource_element.attrib["useRemoteQueryAgent"] = str(datasource_item.use_remote_query_agent).lower()

if datasource_item.ask_data_enablement:
ask_data_element = ET.SubElement(datasource_element, "askData")
ask_data_element.attrib["enablement"] = datasource_item.ask_data_enablement
ask_data_element.attrib["enablement"] = datasource_item.ask_data_enablement.__str__()

project_element = ET.SubElement(datasource_element, "project")
project_element.attrib["id"] = datasource_item.project_id
if datasource_item.certified:
datasource_element.attrib["isCertified"] = datasource_item.certified.__str__()
if datasource_item.certification_note:
datasource_element.attrib["certificationNote"] = datasource_item.certification_note

if datasource_item.project_id:
project_element = ET.SubElement(datasource_element, "project")
project_element.attrib["id"] = datasource_item.project_id

if connection_credentials is not None and connections is not None:
raise RuntimeError("You cannot set both `connections` and `connection_credentials`")
Expand Down
3 changes: 2 additions & 1 deletion tableauserverclient/server/request_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class Operator:
class Field:
Args = "args"
CompletedAt = "completedAt"
ContentUrl = "contentUrl"
CreatedAt = "createdAt"
DomainName = "domainName"
DomainNickname = "domainNickname"
Expand Down Expand Up @@ -147,7 +148,7 @@ def get_query_params(self):
return params


class ExcelRequestOptions(RequestOptionsBase):
class ExcelRequestOptions(_FilterOptionsBase):
def __init__(self, maxage: int = -1) -> None:
super().__init__()
self.max_age = maxage
Expand Down
8 changes: 3 additions & 5 deletions test/test_datasource_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@


class DatasourceModelTests(unittest.TestCase):
def test_invalid_project_id(self):
self.assertRaises(ValueError, TSC.DatasourceItem, None)
datasource = TSC.DatasourceItem("10")
with self.assertRaises(ValueError):
datasource.project_id = None
def test_nullable_project_id(self):
datasource = TSC.DatasourceItem(name="10")
self.assertEqual(datasource.project_id, None)

def test_require_boolean_flag_bridge_fail(self):
datasource = TSC.DatasourceItem("10")
Expand Down
16 changes: 16 additions & 0 deletions test/test_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,3 +299,19 @@ def test_populate_excel(self) -> None:

excel_file = b"".join(single_view.excel)
self.assertEqual(response, excel_file)

def test_filter_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.ExcelRequestOptions(maxage=1)
request_option.vf("stuff", "1")
self.server.views.populate_excel(single_view, request_option)

excel_file = b"".join(single_view.excel)
self.assertEqual(response, excel_file)