8000 Merge pull request #1348 from jorwoods/jorwoods/pdf_height_width · mirror-dump/server-client-python@d09a9ce · GitHub
[go: up one dir, main page]

Skip to content

Commit d09a9ce

Browse files
authored
Merge pull request tableau#1348 from jorwoods/jorwoods/pdf_height_width
feat: allow viz height and width parameters
2 parents 969922b + ffd0b8f commit d09a9ce

File tree

3 files changed

+67
-3
lines changed

3 files changed

+67
-3
lines changed

tableauserverclient/models/property_decorators.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import datetime
22
import re
33
from functools import wraps
4+
from typing import Any, Container, Optional, Tuple
45

56
from tableauserverclient.datetime_helpers import parse_datetime
67

@@ -65,7 +66,7 @@ def wrapper(self, value):
6566
return wrapper
6667

6768

68-
def property_is_int(range, allowed=None):
69+
def property_is_int(range: Tuple[int, int], allowed: Optional[Container[Any]] = None):
6970
"""Takes a range of ints and a list of exemptions to check against
7071
when setting a property on a model. The range is a tuple of (min, max) and the
7172
allowed list (empty by default) allows values outside that range.
@@ -89,8 +90,10 @@ def wrapper(self, value):
8990
raise ValueError(error)
9091

9192
min, max = range
93+
if value in allowed:
94+
return func(self, value)
9295

93-
if (value < min or value > max) and (value not in allowed):
96+
if value < min or value > max:
9497
raise ValueError(error)
9598

9699
return func(self, value)

tableauserverclient/server/request_options.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import sys
2+
13
from tableauserverclient.models.property_decorators import property_is_int
24
import logging
35

@@ -261,11 +263,13 @@ class Orientation:
261263
Portrait = "portrait"
262264
Landscape = "landscape"
263265

264-
def __init__(self, page_type=None, orientation=None, maxage=-1):
266+
def __init__(self, page_type=None, orientation=None, maxage=-1, viz_height=None, viz_width=None):
265267
super(PDFRequestOptions, self).__init__()
266268
self.page_type = page_type
267269
self.orientation = orientation
268270
self.max_age = maxage
271+
self.viz_height = viz_height
272+
self.viz_width = viz_width
269273

270274
@property
271275
def max_age(self):
@@ -276,6 +280,24 @@ def max_age(self):
276280
def max_age(self, value):
277281
self._max_age = value
278282

283+
@property
284+
def viz_height(self):
285+
return self._viz_height
286+
287+
@viz_height.setter
288+
@property_is_int(range=(0, sys.maxsize), allowed=(None,))
289+
def viz_height(self, value):
290+
self._viz_height = value
291+
292+
@property
293+
def viz_width(self):
294+
return self._viz_width
295+
296+
@viz_width.setter
297+
@property_is_int(range=(0, sys.maxsize), allowed=(None,))
298+
def viz_width(self, value):
299+
self._viz_width = value
300+
279301
def get_query_params(self):
280302
params = {}
281303
if self.page_type:
@@ -287,6 +309,16 @@ def get_query_params(self):
287309
if self.max_age != -1:
288310
params["maxAge"] = self.max_age
289311

312+
# XOR. Either both are None or both are not None.
313+
if (self.viz_height is None) ^ (self.viz_width is None):
314+
raise ValueError("viz_height and viz_width must be specified together")
315+
316+
if self.viz_height is not None:
317+
params["vizHeight"] = self.viz_height
318+
319+
if self.viz_width is not None:
320+
params["vizWidth"] = self.viz_width
321+
290322
self._append_view_filters(params)
291323

292324
return params

test/test_view.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,3 +315,32 @@ def test_filter_excel(self) -> None:
315315

316316
excel_file = b"".join(single_view.excel)
317317
self.assertEqual(response, excel_file)
318+
319+
def test_pdf_height(self) -> None:
320+
self.server.version = "3.8"
321+
self.baseurl = self.server.views.baseurl
322+
with open(POPULATE_PDF, "rb") as f:
323+
response = f.read()
324+
with requests_mock.mock() as m:
325+
m.get(
326+
self.baseurl + "/d79634e1-6063-4ec9-95ff-50acbf609ff5/pdf?vizHeight=1080&vizWidth=1920",
327+
content=response,
328+
)
329+
single_view = TSC.ViewItem()
330+
single_view._id = "d79634e1-6063-4ec9-95ff-50acbf609ff5"
331+
332+
req_option = TSC.PDFRequestOptions(
333+
viz_height=1080,
334+
viz_width=1920,
335+
)
336+
337+
self.server.views.populate_pdf(single_view, req_option)
338+
self.assertEqual(response, single_view.pdf)
339+
340+
def test_pdf_errors(self) -> None:
341+
req_option = TSC.PDFRequestOptions(viz_height=1080)
342+
with self.assertRaises(ValueError):
343+
req_option.get_query_params()
344+
req_option = TSC.PDFRequestOptions(viz_width=1920)
345+
with self.assertRaises(ValueError):
346+
req_option.get_query_params()

0 commit comments

Comments
 (0)
0