8000 Updates datasource_item with new fields (#705) · cfmayden/server-client-python@c9d7c6a · GitHub
[go: up one dir, main page]

Skip to content

Commit c9d7c6a

Browse files
author
Chris Shin
authored
Updates datasource_item with new fields (tableau#705)
* Adds new fields and updates parser * Adds new fields to publish and update requests * Adds tests and description field
1 parent 342c0c5 commit c9d7c6a

File tree

5 files changed

+131
-40
lines changed

5 files changed

+131
-40
lines changed

tableauserverclient/models/datasource_item.py

Lines changed: 103 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,50 @@
11
import xml.etree.ElementTree as ET
22
from .exceptions import UnpopulatedPropertyError
3-
from .property_decorators import property_not_nullable, property_is_boolean
3+
from .property_decorators import property_not_nullable, property_is_boolean, property_is_enum
44
from .tag_item import TagItem
55
from ..datetime_helpers import parse_datetime
66
import copy
77

88

99
class DatasourceItem(object):
10+
class AskDataEnablement:
11+
Enabled = 'Enabled'
12+
Disabled = 'Disabled'
13+
SiteDefault = 'SiteDefault'
14+
1015
def __init__(self, project_id, name=None):
16+
self._ask_data_enablement = None
17+
self._certified = None
18+
self._certification_note = None
1119
self._connections = None
1220
self._content_url = None
1321
self._created_at = None
1422
self._datasource_type = None
23+
self._encrypt_extracts = None
24+
self._has_extracts = None
1525
self._id = None
1626
self._initial_tags = set()
1727
self._project_name = None
1828
self._updated_at = None
19-
self._certified = None
20-
self._certification_note = None
29+
self._use_remote_query_agent = None
30+
self._webpage_url = None
31+
self.description = None
2132
self.name = name
2233
self.owner_id = None
2334
self.project_id = project_id
2435
self.tags = set()
2536

2637
self._permissions = None
2738

39+
@property
40+
def ask_data_enablement(self):
41+
return self._ask_data_enablement
42+
43+
@ask_data_enablement.setter
44+
@property_is_enum(AskDataEnablement)
45+
def ask_data_enablement(self, value):
46+
self._ask_data_enablement = value
47+
2848
@property
2949
def connections(self):
3050
if self._connections is None:
@@ -65,6 +85,15 @@ def certification_note(self):
6585
def certification_note(self, value):
6686
self._certification_note = value
6787

88+
@property
89+
def encrypt_extracts(self):
90+
return self._encrypt_extracts
91+
92+
@encrypt_extracts.setter
93+
@property_is_boolean
94+
def encrypt_extracts(self, value):
95+
self._encrypt_extracts = value
96+
6897
@property
6998
def id(self):
7099
return self._id
@@ -90,6 +119,15 @@ def datasource_type(self):
90119
def updated_at(self):
91120
return self._updated_at
92121

122+
@property
123+
def use_remote_query_agent(self):
124+
return self._use_remote_query_agent
125+
126+
@use_remote_query_agent.setter
127+
@property_is_boolean
128+
def use_remote_query_agent(self, value):
129+
self._use_remote_query_agent = value
130+
93131
def _set_connections(self, connections):
94132
self._connections = connections
95133

@@ -100,38 +138,53 @@ def _parse_common_elements(self, datasource_xml, ns):
100138
if not isinstance(datasource_xml, ET.Element):
101139
datasource_xml = ET.fromstring(datasource_xml).find('.//t:datasource', namespaces=ns)
102140
if datasource_xml is not None:
103-
(_, _, _, _, _, updated_at, _, project_id, project_name, owner_id,
104-
certified, certification_note) = self._parse_element(datasource_xml, ns)
105-
self._set_values(None, None, None, None, None, updated_at, None, project_id,
106-
project_name, owner_id, certified, certification_note)
141+
(ask_data_enablement, certified, certification_note, _, _, _, _, encrypt_extracts, has_extracts,
142+
_, _, owner_id, project_id, project_name, _, updated_at, use_remote_query_agent,
143+
webpage_url) = self._parse_element(datasource_xml, ns)
144+
self._set_values(ask_data_enablement, certified, certification_note, None, None, None, None,
145+
encrypt_extracts, has_extracts, None, None, owner_id, project_id, project_name, None,
146+
updated_at, use_remote_query_agent, webpage_url)
107147
return self
108148

109-
def _set_values(self, id, name, datasource_type, content_url, created_at,
110-
updated_at, tags, project_id, project_name, owner_id, certified, certification_note):
111-
if id is not None:
112-
self._id = id
113-
if name:
114-
self.name = name
115-
if datasource_type:
116-
self._datasource_type = datasource_type
149+
def _set_values(self, ask_data_enablement, certified, certification_note, content_url, created_at, datasource_type,
150+
description, encrypt_extracts, has_extracts, id_, name, owner_id, project_id, project_name, tags,
151+
updated_at, use_remote_query_agent, webpage_url):
152+
if ask_data_enablement is not None:
153+
self._ask_data_enablement = ask_data_enablement
154+
if certification_note:
155+
self.certification_note = certification_note
156+
self.certified = certified # Always True/False, not conditional
117157
if content_url:
118158
self._content_url = content_url
119159
if created_at:
120160
self._created_at = created_at
121-
if updated_at:
122-
self._updated_at = updated_at
123-
if tags:
124-
self.tags = tags
125-
self._initial_tags = copy.copy(tags)
161+
if datasource_type:
162+
self._datasource_type = datasource_type
163+
if description:
164+
self.description = description
165+
if encrypt_extracts is not None:
166+
self.encrypt_extracts = str(encrypt_extracts).lower() == 'true'
167+
if has_extracts is not None:
168+
self._has_extracts = str(has_extracts).lower() == 'true'
169+
if id_ is not None:
170+
self._id = id_
171+
if name:
172+
self.name = name
173+
if owner_id:
174+
self.owner_id = owner_id
126175
if project_id:
127176
self.project_id = project_id
128177
if project_name:
129178
self._project_name = project_name
130-
if owner_id:
131-
self.owner_id = owner_id
132-
if certification_note:
133-
self.certification_note = certification_note
134-
self.certified = certified # Always True/False, not conditional
179+
if tags:
180+
self.tags = tags
181+
self._initial_tags = copy.copy(tags)
182+
if updated_at:
183+
self._updated_at = updated_at
184+
if use_remote_query_agent is not None:
185+
self._use_remote_query_agent = str(use_remote_query_agent).lower() == 'true'
186+
if webpage_url:
187+
self._webpage_url = webpage_url
135188

136189
@classmethod
137190
def from_response(cls, resp, ns):
@@ -140,25 +193,32 @@ def from_response(cls, resp, ns):
140193
all_datasource_xml = parsed_response.findall('.//t:datasource', namespaces=ns)
141194

142195
for datasource_xml in all_datasource_xml:
143-
(id_, name, datasource_type, content_url, created_at, updated_at,
144-
tags, project_id, project_name, owner_id,
145-
certified, certification_note) = cls._parse_element(datasource_xml, ns)
196+
(ask_data_enablement, certified, certification_note, content_url, created_at, datasource_type,
197+
description, encrypt_extracts, has_extracts, id_, name, owner_id, project_id, project_name, tags,
198+
updated_at, use_remote_query_agent, webpage_url) = cls._parse_element(datasource_xml, ns)
146199
datasource_item = cls(project_id)
147-
datasource_item._set_values(id_, name, datasource_type, content_url, created_at, updated_at,
148-
tags, None, project_name, owner_id, certified, certification_note)
200+
datasource_item._set_values(ask_data_enablement, certified, certification_note, content_url,
201+
created_at, datasource_type, description, encrypt_extracts,
202+
has_extracts, id_, name, owner_id, None, project_name, tags, updated_at,
203+
use_remote_query_agent, webpage_url)
149204
all_datasource_items.append(datasource_item)
150205
return all_datasource_items
151206

152207
@staticmethod
153208
def _parse_element(datasource_xml, ns):
154-
id_ = datasource_xml.get('id', None)
155-
name = datasource_xml.get('name', None)
156-
datasource_type = datasource_xml.get('type', None)
209+
certification_note = datasource_xml.get('certificationNote', None)
210+
certified = str(datasource_xml.get('isCertified', None)).lower() == 'true'
157211
content_url = datasource_xml.get('contentUrl', None)
158212
created_at = parse_datetime(datasource_xml.get('createdAt', None))
213+
datasource_type = datasource_xml.get('type', None)
214+
description = datasource_xml.get('description', None)
215+
encrypt_extracts = datasource_xml.get('encryptExtracts', None)
216+
has_extracts = datasource_xml.get('hasExtracts', None)
217+
id_ = datasource_xml.get('id', None)
218+
name = datasource_xml.get('name', None)
159219
updated_at = parse_datetime(datasource_xml.get('updatedAt', None))
160-
certification_note = datasource_xml.get('certificationNote', None)
161-
certified = str(datasource_xml.get('isCertified', None)).lower() == 'true'
220+
use_remote_query_agent = datasource_xml.get('useRemoteQueryAgent', None)
221+
webpage_url = datasource_xml.get('webpageUrl', None)
162222

163223
tags = None
164224
tags_elem = datasource_xml.find('.//t:tags', namespaces=ns)
@@ -177,5 +237,11 @@ def _parse_element(datasource_xml, ns):
177237
if owner_elem is not None:
178238
owner_id = owner_elem.get('id', None)
179239

180-
return (id_, name, datasource_type, content_url, created_at, updated_at, tags, project_id,
181-
project_name, owner_id, certified, certification_note)
240+
ask_data_enablement = None
241+
ask_data_elem = datasource_xml.find('.//t:askData', namespaces=ns)
242+
if ask_data_elem is not None:
243+
ask_data_enablement = ask_data_elem.get('enablement', None)
244+
245+
return (ask_data_enablement, certified, certification_note, content_url, created_at,
246+
datasource_type, description, encrypt_extracts, has_extracts, id_, name, owner_id,
247+
project_id, project_name, tags, updated_at, use_remote_query_agent, webpage_url)

tableauserverclient/server/request_factory.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,15 @@ def _generate_xml(self, datasource_item, connection_credentials=None, connection
131131
xml_request = ET.Element('tsRequest')
132132
datasource_element = ET.SubElement(xml_request, 'datasource')
133133
datasource_element.attrib['name'] = datasource_item.name
134+
if datasource_item.description:
135+
datasource_element.attrib['description'] = str(datasource_item.description)
136+
if datasource_item.use_remote_query_agent is not None:
137+
datasource_element.attrib['useRemoteQueryAgent'] = str(datasource_item.use_remote_query_agent).lower()
138+
139+
if datasource_item.ask_data_enablement:
140+
ask_data_element = ET.SubElement(datasource_element, 'askData')
141+
ask_data_element.attrib['enablement'] = datasource_item.ask_data_enablement
142+
134143
project_element = ET.SubElement(datasource_element, 'project')
135144
project_element.attrib['id'] = datasource_item.project_id
136145

@@ -149,6 +158,9 @@ def _generate_xml(self, datasource_item, connection_credentials=None, connection
149158
def update_req(self, datasource_item):
150159
xml_request = ET.Element('tsRequest')
151160
datasource_element = ET.SubElement(xml_request, 'datasource')
161+
if datasource_item.ask_data_enablement:
162+
ask_data_element = ET.SubElement(datasource_element, 'askData')
163+
ask_data_element.attrib['enablement'] = datasource_item.ask_data_enablement
152164
if datasource_item.project_id:
153165
project_element = ET.SubElement(datasource_element, 'project')
154166
project_element.attrib['id'] = datasource_item.project_id
@@ -160,6 +172,8 @@ def update_req(self, datasource_item):
160172

161173
if datasource_item.certification_note:
162174
datasource_element.attrib['certificationNote'] = str(datasource_item.certification_note)
175+
if datasource_item.encrypt_extracts is not None:
176+
datasource_element.attrib['encryptExtracts'] = str(datasource_item.encrypt_extracts).lower()
163177

164178
return ET.tostring(xml_request)
165179

test/assets/datasource_get.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
<tsResponse xmlns="http://tableau.com/api" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://tableau.com/api http://tableau.com/api/ts-api-2.3.xsd">
33
<pagination pageNumber="1" pageSize="100" totalAvailable="2" />
44
<datasources>
5-
<datasource id="e76a1461-3b1d-4588-bf1b-17551a879ad9" name="SampleDS" contentUrl="SampleDS" type="dataengine" createdAt="2016-08-11T21:22:40Z" updatedAt="2016-08-11T21:34:17Z">
5+
<datasource id="e76a1461-3b1d-4588-bf1b-17551a879ad9" name="SampleDS" contentUrl="SampleDS" type="dataengine" createdAt="2016-08-11T21:22:40Z" updatedAt="2016-08-11T21:34:17Z" encryptExtracts="false" hasExtracts="true" useRemoteQueryAgent="false" webpageUrl="https://web.com">
66
<project id="ee8c6e70-43b6-11e6-af4f-f7b0d8e20760" name="default" />
77
<owner id="5de011f8-5aa9-4d5b-b991-f462c8dd6bb7" />
88
<tags />
99
</datasource>
10-
<datasource id="9dbd2263-16b5-46e1-9c43-a76bb8ab65fb" name="Sample datasource" contentUrl="Sampledatasource" type="dataengine" createdAt="2016-08-04T21:31:55Z" updatedAt="2016-08-04T21:31:55Z">
10+
<datasource id="9dbd2263-16b5-46e1-9c43-a76bb8ab65fb" name="Sample datasource" contentUrl="Sampledatasource" type="dataengine" createdAt="2016-08-04T21:31:55Z" updatedAt="2016-08-04T21:31:55Z" encryptExtracts="true" hasExtracts="false" useRemoteQueryAgent="true" webpageUrl="https://page.com">
1111
<project id="ee8c6e70-43b6-11e6-af4f-f7b0d8e20760" name="default" />
1212
<owner id="5de011f8-5aa9-4d5b-b991-f462c8dd6bb7" />
1313
<tags>

test/assets/datasource_get_by_id.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
<?xml version='1.0' encoding='UTF-8'?>
22
<tsResponse xmlns="http://tableau.com/api" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://tableau.com/api http://tableau.com/api/ts-api-2.3.xsd">
3-
<datasource id="9dbd2263-16b5-46e1-9c43-a76bb8ab65fb" name="Sample datasource" contentUrl="Sampledatasource" type="dataengine" createdAt="2016-08-04T21:31:55Z" updatedAt="2016-08-04T21:31:55Z">
3+
<datasource id="9dbd2263-16b5-46e1-9c43-a76bb8ab65fb" name="Sample datasource" contentUrl="Sampledatasource" type="dataengine" createdAt="2016-08-04T21:31:55Z" updatedAt="2016-08-04T21:31:55Z" description="test-ds">
44
<project id="ee8c6e70-43b6-11e6-af4f-f7b0d8e20760" name="default" />
55
<owner id="5de011f8-5aa9-4d5b-b991-f462c8dd6bb7" />
66
<tags>
77
<tag label="world" />
88
<tag label="indicators" />
99
<tag label="sample" />
1010
</tags>
11+
<askData enablement="SiteDefault" />
1112
</datasource>
1213
</tsResponse>

test/test_datasource.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ def test_get(self):
4747
self.assertEqual('SampleDS', all_datasources[0].name)
4848
self.assertEqual('ee8c6e70-43b6-11e6-af4f-f7b0d8e20760', all_datasources[0].project_id)
4949
self.assertEqual('5de011f8-5aa9-4d5b-b991-f462c8dd6bb7', all_datasources[0].owner_id)
50+
self.assertEqual('https://web.com', all_datasources[0]._webpage_url)
51+
self.assertFalse(all_datasources[0].encrypt_extracts)
52+
self.assertTrue(all_datasources[0]._has_extracts)
53+
self.assertFalse(all_datasources[0]._use_remote_query_agent)
5054

5155
self.assertEqual('9dbd2263-16b5-46e1-9c43-a76bb8ab65fb', all_datasources[1].id)
5256
self.assertEqual('dataengine', all_datasources[1].datasource_type)
@@ -58,6 +62,10 @@ def test_get(self):
5862
self.assertEqual('ee8c6e70-43b6-11e6-af4f-f7b0d8e20760', all_datasources[1].project_id)
5963
self.assertEqual('5de011f8-5aa9-4d5b-b991-f462c8dd6bb7', all_datasources[1].owner_id)
6064
self.assertEqual(set(['world', 'indicators', 'sample']), all_datasources[1].tags)
65+
self.assertEqual('https://page.com', all_datasources[1]._webpage_url)
66+
self.assertTrue(all_datasources[1].encrypt_extracts)
67+
self.assertFalse(all_datasources[1]._has_extracts)
68+
self.assertTrue(all_datasources[1]._use_remote_query_agent)
6169

6270
def test_get_before_signin(self):
6371
self.server._auth_token = None
@@ -88,6 +96,8 @@ def test_get_by_id(self):
8896
self.assertEqual('ee8c6e70-43b6-11e6-af4f-f7b0d8e20760', single_datasource.project_id)
8997
self.assertEqual('5de011f8-5aa9-4d5b-b991-f462c8dd6bb7', single_datasource.owner_id)
9098
self.assertEqual(set(['world', 'indicators', 'sample']), single_datasource.tags)
99+
self.assertEqual("test-ds", single_datasource.description)
100+
self.assertEqual(TSC.DatasourceItem.AskDataEnablement.SiteDefault, single_datasource.ask_data_enablement)
91101

92102
def test_update(self):
93103
response_xml = read_xml_asset(UPDATE_XML)

0 commit comments

Comments
 (0)
0