Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for File Input element type #110

Merged
merged 7 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ Here's the list of types of components and corresponding classes:
| rich_text_preformatted | RichTextPreformatted |
| rich_text_quote | RichTextQuote |
| rich_text_section | RichTextSection |
| file_input | FileInput |

### Composition objects

Expand Down
2 changes: 2 additions & 0 deletions blockkit/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
DatePicker,
DatetimePicker,
ExternalSelect,
FileInput,
Image,
MultiChannelsSelect,
MultiConversationsSelect,
Expand Down Expand Up @@ -154,6 +155,7 @@ def __init__(
DatetimePicker,
TimePicker,
NumberInput,
FileInput
]


Expand Down
26 changes: 26 additions & 0 deletions blockkit/elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
validate_datetime,
validate_text_length,
validate_time,
validate_integer_size,
validator,
)

Expand Down Expand Up @@ -51,6 +52,7 @@
"RichTextQuote",
"RichTextSection",
"RichTextList",
"FileInput"
]


Expand Down Expand Up @@ -717,3 +719,27 @@ def __init__(
indent: Optional[int] = None,
):
super().__init__(elements=elements, style=style, indent=indent)

class FileInput(FocusableElement):
type: str = "file_input"
filetypes: Optional[List[str]] = Field(None)
maxfiles: Optional[int] = Field(None)
cleavenworth marked this conversation as resolved.
Show resolved Hide resolved

def __init__(
self,
*,
action_id: Optional[str] = None,
filetypes: Optional[List[str]] = None,
maxfiles: Optional[int] = None,
focus_on_load: Optional[bool] = None,
cleavenworth marked this conversation as resolved.
Show resolved Hide resolved
):
super().__init__(
action_id=action_id,
filetypes=filetypes,
maxfiles=maxfiles,
focus_on_load=focus_on_load,
)

_validate_maxfiles = validator(
"maxfiles", validate_integer_size, min=1, max=10
)
13 changes: 13 additions & 0 deletions blockkit/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,16 @@ def validate_datetime(v: Union[int, datetime]) -> Optional[int]:
_ = datetime.fromtimestamp(v)
return v
return v

def validate_integer_size(v: int, min: int, max: int) -> Optional[int]:
if v is not None:
if v<max and v>min:
return v
elif v<min:
e = ValueError(f"Minimum value is {min}")
raise e
elif v>max:
e = ValueError(f"Maximum value is {max}")
raise e

return v
19 changes: 19 additions & 0 deletions tests/test_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
DatePicker,
DatetimePicker,
ExternalSelect,
FileInput,
Image,
MultiChannelsSelect,
MultiConversationsSelect,
Expand Down Expand Up @@ -1557,3 +1558,21 @@ def test_timepicker_excessive_action_id_raises_exception():
def test_timepicker_excessive_placeholder_raises_exception():
with pytest.raises(ValidationError):
TimePicker(placeholder=PlainText(text="p" * 151))

def test_builds_fileinput():
assert FileInput(
action_id="action_id",
filetypes=["file"],
maxfiles=1,
focus_on_load=True
).build() == {
"type": "file_input",
"action_id": "action_id",
"filetypes": ["file"],
"maxfiles": 1,
"focus_on_load": True
}

def test_fileinput_excessive_maxfiles_raises_exception():
with pytest.raises(ValidationError):
FileInput(maxfiles=11)
Loading