2017-11-16 00:43:10 +01:00
|
|
|
from typing import Any, Dict, List
|
|
|
|
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
2016-12-22 16:11:51 +01:00
|
|
|
from django.utils.translation import ugettext as _
|
2017-11-16 00:43:10 +01:00
|
|
|
|
2017-10-31 04:25:48 +01:00
|
|
|
from zerver.decorator import api_key_only_webhook_view
|
2017-09-30 04:18:16 +02:00
|
|
|
from zerver.lib.actions import check_send_stream_message
|
2017-10-31 04:25:48 +01:00
|
|
|
from zerver.lib.request import REQ, has_request_variables
|
2017-11-16 00:43:10 +01:00
|
|
|
from zerver.lib.response import json_error, json_success
|
2017-05-02 01:00:50 +02:00
|
|
|
from zerver.models import UserProfile
|
2016-12-22 16:11:51 +01:00
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def format_body(signatories: List[Dict[str, Any]], model_payload: Dict[str, Any]) -> str:
|
|
|
|
def append_separator(i: int) -> None:
|
2016-12-22 16:11:51 +01:00
|
|
|
if i + 1 == len(signatories):
|
|
|
|
result.append('.')
|
|
|
|
elif i + 2 == len(signatories):
|
|
|
|
result.append(' and')
|
|
|
|
elif i + 3 != len(signatories):
|
|
|
|
result.append(',')
|
|
|
|
|
2017-06-04 11:38:06 +02:00
|
|
|
result = ["The {}".format(model_payload['contract_title'])] # type: Any
|
2016-12-22 16:11:51 +01:00
|
|
|
for i, signatory in enumerate(signatories):
|
|
|
|
name = model_payload['name_{}'.format(i)]
|
|
|
|
if signatory['status_code'] == 'awaiting_signature':
|
|
|
|
result.append(" is awaiting the signature of {}".format(name))
|
|
|
|
elif signatory['status_code'] in ['signed', 'declined']:
|
|
|
|
status = model_payload['status_{}'.format(i)]
|
|
|
|
result.append(" was just {} by {}".format(status, name))
|
|
|
|
|
2017-04-11 07:19:35 +02:00
|
|
|
append_separator(i)
|
|
|
|
return ''.join(result)
|
2016-12-22 16:11:51 +01:00
|
|
|
|
2017-11-04 07:47:46 +01:00
|
|
|
def ready_payload(signatories: List[Dict[str, Any]],
|
|
|
|
payload: Dict[str, Dict[str, Any]]) -> Dict[str, Any]:
|
2016-12-22 16:11:51 +01:00
|
|
|
model_payload = {'contract_title': payload['signature_request']['title']}
|
|
|
|
for i, signatory in enumerate(signatories):
|
|
|
|
model_payload['name_{}'.format(i)] = signatory['signer_name']
|
|
|
|
model_payload['status_{}'.format(i)] = signatory['status_code']
|
|
|
|
return model_payload
|
|
|
|
|
|
|
|
@api_key_only_webhook_view('HelloSign')
|
|
|
|
@has_request_variables
|
2017-05-02 01:00:50 +02:00
|
|
|
def api_hellosign_webhook(request, user_profile,
|
2016-12-22 16:11:51 +01:00
|
|
|
payload=REQ(argument_type='body'),
|
|
|
|
stream=REQ(default='hellosign'),
|
|
|
|
topic=REQ(default=None)):
|
2017-11-09 08:59:34 +01:00
|
|
|
# type: (HttpRequest, UserProfile, Dict[str, Dict[str, Any]], str, str) -> HttpResponse
|
2017-08-24 17:31:04 +02:00
|
|
|
model_payload = ready_payload(payload['signature_request']['signatures'], payload)
|
2016-12-22 16:11:51 +01:00
|
|
|
body = format_body(payload['signature_request']['signatures'], model_payload)
|
2017-04-11 07:19:35 +02:00
|
|
|
topic = topic or model_payload['contract_title']
|
2017-09-30 04:18:16 +02:00
|
|
|
check_send_stream_message(user_profile, request.client, stream, topic, body)
|
2016-12-22 16:11:51 +01:00
|
|
|
return json_success()
|