Skip to content

Commit

Permalink
[BUG] Fix issues #64 #65 #66
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklambourne committed Feb 9, 2024
1 parent cab8f97 commit afad928
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 14 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "slackblocks"
version = "1.0.1"
version = "1.0.2"
description = "Python wrapper for the Slack Blocks API"
authors = [
"Nicholas Lambourne <[email protected]>",
Expand Down
2 changes: 2 additions & 0 deletions slackblocks/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
ConversationMultiSelectMenu,
ConversationSelectMenu,
DatePicker,
DateTimePicker,
Element,
ElementType,
ExternalMultiSelectMenu,
Expand Down Expand Up @@ -45,6 +46,7 @@
CheckboxGroup,
RadioButtonGroup,
DatePicker,
DateTimePicker,
ChannelSelectMenu,
ChannelMultiSelectMenu,
ConversationSelectMenu,
Expand Down
39 changes: 32 additions & 7 deletions slackblocks/elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
SlackFile,
Text,
TextLike,
TextType,
Workflow,
)
from .rich_text import RichText
Expand Down Expand Up @@ -243,6 +244,8 @@ def __init__(
self.initial_date = datetime.strptime(initial_date, "%Y-%m-%d").strftime(
"%Y-%m-%d"
)
else:
self.initial_date = None
self.confirm = confirm
self.focus_on_load = focus_on_load
self.placeholder = Text.to_text(
Expand All @@ -252,7 +255,7 @@ def __init__(
def _resolve(self) -> Dict[str, Any]:
date_picker = self._attributes()
date_picker["action_id"] = self.action_id
if self.initial_date:
if self.initial_date is not None:
date_picker["initial_date"] = self.initial_date
if self.confirm:
date_picker["confirm"] = self.confirm
Expand Down Expand Up @@ -531,12 +534,20 @@ def __init__(
"If using `option_groups` then `initial_options` must also be of type "
f"`List[OptionGroup]`, not `{type(self.initial_options)}`."
)
self.options = coerce_to_list(
options, class_=Option, allow_none=True, max_size=100
)
self.option_groups = coerce_to_list(
option_groups, class_=OptionGroup, allow_none=True, max_size=100
)

# Check that Option Text is all TextType.PLAINTEXT
if self.options:
options_to_validate = self.options
if self.option_groups:
options_to_validate = sum(
[option_group.options for option_group in option_groups], []
)
for option in options_to_validate:
if option.text.text_type == TextType.MARKDOWN:
raise InvalidUsageError(
"Text in Options for StaticSelectMenu can only be of TextType.PLAINTEXT"
)

self.confirm = confirm
self.max_selected_items = max_selected_items
self.focus_on_load = focus_on_load
Expand Down Expand Up @@ -1169,6 +1180,20 @@ def __init__(
"If using `option_groups` then `initial_option` must also be of type "
f"`OptionGroup`, not `{type(initial_option)}`."
)

# Check that Option Text is all TextType.PLAINTEXT
if self.options:
options_to_validate = self.options
if self.option_groups:
options_to_validate = sum(
[option_group.options for option_group in option_groups], []
)
for option in options_to_validate:
if option.text.text_type == TextType.MARKDOWN:
raise InvalidUsageError(
"Text in Options for StaticSelectMenu can only be of TextType.PLAINTEXT"
)

self.initial_option = initial_option
self.confirm = confirm
self.focus_on_load = focus_on_load
Expand Down
13 changes: 8 additions & 5 deletions slackblocks/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ class OptionGroup(CompositionObject):
Args:
label: a label shown above the group of options.
options: a list of `Option` objects that will form the contents of the group.
options: a list of `Option` objects that will form the contents of the group (max 100).
Throws:
InvalidUsageError: if no options are provided or the label is not valid.
Expand All @@ -300,10 +300,13 @@ class OptionGroup(CompositionObject):
def __init__(self, label: TextLike, options: List[Option]):
super().__init__(type_=CompositionObjectType.OPTION_GROUP)
self.label = Text.to_text(label, max_length=75, force_plaintext=True)
if options:
self.options = options
else:
raise InvalidUsageError("Field `options` cannot be empty")
self.options = coerce_to_list(
options,
class_=Option,
min_size=1,
max_size=100,
allow_none=False,
)

def _resolve(self) -> Dict[str, Any]:
option_group = {} # Does not include type in JSON
Expand Down
32 changes: 31 additions & 1 deletion test/unit/test_elements.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pytest

from slackblocks.elements import (
Button,
ButtonStyle,
Expand Down Expand Up @@ -25,7 +27,15 @@
UserSelectMenu,
WorkflowButton,
)
from slackblocks.objects import InputParameter, Text, TextType, Trigger, Workflow
from slackblocks.errors import InvalidUsageError
from slackblocks.objects import (
InputParameter,
Option,
Text,
TextType,
Trigger,
Workflow,
)
from slackblocks.rich_text import RichText

from .utils import OPTION_A, THREE_OPTIONS, TWO_OPTIONS, fetch_sample
Expand Down Expand Up @@ -136,6 +146,16 @@ def test_multi_select_static() -> None:
)


def test_multi_select_static_invalid_option() -> None:
with pytest.raises(InvalidUsageError):
multi_select_static = StaticMultiSelectMenu(
action_id="multi_static_select",
placeholder=Text("Select one or more", type_=TextType.PLAINTEXT),
options=TWO_OPTIONS
+ [Option(text=Text("C", type_=TextType.MARKDOWN), value="X")],
)


def test_multi_select_user() -> None:
multi_select_user = UserMultiSelectMenu(
action_id="multi_users_select",
Expand Down Expand Up @@ -214,6 +234,16 @@ def test_select_menu_static() -> None:
)


def test_select_menu_static_invalid_option() -> None:
with pytest.raises(InvalidUsageError):
select_menu_static = StaticSelectMenu(
action_id="static_select",
placeholder="Select one item",
options=THREE_OPTIONS
+ [Option(text=Text("C", type_=TextType.MARKDOWN), value="X")],
)


def test_select_menu_user() -> None:
select_menu_user = UserSelectMenu(
action_id="users_select", placeholder="Select one user"
Expand Down

0 comments on commit afad928

Please sign in to comment.