From 5f91cb53be2513a7ae74192b0cfc88a0af27d645 Mon Sep 17 00:00:00 2001 From: Cameron Leavenworth Date: Wed, 28 Feb 2024 13:39:16 -0800 Subject: [PATCH 1/7] Added FileInput input element type, first working iteration. --- blockkit/blocks.py | 2 ++ blockkit/elements.py | 24 ++++++++++++++++++++++++ tests/test_elements.py | 1 + 3 files changed, 27 insertions(+) diff --git a/blockkit/blocks.py b/blockkit/blocks.py index 9d5c2ac..d643dcf 100644 --- a/blockkit/blocks.py +++ b/blockkit/blocks.py @@ -12,6 +12,7 @@ DatePicker, DatetimePicker, ExternalSelect, + FileInput, Image, MultiChannelsSelect, MultiConversationsSelect, @@ -154,6 +155,7 @@ def __init__( DatetimePicker, TimePicker, NumberInput, + FileInput ] diff --git a/blockkit/elements.py b/blockkit/elements.py index 55700f2..68fadbe 100644 --- a/blockkit/elements.py +++ b/blockkit/elements.py @@ -51,6 +51,7 @@ "RichTextQuote", "RichTextSection", "RichTextList", + "FileInput" ] @@ -717,3 +718,26 @@ 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) + # dispatch_action_config: Optional[DispatchActionConfig] = None + + def __init__( + self, + *, + action_id: Optional[str] = None, + filetypes: Optional[List[str]] = None, + maxfiles: Optional[int] = None, + # dispatch_action_config: Optional[DispatchActionConfig] = None, + focus_on_load: Optional[bool] = None, + ): + super().__init__( + action_id=action_id, + filetypes=filetypes, + maxfiles=maxfiles, + # dispatch_action_config=dispatch_action_config, + focus_on_load=focus_on_load, + ) \ No newline at end of file diff --git a/tests/test_elements.py b/tests/test_elements.py index 08f5022..b4be435 100644 --- a/tests/test_elements.py +++ b/tests/test_elements.py @@ -12,6 +12,7 @@ DatePicker, DatetimePicker, ExternalSelect, + FileInput, Image, MultiChannelsSelect, MultiConversationsSelect, From ef22df7ccf56e05c3d6680faf6d1f661ece6be37 Mon Sep 17 00:00:00 2001 From: Cameron Leavenworth Date: Wed, 28 Feb 2024 14:05:58 -0800 Subject: [PATCH 2/7] Added tests for FileInput maxfiles argument --- blockkit/elements.py | 7 ++++++- blockkit/validators.py | 13 +++++++++++++ tests/test_elements.py | 18 ++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/blockkit/elements.py b/blockkit/elements.py index 68fadbe..64c5d79 100644 --- a/blockkit/elements.py +++ b/blockkit/elements.py @@ -23,6 +23,7 @@ validate_datetime, validate_text_length, validate_time, + validate_integer_size, validator, ) @@ -740,4 +741,8 @@ def __init__( maxfiles=maxfiles, # dispatch_action_config=dispatch_action_config, focus_on_load=focus_on_load, - ) \ No newline at end of file + ) + + _validate_maxfiles = validator( + "maxfiles", validate_integer_size, min=1, max=10 + ) \ No newline at end of file diff --git a/blockkit/validators.py b/blockkit/validators.py index 581a63e..6cc503d 100644 --- a/blockkit/validators.py +++ b/blockkit/validators.py @@ -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 vmin: + return v + elif vmax: + e = ValueError(f"Maximum value is {max}") + raise e + + return v \ No newline at end of file diff --git a/tests/test_elements.py b/tests/test_elements.py index b4be435..65f720f 100644 --- a/tests/test_elements.py +++ b/tests/test_elements.py @@ -1558,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) \ No newline at end of file From bfeaa9070dbfdbe752d4b4b6a76404f1a705e272 Mon Sep 17 00:00:00 2001 From: Cameron Leavenworth Date: Wed, 28 Feb 2024 14:15:39 -0800 Subject: [PATCH 3/7] Remove dispatch_action_config from FileInput Updated Readme --- README.md | 1 + blockkit/elements.py | 3 --- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/README.md b/README.md index a04cf51..936eb1a 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/blockkit/elements.py b/blockkit/elements.py index 64c5d79..96418c5 100644 --- a/blockkit/elements.py +++ b/blockkit/elements.py @@ -724,7 +724,6 @@ class FileInput(FocusableElement): type: str = "file_input" filetypes: Optional[List[str]] = Field(None) maxfiles: Optional[int] = Field(None) - # dispatch_action_config: Optional[DispatchActionConfig] = None def __init__( self, @@ -732,14 +731,12 @@ def __init__( action_id: Optional[str] = None, filetypes: Optional[List[str]] = None, maxfiles: Optional[int] = None, - # dispatch_action_config: Optional[DispatchActionConfig] = None, focus_on_load: Optional[bool] = None, ): super().__init__( action_id=action_id, filetypes=filetypes, maxfiles=maxfiles, - # dispatch_action_config=dispatch_action_config, focus_on_load=focus_on_load, ) From 4ddba5f1c5497b3976c9b089421a6ed6c7faee39 Mon Sep 17 00:00:00 2001 From: Cameron Leavenworth Date: Mon, 4 Mar 2024 10:47:37 -0500 Subject: [PATCH 4/7] Removed custom validator to replace with Field() --- blockkit/elements.py | 10 ++-------- blockkit/validators.py | 13 ------------- 2 files changed, 2 insertions(+), 21 deletions(-) diff --git a/blockkit/elements.py b/blockkit/elements.py index 96418c5..01b3283 100644 --- a/blockkit/elements.py +++ b/blockkit/elements.py @@ -723,7 +723,7 @@ def __init__( class FileInput(FocusableElement): type: str = "file_input" filetypes: Optional[List[str]] = Field(None) - maxfiles: Optional[int] = Field(None) + maxfiles: Optional[int] = Field(..., gt=0, le=10) def __init__( self, @@ -731,15 +731,9 @@ def __init__( action_id: Optional[str] = None, filetypes: Optional[List[str]] = None, maxfiles: Optional[int] = None, - focus_on_load: Optional[bool] = None, ): 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 - ) \ No newline at end of file + ) \ No newline at end of file diff --git a/blockkit/validators.py b/blockkit/validators.py index 6cc503d..6b6e254 100644 --- a/blockkit/validators.py +++ b/blockkit/validators.py @@ -52,17 +52,4 @@ def validate_datetime(v: Union[int, datetime]) -> Optional[int]: else: _ = 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 vmin: - return v - elif vmax: - e = ValueError(f"Maximum value is {max}") - raise e - return v \ No newline at end of file From e08fa5c2c2eb183ca15cfd51e415642f7d5fcf1e Mon Sep 17 00:00:00 2001 From: Cameron Leavenworth Date: Mon, 4 Mar 2024 10:53:04 -0500 Subject: [PATCH 5/7] Removed stale import --- blockkit/elements.py | 1 - 1 file changed, 1 deletion(-) diff --git a/blockkit/elements.py b/blockkit/elements.py index 01b3283..d3617c8 100644 --- a/blockkit/elements.py +++ b/blockkit/elements.py @@ -23,7 +23,6 @@ validate_datetime, validate_text_length, validate_time, - validate_integer_size, validator, ) From 3ebc9b3623764d0a2ca51f7c7e84c8ada8fd31e7 Mon Sep 17 00:00:00 2001 From: Cameron Leavenworth Date: Tue, 5 Mar 2024 09:43:34 -0500 Subject: [PATCH 6/7] Resolve pytest error for file_input --- tests/test_elements.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/test_elements.py b/tests/test_elements.py index 65f720f..df478a5 100644 --- a/tests/test_elements.py +++ b/tests/test_elements.py @@ -1564,13 +1564,11 @@ def test_builds_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(): From 883b0eac62506bf9b9dfd6edad7789b1ea1aa5e6 Mon Sep 17 00:00:00 2001 From: Cameron Leavenworth Date: Tue, 5 Mar 2024 09:50:00 -0500 Subject: [PATCH 7/7] Change FileInput class inherit to ActionableComponent --- blockkit/elements.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/blockkit/elements.py b/blockkit/elements.py index d3617c8..81dea60 100644 --- a/blockkit/elements.py +++ b/blockkit/elements.py @@ -719,7 +719,7 @@ def __init__( ): super().__init__(elements=elements, style=style, indent=indent) -class FileInput(FocusableElement): +class FileInput(ActionableComponent): type: str = "file_input" filetypes: Optional[List[str]] = Field(None) maxfiles: Optional[int] = Field(..., gt=0, le=10)