Skip to content

Commit

Permalink
Added logic for converting a datetime.datetime object to the right st…
Browse files Browse the repository at this point in the history
…ring format directly into the UrlFilters.
  • Loading branch information
Bryant Howell committed Dec 9, 2019
1 parent 66e0f71 commit 4c231a6
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions tableau_rest_api/url_filter.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
from ..tableau_exceptions import *
from typing import Union, Optional, List, Dict, Tuple
import datetime

class UrlFilter:
def __init__(self, field: str, operator: str, values: List[str]):
self.field = field
self.operator = operator
self.values = values

@staticmethod
def datetime_to_tableau_date_str(dt: Union[str, datetime.datetime]) -> str:
if isinstance(dt, datetime.datetime):
return dt.isoformat('T')[:19] + 'Z'
else:
return dt


def get_filter_string(self) -> str:
if len(self.values) == 0:
raise InvalidOptionException('Must pass in at least one value for the filter')
Expand All @@ -25,7 +34,7 @@ def get_name_filter(name: str) -> 'UrlFilter':

# Users
@staticmethod
def get_last_login_filter(operator: str, last_login_time: str) -> 'UrlFilter':
def get_last_login_filter(operator: str, last_login_time: Union[str, datetime.datetime]) -> 'UrlFilter':
"""
:param operator: Should be one of 'eq', 'gt', 'gte', 'lt', 'lte'
:param last_login_time: ISO 8601 representation of time like 2016-01-01T00:00:00:00Z
Expand All @@ -34,8 +43,9 @@ def get_last_login_filter(operator: str, last_login_time: str) -> 'UrlFilter':
if operator not in comparison_operators:
raise InvalidOptionException("operator must be one of 'eq', 'gt', 'gte', 'lt', 'lte' ")
# Convert to the correct time format
time = UrlFilter.datetime_to_tableau_date_str(last_login_time)

return UrlFilter('lastLogin', operator, [last_login_time, ])
return UrlFilter('lastLogin', operator, [time, ])

# Users
@staticmethod
Expand All @@ -50,7 +60,7 @@ def get_owner_name_filter(owner_name: str) -> 'UrlFilter':

# Workbooks, Datasources, Views, Jobs
@staticmethod
def get_created_at_filter(operator: str, created_at_time: str) -> 'UrlFilter':
def get_created_at_filter(operator: str, created_at_time: Union[str, datetime.datetime]) -> 'UrlFilter':
"""
:param operator: Should be one of 'eq', 'gt', 'gte', 'lt', 'lte'
:param created_at_time: ISO 8601 representation of time like 2016-01-01T00:00:00:00Z
Expand All @@ -59,12 +69,12 @@ def get_created_at_filter(operator: str, created_at_time: str) -> 'UrlFilter':
if operator not in comparison_operators:
raise InvalidOptionException("operator must be one of 'eq', 'gt', 'gte', 'lt', 'lte' ")
# Convert to the correct time format

return UrlFilter('createdAt', operator, [created_at_time, ])
time = UrlFilter.datetime_to_tableau_date_str(created_at_time)
return UrlFilter('createdAt', operator, [time, ])

# Workbooks, Datasources, Views
@staticmethod
def get_updated_at_filter(operator: str, updated_at_time: str) -> 'UrlFilter':
def get_updated_at_filter(operator: str, updated_at_time: Union[str, datetime.datetime]) -> 'UrlFilter':
"""
:param operator: Should be one of 'eq', 'gt', 'gte', 'lt', 'lte'
:param updated_at_time: ISO 8601 representation of time like 2016-01-01T00:00:00:00Z
Expand All @@ -73,8 +83,9 @@ def get_updated_at_filter(operator: str, updated_at_time: str) -> 'UrlFilter':
if operator not in comparison_operators:
raise InvalidOptionException("operator must be one of 'eq', 'gt', 'gte', 'lt', 'lte' ")
# Convert to the correct time format
time = updated_at_time

return UrlFilter('updatedAt', operator, [updated_at_time, ])
return UrlFilter('updatedAt', operator, [time, ])

# Workbooks, Datasources, Views
@staticmethod
Expand Down Expand Up @@ -121,7 +132,7 @@ def get_domain_names_filter(domain_names: List[str]) -> 'UrlFilter':

# Groups
@staticmethod
def get_domain_nicknames_filter(domain_nicknames: str) -> 'UrlFilter':
def get_domain_nicknames_filter(domain_nicknames: List[str]) -> 'UrlFilter':

return UrlFilter('domainNickname', 'in', domain_nicknames)

Expand Down Expand Up @@ -159,7 +170,6 @@ def get_user_count_filter(operator, user_count: int) -> 'UrlFilter':
comparison_operators = ['eq', 'gt', 'gte', 'lt', 'lte']
if operator not in comparison_operators:
raise InvalidOptionException("operator must be one of 'eq', 'gt', 'gte', 'lt', 'lte' ")
# Convert to the correct time format

return UrlFilter('userCount', operator, [str(user_count), ])

Expand Down Expand Up @@ -221,8 +231,8 @@ def get_started_at_filter(operator: str, started_at_time: str) -> UrlFilter:
if operator not in comparison_operators:
raise InvalidOptionException("operator must be one of 'eq', 'gt', 'gte', 'lt', 'lte' ")
# Convert to the correct time format

return UrlFilter('createdAt', operator, [started_at_time, ])
time = UrlFilter.datetime_to_tableau_date_str(started_at_time)
return UrlFilter('createdAt', operator, [time, ])

# Jobs
@staticmethod
Expand All @@ -235,8 +245,8 @@ def get_ended_at_filter(operator: str, ended_at_time: str) -> UrlFilter:
if operator not in comparison_operators:
raise InvalidOptionException("operator must be one of 'eq', 'gt', 'gte', 'lt', 'lte' ")
# Convert to the correct time format

return UrlFilter('createdAt', operator, [ended_at_time, ])
time = UrlFilter.datetime_to_tableau_date_str(ended_at_time)
return UrlFilter('createdAt', operator, [time, ])

# Jobs
@staticmethod
Expand Down

0 comments on commit 4c231a6

Please sign in to comment.