python: Reformat with Black, except quotes.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg
2021-02-11 23:19:30 -08:00
committed by Tim Abbott
parent 5028c081cb
commit 11741543da
817 changed files with 44952 additions and 24860 deletions

View File

@@ -32,11 +32,15 @@ class TrelloHookTests(WebhookTestCase):
self.check_webhook("removing_label_from_card", "Welcome Board", expected_message)
def test_trello_webhook_when_member_was_added_to_card(self) -> None:
expected_message = "TomaszKolek added TomaszKolek to [Card name](https://trello.com/c/9BduUcVQ)."
expected_message = (
"TomaszKolek added TomaszKolek to [Card name](https://trello.com/c/9BduUcVQ)."
)
self.check_webhook("adding_member_to_card", "Welcome Board", expected_message)
def test_trello_webhook_when_member_was_removed_from_card(self) -> None:
expected_message = "TomaszKolek removed Trello from [Card name](https://trello.com/c/9BduUcVQ)."
expected_message = (
"TomaszKolek removed Trello from [Card name](https://trello.com/c/9BduUcVQ)."
)
self.check_webhook("removing_member_from_card", "Welcome Board", expected_message)
def test_trello_webhook_when_due_date_was_set(self) -> None:
@@ -48,7 +52,9 @@ class TrelloHookTests(WebhookTestCase):
self.check_webhook("changing_due_date_on_card", "Welcome Board", expected_message)
def test_trello_webhook_when_due_date_was_removed(self) -> None:
expected_message = "TomaszKolek removed the due date from [Card name](https://trello.com/c/9BduUcVQ)."
expected_message = (
"TomaszKolek removed the due date from [Card name](https://trello.com/c/9BduUcVQ)."
)
self.check_webhook("removing_due_date_from_card", "Welcome Board", expected_message)
def test_trello_webhook_when_card_was_archived(self) -> None:
@@ -80,15 +86,21 @@ class TrelloHookTests(WebhookTestCase):
self.check_webhook("uncheck_item_on_card_checklist", "Zulip", expected_message)
def test_trello_webhook_when_member_was_removed_from_board(self) -> None:
expected_message = "TomaszKolek removed Trello from [Welcome Board](https://trello.com/b/iqXXzYEj)."
expected_message = (
"TomaszKolek removed Trello from [Welcome Board](https://trello.com/b/iqXXzYEj)."
)
self.check_webhook("removing_member_from_board", "Welcome Board", expected_message)
def test_trello_webhook_when_member_was_added_to_board(self) -> None:
expected_message = "TomaszKolek added Trello to [Welcome Board](https://trello.com/b/iqXXzYEj)."
expected_message = (
"TomaszKolek added Trello to [Welcome Board](https://trello.com/b/iqXXzYEj)."
)
self.check_webhook("adding_member_to_board", "Welcome Board", expected_message)
def test_trello_webhook_when_list_was_added_to_board(self) -> None:
expected_message = "TomaszKolek added New list list to [Welcome Board](https://trello.com/b/iqXXzYEj)."
expected_message = (
"TomaszKolek added New list list to [Welcome Board](https://trello.com/b/iqXXzYEj)."
)
self.check_webhook("adding_new_list_to_board", "Welcome Board", expected_message)
def test_trello_webhook_when_comment_was_added_to_card(self) -> None:
@@ -150,10 +162,7 @@ class TrelloHookTests(WebhookTestCase):
model="whatever",
action=dict(
type="updateCard",
data=dict(
card=card,
old=old
),
data=dict(card=card, old=old),
),
)
payload = orjson.dumps(data).decode()
@@ -164,7 +173,9 @@ class TrelloHookTests(WebhookTestCase):
self.check_webhook("adding_description_to_card", "Welcome Board", expected_message)
def test_trello_webhook_when_description_was_removed_from_card(self) -> None:
expected_message = "Marco Matarazzo removed description from [New Card](https://trello.com/c/P2r0z66z)."
expected_message = (
"Marco Matarazzo removed description from [New Card](https://trello.com/c/P2r0z66z)."
)
self.check_webhook("removing_description_from_card", "Welcome Board", expected_message)
def test_trello_webhook_when_description_was_changed_on_card(self) -> None:

