2024-07-12 02:30:25 +02:00
|
|
|
from typing import Annotated
|
|
|
|
|
2017-11-16 00:43:10 +01:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
2023-08-14 23:48:25 +02:00
|
|
|
from pydantic import Json
|
2017-11-16 00:43:10 +01:00
|
|
|
|
2020-08-20 00:32:15 +02:00
|
|
|
from zerver.decorator import webhook_view
|
2019-02-02 23:53:55 +01:00
|
|
|
from zerver.lib.response import json_success
|
2023-08-14 23:48:25 +02:00
|
|
|
from zerver.lib.typed_endpoint import ApiParamConfig, typed_endpoint
|
|
|
|
from zerver.lib.validator import WildValue, check_string
|
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}."
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-10-08 19:33:40 +02:00
|
|
|
def get_message_body(payload: WildValue) -> str:
|
|
|
|
contract_title = payload["signature_request"]["title"].tame(check_string)
|
2024-07-12 02:30:17 +02:00
|
|
|
recipients: dict[str, list[str]] = {}
|
2021-02-12 08:20:45 +01:00
|
|
|
signatures = payload["signature_request"]["signatures"]
|
2018-10-04 17:23:27 +02:00
|
|
|
|
|
|
|
for signature in signatures:
|
2022-10-08 19:33:40 +02:00
|
|
|
status_code = signature["status_code"].tame(check_string)
|
|
|
|
recipients.setdefault(status_code, [])
|
|
|
|
recipients[status_code].append(signature["signer_name"].tame(check_string))
|
2018-10-04 17:23:27 +02:00
|
|
|
|
|
|
|
recipients_text = ""
|
2021-02-12 08:20:45 +01:00
|
|
|
if recipients.get("awaiting_signature"):
|
2018-10-04 17:23:27 +02:00
|
|
|
recipients_text += IS_AWAITING_SIGNATURE.format(
|
2021-02-12 08:20:45 +01:00
|
|
|
awaiting_recipients=get_recipients_text(recipients["awaiting_signature"]),
|
2018-10-04 17:23:27 +02:00
|
|
|
)
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
if recipients.get("signed"):
|
2018-10-04 17:23:27 +02:00
|
|
|
text = WAS_JUST_SIGNED_BY.format(
|
2021-02-12 08:20:45 +01:00
|
|
|
signed_recipients=get_recipients_text(recipients["signed"]),
|
2018-10-04 17:23:27 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
if recipients_text:
|
2020-06-09 00:25:09 +02:00
|
|
|
recipients_text = f"{recipients_text}, and {text}"
|
2018-10-04 17:23:27 +02:00
|
|
|
else:
|
|
|
|
recipients_text = text
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
return BODY.format(contract_title=contract_title, actions=recipients_text).strip()
|
|
|
|
|
2018-10-04 17:23:27 +02:00
|
|
|
|
2024-07-12 02:30:17 +02:00
|
|
|
def get_recipients_text(recipients: list[str]) -> str:
|
2018-10-04 17:23:27 +02:00
|
|
|
recipients_text = ""
|
|
|
|
if len(recipients) == 1:
|
|
|
|
recipients_text = "{}".format(*recipients)
|
|
|
|
else:
|
|
|
|
for recipient in recipients[:-1]:
|
2020-06-09 00:25:09 +02:00
|
|
|
recipients_text += f"{recipient}, "
|
|
|
|
recipients_text += f"and {recipients[-1]}"
|
2018-10-04 17:23:27 +02:00
|
|
|
|
|
|
|
return recipients_text
|
2016-12-22 16:11:51 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
@webhook_view("HelloSign")
|
2023-08-14 23:48:25 +02:00
|
|
|
@typed_endpoint
|
2021-02-12 08:19:30 +01:00
|
|
|
def api_hellosign_webhook(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2023-08-14 23:48:25 +02:00
|
|
|
*,
|
|
|
|
payload: Annotated[Json[WildValue], ApiParamConfig("json")],
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> HttpResponse:
|
2020-02-08 00:24:39 +01:00
|
|
|
if "signature_request" in payload:
|
|
|
|
body = get_message_body(payload)
|
2024-01-17 15:53:30 +01:00
|
|
|
topic_name = payload["signature_request"]["title"].tame(check_string)
|
|
|
|
check_send_webhook_message(request, user_profile, topic_name, body)
|
2020-02-08 00:24:39 +01:00
|
|
|
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request, data={"msg": "Hello API Event Received"})
|