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
15 changes: 15 additions & 0 deletions tableauserverclient/models/property_decorators.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
from functools import wraps


Expand Down Expand Up @@ -84,3 +85,17 @@ def wrapper(self, value):
return wrapper

return property_type_decorator


def property_matches(regex_to_match, error):

compiled_re = re.compile(regex_to_match)

def wrapper(func):
@wraps(func)
def validate_regex_decorator(self, value):
if not compiled_re.match(value):
raise ValueError(error)
return func(self, value)
return validate_regex_decorator
return wrapper
7 changes: 6 additions & 1 deletion tableauserverclient/models/site_item.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import xml.etree.ElementTree as ET
from .property_decorators import property_is_enum, property_is_boolean, property_not_empty, property_not_nullable
from .property_decorators import (property_is_enum, property_is_boolean, property_matches,
property_not_empty, property_not_nullable)
from .. import NAMESPACE


VALID_CONTENT_URL_RE = r"^[a-zA-Z0-9_\-]*$"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we ever want the content url to be empty? I guess it is when we have the default site, right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, it needs to validate "" or all of our models fail when reading responses from the default site



class SiteItem(object):
class AdminMode:
ContentAndUsers = 'ContentAndUsers'
Expand Down Expand Up @@ -45,6 +49,7 @@ def content_url(self):

@content_url.setter
@property_not_nullable
@property_matches(VALID_CONTENT_URL_RE, "content_url can contain only letters, numbers, dashes, and underscores")
def content_url(self, value):
self._content_url = value

Expand Down
19 changes: 16 additions & 3 deletions test/test_site_model.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# coding=utf-8
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be part of our template for new files!


import unittest
import tableauserverclient as TSC

Expand All @@ -19,10 +21,21 @@ def test_invalid_admin_mode(self):
site.admin_mode = "Hello"

def test_invalid_content_url(self):
self.assertRaises(ValueError, TSC.SiteItem, "site", None)
site = TSC.SiteItem("site", "url")

with self.assertRaises(ValueError):
site.content_url = None
site = TSC.SiteItem(name="蚵仔煎", content_url="蚵仔煎")

with self.assertRaises(ValueError):
site = TSC.SiteItem(name="蚵仔煎", content_url=None)

def test_set_valid_content_url(self):
# Default Site
site = TSC.SiteItem(name="Default", content_url="")
self.assertEqual(site.content_url, "")

# Unicode Name and ascii content_url
site = TSC.SiteItem(name="蚵仔煎", content_url="omlette")
self.assertEqual(site.content_url, "omlette")

def test_invalid_disable_subscriptions(self):
site = TSC.SiteItem("site", "url")
Expand Down