2016-05-05 07:06:41 +02:00
|
|
|
# Webhooks for external integrations.
|
|
|
|
from __future__ import absolute_import
|
|
|
|
import ujson
|
2016-12-04 18:45:46 +01:00
|
|
|
from typing import Mapping, Any, Tuple, Text
|
2016-05-05 07:06:41 +02:00
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
from zerver.lib.actions import check_send_message
|
2016-11-15 17:20:22 +01:00
|
|
|
from zerver.decorator import return_success_on_head_request
|
2016-05-05 07:06:41 +02:00
|
|
|
from zerver.lib.response import json_success, json_error
|
|
|
|
from zerver.models import UserProfile, Client
|
|
|
|
from zerver.decorator import REQ, has_request_variables, api_key_only_webhook_view
|
|
|
|
|
2016-11-23 20:15:23 +01:00
|
|
|
from .view.card_actions import SUPPORTED_CARD_ACTIONS, process_card_action
|
|
|
|
from .view.board_actions import SUPPORTED_BOARD_ACTIONS, process_board_action
|
|
|
|
from .view.exceptions import UnsupportedAction
|
2016-05-05 07:06:41 +02:00
|
|
|
|
|
|
|
@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
|
|
|
|
def api_trello_webhook(request, user_profile, client, payload=REQ(argument_type='body'), stream=REQ(default='trello')):
|
2016-12-04 18:45:46 +01:00
|
|
|
# type: (HttpRequest, UserProfile, Client, Mapping[str, Any], Text) -> HttpResponse
|
2016-05-05 07:06:41 +02:00
|
|
|
payload = ujson.loads(request.body)
|
|
|
|
action_type = payload.get('action').get('type')
|
|
|
|
try:
|
|
|
|
subject, body = get_subject_and_body(payload, action_type)
|
|
|
|
except UnsupportedAction:
|
|
|
|
return json_error(_('Unsupported action_type: {action_type}'.format(action_type=action_type)))
|
|
|
|
|
|
|
|
check_send_message(user_profile, client, 'stream', [stream], subject, body)
|
|
|
|
return json_success()
|
|
|
|
|
|
|
|
def get_subject_and_body(payload, action_type):
|
2016-12-04 18:45:46 +01:00
|
|
|
# type: (Mapping[str, Any], Text) -> 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))
|