2017-11-16 00:43:10 +01:00
|
|
|
from typing import Any, Dict, List
|
|
|
|
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
|
2017-10-31 04:25:48 +01:00
|
|
|
from zerver.decorator import api_key_only_webhook_view
|
|
|
|
from zerver.lib.request import REQ, has_request_variables
|
2019-02-02 23:53:55 +01:00
|
|
|
from zerver.lib.response import json_success
|
2018-03-16 22:53:50 +01:00
|
|
|
from zerver.lib.webhooks.common import check_send_webhook_message
|
2017-05-02 01:00:50 +02:00
|
|
|
from zerver.models import UserProfile
|
2016-12-22 16:11:51 +01:00
|
|
|
|
2018-10-04 17:23:27 +02:00
|
|
|
|
|
|
|
IS_AWAITING_SIGNATURE = "is awaiting the signature of {awaiting_recipients}"
|
|
|
|
WAS_JUST_SIGNED_BY = "was just signed by {signed_recipients}"
|
|
|
|
BODY = "The `{contract_title}` document {actions}."
|
|
|
|
|
|
|
|
def get_message_body(payload: Dict[str, Dict[str, Any]]) -> str:
|
|
|
|
contract_title = payload['signature_request']['title']
|
|
|
|
recipients = {} # type: Dict[str, List[str]]
|
|
|
|
signatures = payload['signature_request']['signatures']
|
|
|
|
|
|
|
|
for signature in signatures:
|
|
|
|
recipients.setdefault(signature['status_code'], [])
|
|
|
|
recipients[signature['status_code']].append(signature['signer_name'])
|
|
|
|
|
|
|
|
recipients_text = ""
|
|
|
|
if recipients.get('awaiting_signature'):
|
|
|
|
recipients_text += IS_AWAITING_SIGNATURE.format(
|
|
|
|
awaiting_recipients=get_recipients_text(recipients['awaiting_signature'])
|
|
|
|
)
|
|
|
|
|
|
|
|
if recipients.get('signed'):
|
|
|
|
text = WAS_JUST_SIGNED_BY.format(
|
|
|
|
signed_recipients=get_recipients_text(recipients['signed'])
|
|
|
|
)
|
|
|
|
|
|
|
|
if recipients_text:
|
|
|
|
recipients_text = "{}, and {}".format(recipients_text, text)
|
|
|
|
else:
|
|
|
|
recipients_text = text
|
|
|
|
|
|
|
|
return BODY.format(contract_title=contract_title,
|
|
|
|
actions=recipients_text).strip()
|
|
|
|
|
|
|
|
def get_recipients_text(recipients: List[str]) -> str:
|
|
|
|
recipients_text = ""
|
|
|
|
if len(recipients) == 1:
|
|
|
|
recipients_text = "{}".format(*recipients)
|
|
|
|
else:
|
|
|
|
for recipient in recipients[:-1]:
|
|
|
|
recipients_text += "{}, ".format(recipient)
|
|
|
|
recipients_text += "and {}".format(recipients[-1])
|
|
|
|
|
|
|
|
return recipients_text
|
2016-12-22 16:11:51 +01:00
|
|
|
|
|
|
|
@api_key_only_webhook_view('HelloSign')
|
|
|
|
@has_request_variables
|
2017-12-30 08:52:28 +01:00
|
|
|
def api_hellosign_webhook(request: HttpRequest, user_profile: UserProfile,
|
2018-03-16 22:53:50 +01:00
|
|
|
payload: Dict[str, Dict[str, Any]]=REQ(argument_type='body')) -> HttpResponse:
|
2018-10-04 17:23:27 +02:00
|
|
|
body = get_message_body(payload)
|
|
|
|
topic = payload['signature_request']['title']
|
2018-03-16 22:53:50 +01:00
|
|
|
check_send_webhook_message(request, user_profile, topic, body)
|
2016-12-22 16:11:51 +01:00
|
|
|
return json_success()
|