Skip to content

Commit 6d3569e

Browse files
WilliamBergaminClaude
andauthored
feat(models): model the datetimepicker, timezone, rich text, and file view state values (#2045)
Co-authored-by: Claude <svc-devxp-claude@slack-corp.com>
1 parent 4639eee commit 6d3569e

3 files changed

Lines changed: 232 additions & 5 deletions

File tree

‎slack_sdk/models/views/__init__.py‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from typing import Optional, Union, Dict, Sequence
44

55
from slack_sdk.models.basic_objects import JsonObject, JsonValidator
6-
from slack_sdk.models.blocks import Block, TextObject, PlainTextObject, Option
6+
from slack_sdk.models.blocks import Block, TextObject, PlainTextObject, Option, RichTextBlock
77

88

99
class View(JsonObject):
@@ -191,6 +191,10 @@ class ViewStateValue(JsonObject):
191191
"selected_channels",
192192
"selected_users",
193193
"selected_options",
194+
"selected_date_time",
195+
"timezone",
196+
"rich_text_value",
197+
"files",
194198
}
195199

196200
def __init__(
@@ -208,6 +212,10 @@ def __init__(
208212
selected_channels: Optional[Sequence[str]] = None,
209213
selected_users: Optional[Sequence[str]] = None,
210214
selected_options: Optional[Sequence[Union[dict, Option]]] = None,
215+
selected_date_time: Optional[int] = None,
216+
timezone: Optional[str] = None,
217+
rich_text_value: Optional[Union[dict, RichTextBlock]] = None,
218+
files: Optional[Sequence[dict]] = None,
211219
**kwargs,
212220
):
213221
self.type = type
@@ -221,6 +229,10 @@ def __init__(
221229
self.selected_conversations = selected_conversations
222230
self.selected_channels = selected_channels
223231
self.selected_users = selected_users
232+
self.selected_date_time = selected_date_time
233+
self.timezone = timezone
234+
self.rich_text_value = rich_text_value
235+
self.files = files
224236

225237
if isinstance(selected_options, list):
226238
self.selected_options = []

‎tests/slack_sdk/models/test_views.py‎

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
PlainTextObject,
1818
Option,
1919
MarkdownTextObject,
20+
RichTextBlock,
2021
)
2122
from slack_sdk.models.views import View, ViewState, ViewStateValue
2223

@@ -360,21 +361,55 @@ def test_view_state_value_with_selected_options(self):
360361
self.assertDictEqual(expected, ViewStateValue(**input).to_dict())
361362

362363
def test_view_state_value_with_unknown_keys(self):
363-
input = {"type": "datetimepicker", "selected_date_time": 1700000000}
364+
input = {"type": "plain_text_input", "value": "Title", "value_changed": True}
364365

365366
view_state_value = ViewStateValue(**input)
366-
self.assertDictEqual({"selected_date_time": 1700000000}, view_state_value.additional_attributes)
367-
self.assertDictEqual({"type": "datetimepicker"}, view_state_value.to_dict())
367+
self.assertDictEqual({"value_changed": True}, view_state_value.additional_attributes)
368+
self.assertDictEqual({"type": "plain_text_input", "value": "Title"}, view_state_value.to_dict())
368369

369370
def test_view_with_unknown_state_value_keys(self):
370371
view = View(
371372
type="modal",
372373
title="Schedule",
373374
blocks=[],
374-
state={"values": {"b1": {"a1": {"type": "datetimepicker", "selected_date_time": 1700000000}}}},
375+
state={"values": {"b1": {"a1": {"type": "plain_text_input", "value_changed": True}}}},
375376
)
376377
self.assertIsInstance(view.state.values["b1"]["a1"], ViewStateValue)
377378

379+
def test_view_state_value_with_selected_date_time(self):
380+
input = {"type": "datetimepicker", "selected_date_time": 1700000000}
381+
382+
view_state_value = ViewStateValue(**input)
383+
self.assertEqual(1700000000, view_state_value.selected_date_time)
384+
self.assertDictEqual(input, view_state_value.to_dict())
385+
386+
def test_view_state_value_with_timezone(self):
387+
input = {"type": "timepicker", "selected_time": "13:37", "timezone": "America/Toronto"}
388+
389+
view_state_value = ViewStateValue(**input)
390+
self.assertEqual("America/Toronto", view_state_value.timezone)
391+
self.assertDictEqual(input, view_state_value.to_dict())
392+
393+
def test_view_state_value_with_rich_text_value(self):
394+
rich_text = {
395+
"type": "rich_text",
396+
"elements": [{"type": "rich_text_section", "elements": [{"type": "text", "text": "Hello"}]}],
397+
}
398+
input = {"type": "rich_text_input", "rich_text_value": rich_text}
399+
400+
self.assertDictEqual(input, ViewStateValue(**input).to_dict())
401+
self.assertDictEqual(
402+
input,
403+
ViewStateValue(type="rich_text_input", rich_text_value=RichTextBlock(**rich_text)).to_dict(),
404+
)
405+
406+
def test_view_state_value_with_files(self):
407+
input = {"type": "file_input", "files": [{"id": "F111", "name": "notes.txt", "filetype": "text"}]}
408+
409+
view_state_value = ViewStateValue(**input)
410+
self.assertEqual("F111", view_state_value.files[0]["id"])
411+
self.assertDictEqual(input, view_state_value.to_dict())
412+
378413
def test_load_modal_view_001(self):
379414
with open("tests/slack_sdk_fixture/view_modal_001.json") as file:
380415
self.verify_loaded_view_object(file)
@@ -415,6 +450,10 @@ def test_load_modal_view_010(self):
415450
with open("tests/slack_sdk_fixture/view_modal_010.json") as file:
416451
self.verify_loaded_view_object(file)
417452

453+
def test_load_modal_view_011(self):
454+
with open("tests/slack_sdk_fixture/view_modal_011.json") as file:
455+
self.verify_loaded_view_object(file)
456+
418457
# --------------------------------
419458
# Home Tabs
420459
# --------------------------------
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
{
2+
"id": "VMHU10V26",
3+
"team_id": "T8N4K1JN",
4+
"type": "modal",
5+
"blocks": [
6+
{
7+
"type": "input",
8+
"block_id": "datetime_block",
9+
"label": {
10+
"type": "plain_text",
11+
"text": "Date and time",
12+
"emoji": true
13+
},
14+
"optional": false,
15+
"dispatch_action": false,
16+
"element": {
17+
"type": "datetimepicker",
18+
"action_id": "datetime_action"
19+
}
20+
},
21+
{
22+
"type": "input",
23+
"block_id": "time_block",
24+
"label": {
25+
"type": "plain_text",
26+
"text": "Time",
27+
"emoji": true
28+
},
29+
"optional": false,
30+
"dispatch_action": false,
31+
"element": {
32+
"type": "timepicker",
33+
"action_id": "time_action",
34+
"timezone": "America/Toronto"
35+
}
36+
},
37+
{
38+
"type": "input",
39+
"block_id": "rich_text_block",
40+
"label": {
41+
"type": "plain_text",
42+
"text": "Rich text",
43+
"emoji": true
44+
},
45+
"optional": false,
46+
"dispatch_action": false,
47+
"element": {
48+
"type": "rich_text_input",
49+
"action_id": "rich_text_action"
50+
}
51+
},
52+
{
53+
"type": "input",
54+
"block_id": "file_block",
55+
"label": {
56+
"type": "plain_text",
57+
"text": "File",
58+
"emoji": true
59+
},
60+
"optional": false,
61+
"dispatch_action": false,
62+
"element": {
63+
"type": "file_input",
64+
"action_id": "file_action",
65+
"max_files": 1
66+
}
67+
}
68+
],
69+
"callback_id": "capture_view",
70+
"state": {
71+
"values": {
72+
"datetime_block": {
73+
"datetime_action": {
74+
"type": "datetimepicker",
75+
"selected_date_time": 1794941340
76+
}
77+
},
78+
"time_block": {
79+
"time_action": {
80+
"type": "timepicker",
81+
"selected_time": "02:00",
82+
"timezone": "America/Toronto"
83+
}
84+
},
85+
"rich_text_block": {
86+
"rich_text_action": {
87+
"type": "rich_text_input",
88+
"rich_text_value": {
89+
"type": "rich_text",
90+
"elements": [
91+
{
92+
"type": "rich_text_section",
93+
"elements": [
94+
{
95+
"type": "text",
96+
"text": "hello there friend"
97+
}
98+
]
99+
}
100+
]
101+
}
102+
}
103+
},
104+
"file_block": {
105+
"file_action": {
106+
"type": "file_input",
107+
"files": [
108+
{
109+
"id": "F0000000001",
110+
"created": 1700000000,
111+
"timestamp": 1700000000,
112+
"mimetype": "image/gif",
113+
"filetype": "gif",
114+
"pretty_type": "GIF",
115+
"user": "U0000000001",
116+
"user_team": "E0000000001",
117+
"editable": false,
118+
"size": 125649,
119+
"mode": "hosted",
120+
"is_external": false,
121+
"external_type": "",
122+
"is_public": false,
123+
"public_url_shared": false,
124+
"display_as_bot": false,
125+
"username": "",
126+
"name": "sample.gif",
127+
"title": "sample.gif",
128+
"is_modified_by_ai": false,
129+
"url_private": "https://files.slack.com/files-pri/T0000000001-F0000000001/sample.gif",
130+
"url_private_download": "https://files.slack.com/files-pri/T0000000001-F0000000001/download/sample.gif",
131+
"media_display_type": "unknown",
132+
"thumb_64": "https://files.slack.com/files-tmb/T0000000001-F0000000001-0123456789/sample_64.png",
133+
"thumb_80": "https://files.slack.com/files-tmb/T0000000001-F0000000001-0123456789/sample_80.png",
134+
"thumb_360": "https://files.slack.com/files-tmb/T0000000001-F0000000001-0123456789/sample_360.png",
135+
"thumb_360_w": 82,
136+
"thumb_360_h": 82,
137+
"thumb_160": "https://files.slack.com/files-tmb/T0000000001-F0000000001-0123456789/sample_160.png",
138+
"thumb_360_gif": "https://files.slack.com/files-tmb/T0000000001-F0000000001-0123456789/sample_360.gif",
139+
"original_w": 82,
140+
"original_h": 82,
141+
"deanimate": "https://files.slack.com/files-tmb/T0000000001-F0000000001-0123456789/sample_deanimate.png",
142+
"deanimate_gif": "https://files.slack.com/files-tmb/T0000000001-F0000000001-0123456789/sample_deanimate_gif.png",
143+
"thumb_tiny": "AwAwADDMp6nIKnv0plORdzgVQCAHPSt",
144+
"permalink": "https://example.enterprise.slack.com/files/U0000000001/F0000000001/sample.gif",
145+
"permalink_public": "https://slack-files.com/T0000000001-F0000000001-abcdef0123",
146+
"comments_count": 0,
147+
"shares": {},
148+
"channels": [],
149+
"groups": [],
150+
"ims": [],
151+
"has_more_shares": false,
152+
"has_rich_preview": false,
153+
"file_access": "visible"
154+
}
155+
]
156+
}
157+
}
158+
}
159+
},
160+
"hash": "156772938.1827395",
161+
"title": {
162+
"type": "plain_text",
163+
"text": "Capture view state",
164+
"emoji": true
165+
},
166+
"clear_on_close": false,
167+
"notify_on_close": false,
168+
"submit": {
169+
"type": "plain_text",
170+
"text": "Submit",
171+
"emoji": true
172+
},
173+
"root_view_id": "VMHU10V26",
174+
"app_id": "AA4928AQ",
175+
"bot_id": "BA13894H"
176+
}

0 commit comments

Comments
 (0)