2016-12-03 18:51:33 +01:00
|
|
|
# Webhooks for external integrations.
|
2017-11-16 00:43:10 +01:00
|
|
|
import time
|
2022-06-23 16:19:20 +02:00
|
|
|
from typing import Dict, Optional, Sequence, Tuple
|
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
|
2020-08-19 22:26:38 +02:00
|
|
|
from zerver.lib.exceptions import UnsupportedWebhookEventType
|
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-12-06 18:40:43 +01:00
|
|
|
from zerver.lib.timestamp import timestamp_to_datetime
|
2022-06-23 16:19:20 +02:00
|
|
|
from zerver.lib.validator import (
|
|
|
|
WildValue,
|
|
|
|
check_bool,
|
|
|
|
check_int,
|
|
|
|
check_none_or,
|
|
|
|
check_string,
|
|
|
|
to_wild_value,
|
|
|
|
)
|
2020-08-19 22:14:40 +02: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-03 18:51:33 +01:00
|
|
|
|
2020-01-14 22:06:24 +01:00
|
|
|
|
2018-12-15 23:14:09 +01:00
|
|
|
class SuppressedEvent(Exception):
|
2018-12-06 19:17:51 +01:00
|
|
|
pass
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-12-15 23:14:09 +01:00
|
|
|
class NotImplementedEventType(SuppressedEvent):
|
2018-12-06 09:06:23 +01:00
|
|
|
pass
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-07-16 11:40:46 +02:00
|
|
|
ALL_EVENT_TYPES = [
|
|
|
|
"charge.dispute.closed",
|
|
|
|
"charge.dispute.created",
|
|
|
|
"charge.failed",
|
|
|
|
"charge.succeeded",
|
|
|
|
"charge.succeeded",
|
|
|
|
"customer.created",
|
|
|
|
"customer.created",
|
|
|
|
"customer.deleted",
|
|
|
|
"customer.discount.created",
|
|
|
|
"customer.subscription.created",
|
|
|
|
"customer.subscription.deleted",
|
|
|
|
"customer.subscription.trial_will_end",
|
|
|
|
"customer.subscription.updated",
|
|
|
|
"customer.updated",
|
|
|
|
"invoice.created",
|
|
|
|
"invoice.updated",
|
|
|
|
"invoice.payment_failed",
|
|
|
|
"invoiceitem.created",
|
|
|
|
"charge.refund.updated",
|
|
|
|
"charge.refund.updated",
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
@webhook_view("Stripe", all_event_types=ALL_EVENT_TYPES)
|
2016-12-03 18:51:33 +01:00
|
|
|
@has_request_variables
|
2021-02-12 08:19:30 +01:00
|
|
|
def api_stripe_webhook(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2022-06-23 16:19:20 +02:00
|
|
|
payload: WildValue = REQ(argument_type="body", converter=to_wild_value),
|
2021-02-12 08:20:45 +01:00
|
|
|
stream: str = REQ(default="test"),
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> HttpResponse:
|
2018-12-06 19:17:51 +01:00
|
|
|
try:
|
2018-12-13 19:22:19 +01:00
|
|
|
topic, body = topic_and_body(payload)
|
2018-12-06 09:06:23 +01:00
|
|
|
except SuppressedEvent: # nocoverage
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|
2022-06-23 16:19:20 +02:00
|
|
|
check_send_webhook_message(
|
|
|
|
request, user_profile, topic, body, payload["type"].tame(check_string)
|
|
|
|
)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|
2018-12-06 19:17:51 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-06-23 16:19:20 +02:00
|
|
|
def topic_and_body(payload: WildValue) -> Tuple[str, str]:
|
|
|
|
event_type = payload["type"].tame(
|
|
|
|
check_string
|
|
|
|
) # invoice.created, customer.subscription.created, etc
|
2021-02-12 08:20:45 +01:00
|
|
|
if len(event_type.split(".")) == 3:
|
|
|
|
category, resource, event = event_type.split(".")
|
2018-11-21 01:39:37 +01:00
|
|
|
else:
|
2021-02-12 08:20:45 +01:00
|
|
|
resource, event = event_type.split(".")
|
2018-11-21 01:39:37 +01:00
|
|
|
category = resource
|
|
|
|
|
|
|
|
object_ = payload["data"]["object"] # The full, updated Stripe object
|
|
|
|
|
|
|
|
# Set the topic to the customer_id when we can
|
2021-02-12 08:20:45 +01:00
|
|
|
topic = ""
|
2022-06-23 16:19:20 +02:00
|
|
|
customer_id = object_.get("customer").tame(check_none_or(check_string))
|
2018-11-21 01:39:37 +01:00
|
|
|
if customer_id is not None:
|
|
|
|
# Running into the 60 character topic limit.
|
|
|
|
# topic = '[{}](https://dashboard.stripe.com/customers/{})' % (customer_id, customer_id)
|
|
|
|
topic = customer_id
|
2016-12-23 17:21:51 +01:00
|
|
|
body = None
|
2016-12-23 17:00:32 +01:00
|
|
|
|
2020-06-13 05:24:42 +02:00
|
|
|
def update_string(blacklist: Sequence[str] = []) -> str:
|
2021-02-12 08:20:45 +01:00
|
|
|
assert "previous_attributes" in payload["data"]
|
2022-09-09 03:20:09 +02:00
|
|
|
previous_attributes = set(payload["data"]["previous_attributes"].keys()).difference(
|
|
|
|
blacklist
|
|
|
|
)
|
2018-12-06 09:06:23 +01:00
|
|
|
if not previous_attributes: # nocoverage
|
|
|
|
raise SuppressedEvent()
|
2021-02-12 08:20:45 +01:00
|
|
|
return "".join(
|
|
|
|
"\n* "
|
|
|
|
+ attribute.replace("_", " ").capitalize()
|
|
|
|
+ " is now "
|
2022-09-09 03:20:09 +02:00
|
|
|
+ stringify(object_[attribute].value)
|
|
|
|
for attribute in sorted(previous_attributes)
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2018-12-06 09:06:23 +01:00
|
|
|
|
2020-06-13 05:24:42 +02:00
|
|
|
def default_body(update_blacklist: Sequence[str] = []) -> str:
|
2021-02-12 08:20:45 +01:00
|
|
|
body = "{resource} {verbed}".format(
|
2022-06-23 16:19:20 +02:00
|
|
|
resource=linkified_id(object_["id"].tame(check_string)), verbed=event.replace("_", " ")
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2021-02-12 08:20:45 +01:00
|
|
|
if event == "updated":
|
2018-12-06 09:06:23 +01:00
|
|
|
return body + update_string(blacklist=update_blacklist)
|
|
|
|
return body
|
2018-11-21 01:39:37 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
if category == "account": # nocoverage
|
|
|
|
if resource == "account":
|
|
|
|
if event == "updated":
|
|
|
|
if "previous_attributes" not in payload["data"]:
|
2019-04-04 22:54:07 +02:00
|
|
|
raise SuppressedEvent()
|
2018-12-06 09:06:23 +01:00
|
|
|
topic = "account updates"
|
|
|
|
body = update_string()
|
2017-08-24 17:31:04 +02:00
|
|
|
else:
|
2018-11-21 01:39:37 +01:00
|
|
|
# Part of Stripe Connect
|
2018-12-06 19:17:51 +01:00
|
|
|
raise NotImplementedEventType()
|
2021-02-12 08:20:45 +01:00
|
|
|
if category == "application_fee": # nocoverage
|
2018-11-21 01:39:37 +01:00
|
|
|
# Part of Stripe Connect
|
2018-12-06 19:17:51 +01:00
|
|
|
raise NotImplementedEventType()
|
2021-02-12 08:20:45 +01:00
|
|
|
if category == "balance": # nocoverage
|
2018-11-21 01:39:37 +01:00
|
|
|
# Not that interesting to most businesses, I think
|
2018-12-06 19:17:51 +01:00
|
|
|
raise NotImplementedEventType()
|
2021-02-12 08:20:45 +01:00
|
|
|
if category == "charge":
|
|
|
|
if resource == "charge":
|
2018-12-02 09:21:09 +01:00
|
|
|
if not topic: # only in legacy fixtures
|
2021-02-12 08:20:45 +01:00
|
|
|
topic = "charges"
|
2018-11-21 01:39:37 +01:00
|
|
|
body = "{resource} for {amount} {verbed}".format(
|
2022-06-23 16:19:20 +02:00
|
|
|
resource=linkified_id(object_["id"].tame(check_string)),
|
|
|
|
amount=amount_string(
|
|
|
|
object_["amount"].tame(check_int), object_["currency"].tame(check_string)
|
|
|
|
),
|
2021-02-12 08:19:30 +01:00
|
|
|
verbed=event,
|
|
|
|
)
|
2021-02-12 08:20:45 +01:00
|
|
|
if object_["failure_code"]: # nocoverage
|
2022-06-23 16:19:20 +02:00
|
|
|
body += ". Failure code: {}".format(object_["failure_code"].tame(check_string))
|
2021-02-12 08:20:45 +01:00
|
|
|
if resource == "dispute":
|
|
|
|
topic = "disputes"
|
|
|
|
body = default_body() + ". Current status: {status}.".format(
|
2022-06-23 16:19:20 +02:00
|
|
|
status=object_["status"].tame(check_string).replace("_", " ")
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2021-02-12 08:20:45 +01:00
|
|
|
if resource == "refund":
|
|
|
|
topic = "refunds"
|
|
|
|
body = "A {resource} for a {charge} of {amount} was updated.".format(
|
2022-06-23 16:19:20 +02:00
|
|
|
resource=linkified_id(object_["id"].tame(check_string), lower=True),
|
|
|
|
charge=linkified_id(object_["charge"].tame(check_string), lower=True),
|
|
|
|
amount=amount_string(
|
|
|
|
object_["amount"].tame(check_int), object_["currency"].tame(check_string)
|
|
|
|
),
|
2020-05-14 09:42:22 +02:00
|
|
|
)
|
2021-02-12 08:20:45 +01:00
|
|
|
if category == "checkout_beta": # nocoverage
|
2018-11-21 01:39:37 +01:00
|
|
|
# Not sure what this is
|
2018-12-06 19:17:51 +01:00
|
|
|
raise NotImplementedEventType()
|
2021-02-12 08:20:45 +01:00
|
|
|
if category == "coupon": # nocoverage
|
2018-11-21 01:39:37 +01:00
|
|
|
# Not something that likely happens programmatically
|
2018-12-06 19:17:51 +01:00
|
|
|
raise NotImplementedEventType()
|
2021-02-12 08:20:45 +01:00
|
|
|
if category == "customer":
|
|
|
|
if resource == "customer":
|
2018-11-21 01:39:37 +01:00
|
|
|
# Running into the 60 character topic limit.
|
|
|
|
# topic = '[{}](https://dashboard.stripe.com/customers/{})' % (object_['id'], object_['id'])
|
2022-06-23 16:19:20 +02:00
|
|
|
topic = object_["id"].tame(check_string)
|
2021-02-12 08:20:45 +01:00
|
|
|
body = default_body(update_blacklist=["delinquent", "currency", "default_source"])
|
|
|
|
if event == "created":
|
|
|
|
if object_["email"]:
|
2022-06-23 16:19:20 +02:00
|
|
|
body += "\nEmail: {}".format(object_["email"].tame(check_string))
|
2021-02-12 08:20:45 +01:00
|
|
|
if object_["metadata"]: # nocoverage
|
|
|
|
for key, value in object_["metadata"].items():
|
2022-09-09 03:20:09 +02:00
|
|
|
body += f"\n{key}: {value.tame(check_string)}"
|
2021-02-12 08:20:45 +01:00
|
|
|
if resource == "discount":
|
|
|
|
body = "Discount {verbed} ([{coupon_name}]({coupon_url})).".format(
|
|
|
|
verbed=event.replace("_", " "),
|
2022-06-23 16:19:20 +02:00
|
|
|
coupon_name=object_["coupon"]["name"].tame(check_string),
|
2021-02-12 08:20:45 +01:00
|
|
|
coupon_url="https://dashboard.stripe.com/{}/{}".format(
|
2022-06-23 16:19:20 +02:00
|
|
|
"coupons", object_["coupon"]["id"].tame(check_string)
|
2021-02-12 08:19:30 +01:00
|
|
|
),
|
2018-11-28 22:11:08 +01:00
|
|
|
)
|
2021-02-12 08:20:45 +01:00
|
|
|
if resource == "source": # nocoverage
|
2018-11-21 01:39:37 +01:00
|
|
|
body = default_body()
|
2021-02-12 08:20:45 +01:00
|
|
|
if resource == "subscription":
|
2018-11-21 01:39:37 +01:00
|
|
|
body = default_body()
|
2021-02-12 08:20:45 +01:00
|
|
|
if event == "trial_will_end":
|
2017-08-24 17:31:04 +02:00
|
|
|
DAY = 60 * 60 * 24 # seconds in a day
|
2018-11-21 01:39:37 +01:00
|
|
|
# Basically always three: https://stripe.com/docs/api/python#event_types
|
2021-02-12 08:20:45 +01:00
|
|
|
body += " in {days} days".format(
|
2022-06-23 16:19:20 +02:00
|
|
|
days=int((object_["trial_end"].tame(check_int) - time.time() + DAY // 2) // DAY)
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2021-02-12 08:20:45 +01:00
|
|
|
if event == "created":
|
|
|
|
if object_["plan"]:
|
|
|
|
body += "\nPlan: [{plan_nickname}](https://dashboard.stripe.com/plans/{plan_id})".format(
|
2022-06-23 16:19:20 +02:00
|
|
|
plan_nickname=object_["plan"]["nickname"].tame(check_string),
|
|
|
|
plan_id=object_["plan"]["id"].tame(check_string),
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2021-02-12 08:20:45 +01:00
|
|
|
if object_["quantity"]:
|
2022-06-23 16:19:20 +02:00
|
|
|
body += "\nQuantity: {}".format(object_["quantity"].tame(check_int))
|
2021-02-12 08:20:45 +01:00
|
|
|
if "billing" in object_: # nocoverage
|
2022-06-23 16:19:20 +02:00
|
|
|
body += "\nBilling method: {}".format(
|
|
|
|
object_["billing"].tame(check_string).replace("_", " ")
|
|
|
|
)
|
2021-02-12 08:20:45 +01:00
|
|
|
if category == "file": # nocoverage
|
|
|
|
topic = "files"
|
|
|
|
body = default_body() + " ({purpose}). \nTitle: {title}".format(
|
2022-06-23 16:19:20 +02:00
|
|
|
purpose=object_["purpose"].tame(check_string).replace("_", " "),
|
|
|
|
title=object_["title"].tame(check_string),
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2021-02-12 08:20:45 +01:00
|
|
|
if category == "invoice":
|
|
|
|
if event == "upcoming": # nocoverage
|
|
|
|
body = "Upcoming invoice created"
|
2021-02-12 08:19:30 +01:00
|
|
|
elif (
|
2021-02-12 08:20:45 +01:00
|
|
|
event == "updated"
|
2022-06-23 16:19:20 +02:00
|
|
|
and payload["data"]["previous_attributes"].get("paid").tame(check_none_or(check_bool))
|
|
|
|
is False
|
|
|
|
and object_["paid"].tame(check_bool) is True
|
|
|
|
and object_["amount_paid"].tame(check_int) != 0
|
|
|
|
and object_["amount_remaining"].tame(check_int) == 0
|
2021-02-12 08:19:30 +01:00
|
|
|
):
|
2019-06-09 13:43:58 +02:00
|
|
|
# We are taking advantage of logical AND short circuiting here since we need the else
|
|
|
|
# statement below.
|
2022-06-23 16:19:20 +02:00
|
|
|
object_id = object_["id"].tame(check_string)
|
2021-02-12 08:20:45 +01:00
|
|
|
invoice_link = f"https://dashboard.stripe.com/invoices/{object_id}"
|
|
|
|
body = f"[Invoice]({invoice_link}) is now paid"
|
2019-02-04 21:18:47 +01:00
|
|
|
else:
|
2021-02-12 08:19:30 +01:00
|
|
|
body = default_body(
|
|
|
|
update_blacklist=[
|
2021-02-12 08:20:45 +01:00
|
|
|
"lines",
|
|
|
|
"description",
|
|
|
|
"number",
|
|
|
|
"finalized_at",
|
|
|
|
"status_transitions",
|
|
|
|
"payment_intent",
|
2021-02-12 08:19:30 +01:00
|
|
|
]
|
|
|
|
)
|
2021-02-12 08:20:45 +01:00
|
|
|
if event == "created":
|
2019-02-04 21:18:47 +01:00
|
|
|
# Could potentially add link to invoice PDF here
|
2021-02-12 08:20:45 +01:00
|
|
|
body += " ({reason})\nTotal: {total}\nAmount due: {due}".format(
|
2022-06-23 16:19:20 +02:00
|
|
|
reason=object_["billing_reason"].tame(check_string).replace("_", " "),
|
|
|
|
total=amount_string(
|
|
|
|
object_["total"].tame(check_int), object_["currency"].tame(check_string)
|
|
|
|
),
|
|
|
|
due=amount_string(
|
|
|
|
object_["amount_due"].tame(check_int), object_["currency"].tame(check_string)
|
|
|
|
),
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2021-02-12 08:20:45 +01:00
|
|
|
if category == "invoiceitem":
|
|
|
|
body = default_body(update_blacklist=["description", "invoice"])
|
|
|
|
if event == "created":
|
|
|
|
body += " for {amount}".format(
|
2022-06-23 16:19:20 +02:00
|
|
|
amount=amount_string(
|
|
|
|
object_["amount"].tame(check_int), object_["currency"].tame(check_string)
|
|
|
|
)
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2021-02-12 08:20:45 +01:00
|
|
|
if category.startswith("issuing"): # nocoverage
|
2018-11-21 01:39:37 +01:00
|
|
|
# Not implemented
|
2018-12-06 19:17:51 +01:00
|
|
|
raise NotImplementedEventType()
|
2021-02-12 08:20:45 +01:00
|
|
|
if category.startswith("order"): # nocoverage
|
2018-11-21 01:39:37 +01:00
|
|
|
# Not implemented
|
2018-12-06 19:17:51 +01:00
|
|
|
raise NotImplementedEventType()
|
2021-02-12 08:19:30 +01:00
|
|
|
if category in [
|
2021-02-12 08:20:45 +01:00
|
|
|
"payment_intent",
|
|
|
|
"payout",
|
|
|
|
"plan",
|
|
|
|
"product",
|
|
|
|
"recipient",
|
|
|
|
"reporting",
|
|
|
|
"review",
|
|
|
|
"sigma",
|
|
|
|
"sku",
|
|
|
|
"source",
|
|
|
|
"subscription_schedule",
|
|
|
|
"topup",
|
|
|
|
"transfer",
|
2021-02-12 08:19:30 +01:00
|
|
|
]: # nocoverage
|
2018-11-21 01:39:37 +01:00
|
|
|
# Not implemented. In theory doing something like
|
|
|
|
# body = default_body()
|
|
|
|
# may not be hard for some of these
|
2018-12-06 19:17:51 +01:00
|
|
|
raise NotImplementedEventType()
|
2016-12-23 17:21:51 +01:00
|
|
|
|
|
|
|
if body is None:
|
2020-08-20 00:50:06 +02:00
|
|
|
raise UnsupportedWebhookEventType(event_type)
|
2018-12-06 19:17:51 +01:00
|
|
|
return (topic, body)
|
2016-12-03 18:51:33 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-11-21 01:39:37 +01:00
|
|
|
def amount_string(amount: int, currency: str) -> str:
|
2021-02-12 08:19:30 +01:00
|
|
|
zero_decimal_currencies = [
|
|
|
|
"bif",
|
|
|
|
"djf",
|
|
|
|
"jpy",
|
|
|
|
"krw",
|
|
|
|
"pyg",
|
|
|
|
"vnd",
|
|
|
|
"xaf",
|
|
|
|
"xpf",
|
|
|
|
"clp",
|
|
|
|
"gnf",
|
|
|
|
"kmf",
|
|
|
|
"mga",
|
|
|
|
"rwf",
|
|
|
|
"vuv",
|
|
|
|
"xof",
|
|
|
|
]
|
2016-12-03 18:51:33 +01:00
|
|
|
if currency in zero_decimal_currencies:
|
2018-11-21 01:39:37 +01:00
|
|
|
decimal_amount = str(amount) # nocoverage
|
2016-12-03 18:51:33 +01:00
|
|
|
else:
|
2021-02-12 08:20:45 +01:00
|
|
|
decimal_amount = f"{float(amount) * 0.01:.02f}"
|
2018-11-21 01:39:37 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
if currency == "usd": # nocoverage
|
|
|
|
return "$" + decimal_amount
|
|
|
|
return decimal_amount + f" {currency.upper()}"
|
2018-11-21 01:39:37 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
|
|
|
def linkified_id(object_id: str, lower: bool = False) -> str:
|
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
|
|
|
names_and_urls: Dict[str, Tuple[str, Optional[str]]] = {
|
2018-11-21 01:39:37 +01:00
|
|
|
# Core resources
|
2021-02-12 08:20:45 +01:00
|
|
|
"ch": ("Charge", "charges"),
|
|
|
|
"cus": ("Customer", "customers"),
|
|
|
|
"dp": ("Dispute", "disputes"),
|
|
|
|
"du": ("Dispute", "disputes"),
|
|
|
|
"file": ("File", "files"),
|
|
|
|
"link": ("File link", "file_links"),
|
|
|
|
"pi": ("Payment intent", "payment_intents"),
|
|
|
|
"po": ("Payout", "payouts"),
|
|
|
|
"prod": ("Product", "products"),
|
|
|
|
"re": ("Refund", "refunds"),
|
|
|
|
"tok": ("Token", "tokens"),
|
2018-11-21 01:39:37 +01:00
|
|
|
# Payment methods
|
|
|
|
# payment methods have URL prefixes like /customers/cus_id/sources
|
2021-02-12 08:20:45 +01:00
|
|
|
"ba": ("Bank account", None),
|
|
|
|
"card": ("Card", None),
|
|
|
|
"src": ("Source", None),
|
2018-11-21 01:39:37 +01:00
|
|
|
# Billing
|
|
|
|
# coupons have a configurable id, but the URL prefix is /coupons
|
|
|
|
# discounts don't have a URL, I think
|
2021-02-12 08:20:45 +01:00
|
|
|
"in": ("Invoice", "invoices"),
|
|
|
|
"ii": ("Invoice item", "invoiceitems"),
|
2018-11-21 01:39:37 +01:00
|
|
|
# products are covered in core resources
|
|
|
|
# plans have a configurable id, though by default they are created with this pattern
|
|
|
|
# 'plan': ('Plan', 'plans'),
|
2021-02-12 08:20:45 +01:00
|
|
|
"sub": ("Subscription", "subscriptions"),
|
|
|
|
"si": ("Subscription item", "subscription_items"),
|
2018-11-21 01:39:37 +01:00
|
|
|
# I think usage records have URL prefixes like /subscription_items/si_id/usage_record_summaries
|
2021-02-12 08:20:45 +01:00
|
|
|
"mbur": ("Usage record", None),
|
2018-12-02 09:21:09 +01:00
|
|
|
# Undocumented :|
|
2021-02-12 08:20:45 +01:00
|
|
|
"py": ("Payment", "payments"),
|
|
|
|
"pyr": ("Refund", "refunds"), # Pseudo refunds. Not fully tested.
|
2018-11-21 01:39:37 +01:00
|
|
|
# Connect, Fraud, Orders, etc not implemented
|
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
|
|
|
}
|
2021-02-12 08:20:45 +01:00
|
|
|
name, url_prefix = names_and_urls[object_id.split("_")[0]]
|
2018-11-21 01:39:37 +01:00
|
|
|
if lower: # nocoverage
|
|
|
|
name = name.lower()
|
|
|
|
if url_prefix is None: # nocoverage
|
|
|
|
return name
|
2021-02-12 08:20:45 +01:00
|
|
|
return f"[{name}](https://dashboard.stripe.com/{url_prefix}/{object_id})"
|
2018-12-06 18:40:43 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-06-23 16:19:20 +02:00
|
|
|
def stringify(value: object) -> str:
|
2018-12-06 18:40:43 +01:00
|
|
|
if isinstance(value, int) and value > 1500000000 and value < 2000000000:
|
2021-02-12 08:20:45 +01:00
|
|
|
return timestamp_to_datetime(value).strftime("%b %d, %Y, %H:%M:%S %Z")
|
2018-12-06 18:40:43 +01:00
|
|
|
return str(value)
|