2016-05-05 07:06:41 +02:00
|
|
|
# Webhooks for external integrations.
|
|
|
|
import ujson
|
2017-08-06 08:31:36 +02:00
|
|
|
from typing import Mapping, Any, Tuple, Text, Optional
|
2016-05-05 07:06:41 +02:00
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
2017-10-31 04:25:48 +01:00
|
|
|
from zerver.decorator import api_key_only_webhook_view, return_success_on_head_request
|
2017-09-30 04:18:16 +02:00
|
|
|
from zerver.lib.actions import check_send_stream_message
|
2016-05-05 07:06:41 +02:00
|
|
|
from zerver.lib.response import json_success, json_error
|
2017-10-31 04:25:48 +01:00
|
|
|
from zerver.lib.request import REQ, has_request_variables
|
2017-05-02 01:00:50 +02:00
|
|
|
from zerver.models import UserProfile
|
2016-05-05 07:06:41 +02:00
|
|
|
|
|
|
|
from .card_actions import SUPPORTED_CARD_ACTIONS, process_card_action
|
|
|
|
from .board_actions import SUPPORTED_BOARD_ACTIONS, process_board_action
|
|
|
|
from .exceptions import UnsupportedAction
|
|
|
|
|
|
|
|
@api_key_only_webhook_view('Trello')
|
2016-11-15 17:20:22 +01:00
|
|
|
@return_success_on_head_request
|
2016-05-05 07:06:41 +02:00
|
|
|
@has_request_variables
|
2017-05-02 01:00:50 +02:00
|
|
|
def api_trello_webhook(request, user_profile, payload=REQ(argument_type='body'), stream=REQ(default='trello')):
|
|
|
|
# type: (HttpRequest, UserProfile, Mapping[str, Any], Text) -> HttpResponse
|
2016-05-05 07:06:41 +02:00
|
|
|
payload = ujson.loads(request.body)
|
2017-05-24 23:03:06 +02:00
|
|
|
action_type = payload['action'].get('type')
|
2016-05-05 07:06:41 +02:00
|
|
|
try:
|
2017-07-27 06:33:57 +02:00
|
|
|
message = get_subject_and_body(payload, action_type)
|
|
|
|
if message is None:
|
|
|
|
return json_success()
|
|
|
|
else:
|
|
|
|
subject, body = message
|
2016-05-05 07:06:41 +02:00
|
|
|
except UnsupportedAction:
|
|
|
|
return json_error(_('Unsupported action_type: {action_type}'.format(action_type=action_type)))
|
|
|
|
|
2017-09-30 04:18:16 +02:00
|
|
|
check_send_stream_message(user_profile, request.client, stream, subject, body)
|
2016-05-05 07:06:41 +02:00
|
|
|
return json_success()
|
|
|
|
|
|
|
|
def get_subject_and_body(payload, action_type):
|
2017-08-06 08:31:36 +02:00
|
|
|
# type: (Mapping[str, Any], Text) -> Optional[Tuple[Text, Text]]
|
2016-05-05 07:06:41 +02:00
|
|
|
if action_type in SUPPORTED_CARD_ACTIONS:
|
|
|
|
return process_card_action(payload, action_type)
|
|
|
|
if action_type in SUPPORTED_BOARD_ACTIONS:
|
|
|
|
return process_board_action(payload, action_type)
|
|
|
|
raise UnsupportedAction('{} if not supported'.format(action_type))
|