Skip to content

Commit

Permalink
Merge pull request #699 from seratch/confirm-style
Browse files Browse the repository at this point in the history
Add style to confirm in a block element
  • Loading branch information
seratch committed May 22, 2020
2 parents ac3b8aa + 9af283c commit 0725f4f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
9 changes: 9 additions & 0 deletions slack/web/classes/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ def __init__(
text: Union[str, dict, TextObject],
confirm: Union[str, dict, PlainTextObject] = "Yes",
deny: Union[str, dict, PlainTextObject] = "No",
style: str = None,
):
"""
An object that defines a dialog that provides a confirmation step to any
Expand All @@ -261,12 +262,14 @@ def __init__(
self._text = TextObject.parse(text, default_type=MarkdownTextObject.type)
self._confirm = TextObject.parse(confirm, default_type=PlainTextObject.type)
self._deny = TextObject.parse(deny, default_type=PlainTextObject.type)
self._style = style

# for backward-compatibility with version 2.0-2.5, the following fields return str values
self.title = self._title.text if self._title else None
self.text = self._text.text if self._text else None
self.confirm = self._confirm.text if self._confirm else None
self.deny = self._deny.text if self._deny else None
self.style = self._style

@JsonValidator(f"title attribute cannot exceed {title_max_length} characters")
def title_length(self):
Expand All @@ -286,6 +289,10 @@ def confirm_length(self):
def deny_length(self):
return self._deny is None or len(self._deny.text) <= self.deny_max_length

@JsonValidator('style for confirm must be either "primary" or "danger"')
def _validate_confirm_style(self):
return self._style is None or self._style in ["primary", "danger"]

def to_dict(self, option_type: str = "block") -> dict:
if option_type == "action":
# deliberately skipping JSON validators here - can't find documentation
Expand Down Expand Up @@ -315,6 +322,8 @@ def to_dict(self, option_type: str = "block") -> dict:
json["confirm"] = self._confirm.to_dict()
if self._deny:
json["deny"] = self._deny.to_dict()
if self._style:
json["style"] = self._style
return json


Expand Down
44 changes: 44 additions & 0 deletions tests/web/classes/test_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,3 +476,47 @@ def test_options_length(self):
"text"
)

def test_confirm_style(self):
obj = ConfirmObject.parse({
"title": {
"type": "plain_text",
"text": "Are you sure?"
},
"text": {
"type": "mrkdwn",
"text": "Wouldn't you prefer a good game of _chess_?"
},
"confirm": {
"type": "plain_text",
"text": "Do it"
},
"deny": {
"type": "plain_text",
"text": "Stop, I've changed my mind!"
},
"style": "primary"
})
obj.validate_json()
self.assertEqual("primary", obj.style)

def test_confirm_style_validation(self):
with self.assertRaises(SlackObjectFormationError):
ConfirmObject.parse({
"title": {
"type": "plain_text",
"text": "Are you sure?"
},
"text": {
"type": "mrkdwn",
"text": "Wouldn't you prefer a good game of _chess_?"
},
"confirm": {
"type": "plain_text",
"text": "Do it"
},
"deny": {
"type": "plain_text",
"text": "Stop, I've changed my mind!"
},
"style": "something-wrong"
}).validate_json()

0 comments on commit 0725f4f

Please sign in to comment.