MCPcopy Index your code
hub / github.com/Flagsmith/flagsmith / _send_api_usage_notification

Function _send_api_usage_notification

api/organisations/task_helpers.py:52–99  ·  view source on GitHub ↗

Send notification to users that the API has breached a threshold. Only admins are included if the matched threshold is under 100% of the API usage limits.

(
    organisation: Organisation, matched_threshold: int
)

Source from the content-addressed store, hash-verified

50
51
52def _send_api_usage_notification(
53 organisation: Organisation, matched_threshold: int
54) -> None:
55 """
56 Send notification to users that the API has breached a threshold.
57
58 Only admins are included if the matched threshold is under
59 100% of the API usage limits.
60 """
61
62 recipient_list = FFAdminUser.objects.filter(
63 userorganisation__organisation=organisation,
64 )
65
66 if matched_threshold < 100:
67 message = "organisations/api_usage_notification.txt"
68 html_message = "organisations/api_usage_notification.html"
69
70 # Since threshold < 100 only include admins.
71 recipient_list = recipient_list.filter(
72 userorganisation__role=OrganisationRole.ADMIN,
73 )
74 else:
75 message = "organisations/api_usage_notification_limit.txt"
76 html_message = "organisations/api_usage_notification_limit.html"
77
78 url = get_current_site_url()
79 context = {
80 "organisation": organisation,
81 "matched_threshold": matched_threshold,
82 "grace_period": not hasattr(organisation, "breached_grace_period"),
83 "usage_url": f"{url}/organisation/{organisation.id}/usage",
84 }
85
86 send_mail(
87 subject=f"Flagsmith API use has reached {matched_threshold}%",
88 message=render_to_string(message, context),
89 from_email=settings.DEFAULT_FROM_EMAIL,
90 recipient_list=list(recipient_list.values_list("email", flat=True)),
91 html_message=render_to_string(html_message, context),
92 fail_silently=True,
93 )
94
95 OrganisationAPIUsageNotification.objects.create(
96 organisation=organisation,
97 percent_usage=matched_threshold,
98 notified_at=timezone.now(),
99 )
100
101
102def handle_api_usage_notification_for_organisation(organisation: Organisation) -> None:

Calls 2

get_current_site_urlFunction · 0.90
createMethod · 0.45

Tested by

no test coverage detected