View File

@@ -18,9 +18,11 @@ from .card_actions import IGNORED_CARD_ACTIONS, SUPPORTED_CARD_ACTIONS, process_
@webhook_view('Trello')
@return_success_on_head_request
@has_request_variables
def api_trello_webhook(request: HttpRequest,
user_profile: UserProfile,
payload: Mapping[str, Any]=REQ(argument_type='body')) -> HttpResponse:
def api_trello_webhook(
request: HttpRequest,
user_profile: UserProfile,
payload: Mapping[str, Any] = REQ(argument_type='body'),
) -> HttpResponse:
payload = orjson.loads(request.body)
action_type = payload['action'].get('type')
message = get_subject_and_body(payload, action_type)
@@ -32,6 +34,7 @@ def api_trello_webhook(request: HttpRequest,
check_send_webhook_message(request, user_profile, subject, body)
return json_success()
def get_subject_and_body(payload: Mapping[str, Any], action_type: str) -> Optional[Tuple[str, str]]:
if action_type in SUPPORTED_CARD_ACTIONS:
return process_card_action(payload, action_type)

View File

@@ -23,13 +23,16 @@ ACTIONS_TO_MESSAGE_MAPPER = {
CHANGE_NAME: 'renamed the board from {old_name} to {board_url_template}.',
}
def process_board_action(payload: Mapping[str, Any],
action_type: Optional[str]) -> Optional[Tuple[str, str]]:
def process_board_action(
payload: Mapping[str, Any], action_type: Optional[str]
) -> Optional[Tuple[str, str]]:
action_type = get_proper_action(payload, action_type)
if action_type is not None:
return get_subject(payload), get_body(payload, action_type)
return None
def get_proper_action(payload: Mapping[str, Any], action_type: Optional[str]) -> Optional[str]:
if action_type == 'updateBoard':
data = get_action_data(payload)
@@ -42,26 +45,31 @@ def get_proper_action(payload: Mapping[str, Any], action_type: Optional[str]) ->
raise UnsupportedWebhookEventType(action_type)
return action_type
def get_subject(payload: Mapping[str, Any]) -> str:
return get_action_data(payload)['board']['name']
def get_body(payload: Mapping[str, Any], action_type: str) -> str:
message_body = ACTIONS_TO_FILL_BODY_MAPPER[action_type](payload, action_type)
creator = payload['action']['memberCreator']['fullName']
return f'{creator} {message_body}'
def get_managed_member_body(payload: Mapping[str, Any], action_type: str) -> str:
data = {
'member_name': payload['action']['member']['fullName'],
}
return fill_appropriate_message_content(payload, action_type, data)
def get_create_list_body(payload: Mapping[str, Any], action_type: str) -> str:
data = {
'list_name': get_action_data(payload)['list']['name'],
}
return fill_appropriate_message_content(payload, action_type, data)
def get_change_name_body(payload: Mapping[str, Any], action_type: str) -> str:
data = {
'old_name': get_action_data(payload)['old']['name'],
@@ -69,31 +77,38 @@ def get_change_name_body(payload: Mapping[str, Any], action_type: str) -> str:
return fill_appropriate_message_content(payload, action_type, data)
def fill_appropriate_message_content(payload: Mapping[str, Any],
action_type: str,
data: Mapping[str, Any] = {}) -> str:
def fill_appropriate_message_content(
payload: Mapping[str, Any], action_type: str, data: Mapping[str, Any] = {}
) -> str:
data = dict(data)
if 'board_url_template' not in data:
data['board_url_template'] = get_filled_board_url_template(payload)
message_body = get_message_body(action_type)
return message_body.format(**data)
def get_filled_board_url_template(payload: Mapping[str, Any]) -> str:
return TRELLO_BOARD_URL_TEMPLATE.format(board_name=get_board_name(payload),
board_url=get_board_url(payload))
return TRELLO_BOARD_URL_TEMPLATE.format(
board_name=get_board_name(payload), board_url=get_board_url(payload)
)
def get_board_name(payload: Mapping[str, Any]) -> str:
return get_action_data(payload)['board']['name']
def get_board_url(payload: Mapping[str, Any]) -> str:
return 'https://trello.com/b/{}'.format(get_action_data(payload)['board']['shortLink'])
def get_message_body(action_type: str) -> str:
return ACTIONS_TO_MESSAGE_MAPPER[action_type]
def get_action_data(payload: Mapping[str, Any]) -> Mapping[str, Any]:
return payload['action']['data']
ACTIONS_TO_FILL_BODY_MAPPER = {
REMOVE_MEMBER: get_managed_member_body,
ADD_MEMBER: get_managed_member_body,

View File

@@ -49,8 +49,10 @@ ACTIONS_TO_MESSAGE_MAPPER = {
CHANGE_LIST: 'moved {card_url_template} from {old_list} to {new_list}.',
CHANGE_NAME: 'renamed the card from "{old_name}" to {card_url_template}.',
SET_DESC: 'set description for {card_url_template} to:\n~~~ quote\n{desc}\n~~~\n',
CHANGE_DESC: ('changed description for {card_url_template} from\n' +
'~~~ quote\n{old_desc}\n~~~\nto\n~~~ quote\n{desc}\n~~~\n'),
CHANGE_DESC: (
'changed description for {card_url_template} from\n'
+ '~~~ quote\n{old_desc}\n~~~\nto\n~~~ quote\n{desc}\n~~~\n'
),
REMOVE_DESC: 'removed description from {card_url_template}.',
ARCHIVE: 'archived {card_url_template}.',
REOPEN: 'reopened {card_url_template}.',
@@ -67,15 +69,18 @@ ACTIONS_TO_MESSAGE_MAPPER = {
UPDATE_CHECK_ITEM_STATE: '{action} **{item_name}** in **{checklist_name}** ({card_url_template}).',
}
def prettify_date(date_string: str) -> str:
return date_string.replace('T', ' ').replace('.000', '').replace('Z', ' UTC')
def process_card_action(payload: Mapping[str, Any], action_type: str) -> Optional[Tuple[str, str]]:
proper_action = get_proper_action(payload, action_type)
if proper_action is not None:
return get_subject(payload), get_body(payload, proper_action)
return None
def get_proper_action(payload: Mapping[str, Any], action_type: str) -> Optional[str]:
if action_type == 'updateCard':
data = get_action_data(payload)
@@ -119,20 +124,24 @@ def get_proper_action(payload: Mapping[str, Any], action_type: str) -> Optional[
return action_type
def get_subject(payload: Mapping[str, Any]) -> str:
return get_action_data(payload)['board'].get('name')
def get_body(payload: Mapping[str, Any], action_type: str) -> str:
message_body = ACTIONS_TO_FILL_BODY_MAPPER[action_type](payload, action_type)
creator = payload['action']['memberCreator'].get('fullName')
return f'{creator} {message_body}'
def get_added_checklist_body(payload: Mapping[str, Any], action_type: str) -> str:
data = {
'checklist_name': get_action_data(payload)['checklist'].get('name'),
}
return fill_appropriate_message_content(payload, action_type, data)
def get_update_check_item_body(payload: Mapping[str, Any], action_type: str) -> str:
action = get_action_data(payload)
state = action['checkItem']['state']
@@ -143,6 +152,7 @@ def get_update_check_item_body(payload: Mapping[str, Any], action_type: str) ->
}
return fill_appropriate_message_content(payload, action_type, data)
def get_added_attachment_body(payload: Mapping[str, Any], action_type: str) -> str:
data = {
'attachment_url': get_action_data(payload)['attachment'].get('url'),
@@ -150,6 +160,7 @@ def get_added_attachment_body(payload: Mapping[str, Any], action_type: str) -> s
}
return fill_appropriate_message_content(payload, action_type, data)
def get_updated_card_body(payload: Mapping[str, Any], action_type: str) -> str:
data = {
'card_name': get_card_name(payload),
@@ -158,6 +169,7 @@ def get_updated_card_body(payload: Mapping[str, Any], action_type: str) -> str:
}
return fill_appropriate_message_content(payload, action_type, data)
def get_renamed_card_body(payload: Mapping[str, Any], action_type: str) -> str:
data = {
@@ -166,6 +178,7 @@ def get_renamed_card_body(payload: Mapping[str, Any], action_type: str) -> str:
}
return fill_appropriate_message_content(payload, action_type, data)
def get_added_label_body(payload: Mapping[str, Any], action_type: str) -> str:
data = {
'color': get_action_data(payload).get('value'),
@@ -173,24 +186,28 @@ def get_added_label_body(payload: Mapping[str, Any], action_type: str) -> str:
}
return fill_appropriate_message_content(payload, action_type, data)
def get_managed_member_body(payload: Mapping[str, Any], action_type: str) -> str:
data = {
'member_name': payload['action']['member'].get('fullName'),
}
return fill_appropriate_message_content(payload, action_type, data)
def get_comment_body(payload: Mapping[str, Any], action_type: str) -> str:
data = {
'text': get_action_data(payload)['text'],
}
return fill_appropriate_message_content(payload, action_type, data)
def get_managed_due_date_body(payload: Mapping[str, Any], action_type: str) -> str:
data = {
'due_date': prettify_date(get_action_data(payload)['card'].get('due')),
}
return fill_appropriate_message_content(payload, action_type, data)
def get_changed_due_date_body(payload: Mapping[str, Any], action_type: str) -> str:
data = {
'due_date': prettify_date(get_action_data(payload)['card'].get('due')),
@@ -198,12 +215,14 @@ def get_changed_due_date_body(payload: Mapping[str, Any], action_type: str) -> s
}
return fill_appropriate_message_content(payload, action_type, data)
def get_managed_desc_body(payload: Mapping[str, Any], action_type: str) -> str:
data = {
'desc': get_action_data(payload)['card']['desc'],
}
return fill_appropriate_message_content(payload, action_type, data)
def get_changed_desc_body(payload: Mapping[str, Any], action_type: str) -> str:
data = {
'desc': get_action_data(payload)['card']['desc'],
@@ -211,33 +230,43 @@ def get_changed_desc_body(payload: Mapping[str, Any], action_type: str) -> str:
}
return fill_appropriate_message_content(payload, action_type, data)
def get_body_by_action_type_without_data(payload: Mapping[str, Any], action_type: str) -> str:
return fill_appropriate_message_content(payload, action_type)
def fill_appropriate_message_content(payload: Mapping[str, Any],
action_type: str,
data: Mapping[str, Any] = {}) -> str:
def fill_appropriate_message_content(
payload: Mapping[str, Any], action_type: str, data: Mapping[str, Any] = {}
) -> str:
data = dict(data)
if 'card_url_template' not in data:
data['card_url_template'] = get_filled_card_url_template(payload)
message_body = get_message_body(action_type)
return message_body.format(**data)
def get_filled_card_url_template(payload: Mapping[str, Any]) -> str:
return TRELLO_CARD_URL_TEMPLATE.format(card_name=get_card_name(payload), card_url=get_card_url(payload))
return TRELLO_CARD_URL_TEMPLATE.format(
card_name=get_card_name(payload), card_url=get_card_url(payload)
)
def get_card_url(payload: Mapping[str, Any]) -> str:
return 'https://trello.com/c/{}'.format(get_action_data(payload)['card'].get('shortLink'))
def get_message_body(action_type: str) -> str:
return ACTIONS_TO_MESSAGE_MAPPER[action_type]
def get_card_name(payload: Mapping[str, Any]) -> str:
return get_action_data(payload)['card'].get('name')
def get_action_data(payload: Mapping[str, Any]) -> Mapping[str, Any]:
return payload['action'].get('data')
ACTIONS_TO_FILL_BODY_MAPPER = {
CREATE: get_body_by_action_type_without_data,
CHANGE_LIST: get_updated_card_body,