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

@@ -43,4 +43,6 @@ TODOS_SUPPORT_EVENTS = [
'todo_due_on_changed',
]
SUPPORT_EVENTS = DOC_SUPPORT_EVENTS + QUESTION_SUPPORT_EVENTS + MESSAGE_SUPPORT_EVENTS + TODOS_SUPPORT_EVENTS
SUPPORT_EVENTS = (
DOC_SUPPORT_EVENTS + QUESTION_SUPPORT_EVENTS + MESSAGE_SUPPORT_EVENTS + TODOS_SUPPORT_EVENTS
)

View File

@@ -2,6 +2,7 @@ from zerver.lib.test_classes import WebhookTestCase
TOPIC = "Zulip HQ"
class BasecampHookTests(WebhookTestCase):
STREAM_NAME = 'basecamp'
URL_TEMPLATE = "/api/v1/external/basecamp?stream={stream}&api_key={api_key}"

View File

@@ -15,17 +15,25 @@ from .support_event import SUPPORT_EVENTS
DOCUMENT_TEMPLATE = "{user_name} {verb} the document [{title}]({url})"
QUESTION_TEMPLATE = "{user_name} {verb} the question [{title}]({url})"
QUESTIONS_ANSWER_TEMPLATE = ("{user_name} {verb} the [answer]({answer_url}) " +
"of the question [{question_title}]({question_url})")
COMMENT_TEMPLATE = "{user_name} {verb} the [comment]({answer_url}) of the task [{task_title}]({task_url})"
QUESTIONS_ANSWER_TEMPLATE = (
"{user_name} {verb} the [answer]({answer_url}) "
+ "of the question [{question_title}]({question_url})"
)
COMMENT_TEMPLATE = (
"{user_name} {verb} the [comment]({answer_url}) of the task [{task_title}]({task_url})"
)
MESSAGE_TEMPLATE = "{user_name} {verb} the message [{title}]({url})"
TODO_LIST_TEMPLATE = "{user_name} {verb} the todo list [{title}]({url})"
TODO_TEMPLATE = "{user_name} {verb} the todo task [{title}]({url})"
@webhook_view('Basecamp')
@has_request_variables
def api_basecamp_webhook(request: HttpRequest, user_profile: UserProfile,
payload: Dict[str, Any]=REQ(argument_type='body')) -> HttpResponse:
def api_basecamp_webhook(
request: HttpRequest,
user_profile: UserProfile,
payload: Dict[str, Any] = REQ(argument_type='body'),
) -> HttpResponse:
event = get_event_type(payload)
if event not in SUPPORT_EVENTS:
@@ -52,21 +60,27 @@ def api_basecamp_webhook(request: HttpRequest, user_profile: UserProfile,
check_send_webhook_message(request, user_profile, subject, body)
return json_success()
def get_project_name(payload: Dict[str, Any]) -> str:
return payload['recording']['bucket']['name']
def get_event_type(payload: Dict[str, Any]) -> str:
return payload['kind']
def get_event_creator(payload: Dict[str, Any]) -> str:
return payload['creator']['name']
def get_subject_url(payload: Dict[str, Any]) -> str:
return payload['recording']['app_url']
def get_subject_title(payload: Dict[str, Any]) -> str:
return payload['recording']['title']
def get_verb(event: str, prefix: str) -> str:
verb = event.replace(prefix, '')
if verb == 'active':
@@ -77,14 +91,17 @@ def get_verb(event: str, prefix: str) -> str:
return "changed {} of".format(matched.group('subject'))
return verb
def add_punctuation_if_necessary(body: str, title: str) -> str:
if title[-1] not in string.punctuation:
body = f'{body}.'
return body
def get_document_body(event: str, payload: Dict[str, Any]) -> str:
return get_generic_body(event, payload, 'document_', DOCUMENT_TEMPLATE)
def get_questions_answer_body(event: str, payload: Dict[str, Any]) -> str:
verb = get_verb(event, 'question_answer_')
question = payload['recording']['parent']
@@ -99,6 +116,7 @@ def get_questions_answer_body(event: str, payload: Dict[str, Any]) -> str:
question_url=question['app_url'],
)
def get_comment_body(event: str, payload: Dict[str, Any]) -> str:
verb = get_verb(event, 'comment_')
task = payload['recording']['parent']
@@ -112,18 +130,23 @@ def get_comment_body(event: str, payload: Dict[str, Any]) -> str:
task_url=task['app_url'],
)
def get_questions_body(event: str, payload: Dict[str, Any]) -> str:
return get_generic_body(event, payload, 'question_', QUESTION_TEMPLATE)
def get_message_body(event: str, payload: Dict[str, Any]) -> str:
return get_generic_body(event, payload, 'message_', MESSAGE_TEMPLATE)
def get_todo_list_body(event: str, payload: Dict[str, Any]) -> str:
return get_generic_body(event, payload, 'todolist_', TODO_LIST_TEMPLATE)
def get_todo_body(event: str, payload: Dict[str, Any]) -> str:
return get_generic_body(event, payload, 'todo_', TODO_TEMPLATE)
def get_generic_body(event: str, payload: Dict[str, Any], prefix: str, template: str) -> str:
verb = get_verb(event, prefix)
title = get_subject_title(payload)