2017-11-16 00:43:10 +01:00
|
|
|
from typing import Any, Dict, List
|
|
|
|
|
2020-08-07 01:09:47 +02:00
|
|
|
import orjson
|
2017-11-16 00:43:10 +01:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
|
2020-08-20 00:32:15 +02:00
|
|
|
from zerver.decorator import webhook_view
|
2017-10-31 04:25:48 +01:00
|
|
|
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']
|
python: Convert assignment type annotations to Python 3.6 style.
This commit was split by tabbott; this piece covers the vast majority
of files in Zulip, but excludes scripts/, tools/, and puppet/ to help
ensure we at least show the right error messages for Xenial systems.
We can likely further refine the remaining pieces with some testing.
Generated by com2ann, with whitespace fixes and various manual fixes
for runtime issues:
- invoiced_through: Optional[LicenseLedger] = models.ForeignKey(
+ invoiced_through: Optional["LicenseLedger"] = models.ForeignKey(
-_apns_client: Optional[APNsClient] = None
+_apns_client: Optional["APNsClient"] = None
- notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- signup_notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ signup_notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- author: Optional[UserProfile] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
+ author: Optional["UserProfile"] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
- bot_owner: Optional[UserProfile] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
+ bot_owner: Optional["UserProfile"] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
- default_sending_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
- default_events_register_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_sending_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_events_register_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
-descriptors_by_handler_id: Dict[int, ClientDescriptor] = {}
+descriptors_by_handler_id: Dict[int, "ClientDescriptor"] = {}
-worker_classes: Dict[str, Type[QueueProcessingWorker]] = {}
-queues: Dict[str, Dict[str, Type[QueueProcessingWorker]]] = {}
+worker_classes: Dict[str, Type["QueueProcessingWorker"]] = {}
+queues: Dict[str, Dict[str, Type["QueueProcessingWorker"]]] = {}
-AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional[LDAPSearch] = None
+AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional["LDAPSearch"] = None
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-22 01:09:50 +02:00
|
|
|
recipients: Dict[str, List[str]] = {}
|
2018-10-04 17:23:27 +02:00
|
|
|
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(
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
awaiting_recipients=get_recipients_text(recipients['awaiting_signature']),
|
2018-10-04 17:23:27 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
if recipients.get('signed'):
|
|
|
|
text = WAS_JUST_SIGNED_BY.format(
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02: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
|
|
|
|
|
|
|
|
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]:
|
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
|
|
|
|
2020-08-20 00:32:15 +02:00
|
|
|
@webhook_view('HelloSign')
|
2016-12-22 16:11:51 +01:00
|
|
|
@has_request_variables
|
2017-12-30 08:52:28 +01:00
|
|
|
def api_hellosign_webhook(request: HttpRequest, user_profile: UserProfile,
|
2020-02-08 00:24:39 +01:00
|
|
|
payload: Dict[str, Dict[str, Any]]=REQ(
|
2020-08-07 01:09:47 +02:00
|
|
|
whence='json', converter=orjson.loads)) -> HttpResponse:
|
2020-02-08 00:24:39 +01:00
|
|
|
if "signature_request" in payload:
|
|
|
|
body = get_message_body(payload)
|
|
|
|
topic = payload['signature_request']['title']
|
|
|
|
check_send_webhook_message(request, user_profile, topic, body)
|
|
|
|
|
|
|
|
return json_success({"msg": "Hello API Event Received"})
|