Skip to content

Commit

Permalink
Add timepicker block element support (#876)
Browse files Browse the repository at this point in the history
  • Loading branch information
seratch committed May 7, 2021
1 parent 833e3c5 commit 66c8859
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
1 change: 1 addition & 0 deletions slack_sdk/models/blocks/__init__.py
Expand Up @@ -23,6 +23,7 @@
from .block_elements import ConversationMultiSelectElement # noqa
from .block_elements import ConversationSelectElement # noqa
from .block_elements import DatePickerElement # noqa
from .block_elements import TimePickerElement # noqa
from .block_elements import ExternalDataMultiSelectElement # noqa
from .block_elements import ExternalDataSelectElement # noqa
from .block_elements import ImageElement # noqa
Expand Down
42 changes: 42 additions & 0 deletions slack_sdk/models/blocks/block_elements.py
Expand Up @@ -377,6 +377,48 @@ def _validate_initial_date_valid(self):
)


# -------------------------------------------------
# TimePicker
# -------------------------------------------------


class TimePickerElement(InputInteractiveElement):
type = "timepicker"

@property
def attributes(self) -> Set[str]:
return super().attributes.union({"initial_time"})

def __init__(
self,
*,
action_id: Optional[str] = None,
placeholder: Optional[Union[str, dict, TextObject]] = None,
initial_time: Optional[str] = None,
confirm: Optional[Union[dict, ConfirmObject]] = None,
**others: dict,
):
"""
An element which allows selection of a time of day.
https://api.slack.com/reference/block-kit/block-elements#timepicker
"""
super().__init__(
type=self.type,
action_id=action_id,
placeholder=TextObject.parse(placeholder, PlainTextObject.type),
confirm=ConfirmObject.parse(confirm),
)
show_unknown_key_warning(self, others)

self.initial_time = initial_time

@JsonValidator("initial_time attribute must be in format 'HH:mm'")
def _validate_initial_time_valid(self):
return self.initial_time is None or re.match(
r"([0-1][0-9]|2[0-3]):([0-5][0-9])", self.initial_time
)


# -------------------------------------------------
# Image
# -------------------------------------------------
Expand Down
46 changes: 46 additions & 0 deletions tests/slack_sdk/models/test_elements.py
Expand Up @@ -4,6 +4,7 @@
from slack_sdk.models.blocks import (
ButtonElement,
DatePickerElement,
TimePickerElement,
ExternalDataSelectElement,
ImageElement,
LinkButtonElement,
Expand Down Expand Up @@ -216,6 +217,51 @@ def test_issue_623(self):
elem.to_dict()


# -------------------------------------------------
# TimePicker
# -------------------------------------------------


class TimePickerElementTests(unittest.TestCase):
def test_document(self):
input = {
"type": "timepicker",
"action_id": "timepicker123",
"initial_time": "11:40",
"placeholder": {"type": "plain_text", "text": "Select a time",},
}
self.assertDictEqual(input, TimePickerElement(**input).to_dict())

def test_json(self):
for hour in range(0, 23):
for minute in range(0, 59):
time = f"{hour:02}:{minute:02}"
self.assertDictEqual(
{
"action_id": "timepicker123",
"initial_time": time,
"placeholder": {
"emoji": True,
"type": "plain_text",
"text": "Select a time",
},
"type": "timepicker",
},
TimePickerElement(
action_id="timepicker123",
placeholder="Select a time",
initial_time=time,
).to_dict(),
)

with self.assertRaises(SlackObjectFormationError):
TimePickerElement(
action_id="timepicker123",
placeholder="Select a time",
initial_time="25:00",
).to_dict()


# -------------------------------------------------
# Image
# -------------------------------------------------
Expand Down

0 comments on commit 66c8859

Please sign in to comment.