2024-02-08 17:23:15 -05:00
|
|
|
import typing
|
|
|
|
|
|
2023-01-05 16:16:43 +01:00
|
|
|
from emoji import emojize
|
|
|
|
|
|
2024-02-08 17:23:15 -05:00
|
|
|
from apps.alerts.incident_appearance.templaters.alert_templater import AlertTemplater, TemplatedAlert
|
|
|
|
|
from apps.alerts.models import AlertGroup
|
2022-11-23 15:56:43 +00:00
|
|
|
from common.utils import str_or_backup
|
|
|
|
|
|
|
|
|
|
|
2024-02-08 17:23:15 -05:00
|
|
|
def _validate_fcm_length_limit(value: typing.Optional[str]) -> str:
|
|
|
|
|
"""
|
|
|
|
|
NOTE: technically FCM limits the data we send based on total # of bytes, not characters for title/subtitle. For now
|
|
|
|
|
lets simply limit the title and subtitle to 200 characters and see how that goes with avoiding the `message is too big`
|
|
|
|
|
FCM exception
|
|
|
|
|
|
|
|
|
|
https://firebase.google.com/docs/reference/fcm/rest/v1/ErrorCode
|
|
|
|
|
"""
|
|
|
|
|
MAX_ALERT_TITLE_LENGTH = 200
|
|
|
|
|
|
|
|
|
|
if value is None:
|
|
|
|
|
return ""
|
|
|
|
|
return f"{value[:MAX_ALERT_TITLE_LENGTH]}..." if len(value) > MAX_ALERT_TITLE_LENGTH else value
|
|
|
|
|
|
|
|
|
|
|
2022-11-23 15:56:43 +00:00
|
|
|
class AlertMobileAppTemplater(AlertTemplater):
|
|
|
|
|
def _render_for(self):
|
2024-11-28 18:11:00 -03:00
|
|
|
return "mobile_app"
|
2022-11-23 15:56:43 +00:00
|
|
|
|
2024-02-08 17:23:15 -05:00
|
|
|
def _postformat(self, templated_alert: TemplatedAlert) -> TemplatedAlert:
|
|
|
|
|
templated_alert.title = _validate_fcm_length_limit(templated_alert.title)
|
|
|
|
|
templated_alert.message = _validate_fcm_length_limit(templated_alert.message)
|
|
|
|
|
return templated_alert
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _templatize_alert(alert_group: AlertGroup) -> TemplatedAlert:
|
|
|
|
|
alert = alert_group.alerts.first()
|
|
|
|
|
return AlertMobileAppTemplater(alert).render()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_push_notification_title(alert_group: AlertGroup, critical: bool) -> str:
|
|
|
|
|
return _templatize_alert(alert_group).title or ("New Important Alert" if critical else "New Alert")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_push_notification_subtitle(alert_group: AlertGroup) -> str:
|
|
|
|
|
templatized_subtitle = _templatize_alert(alert_group).message
|
|
|
|
|
if templatized_subtitle:
|
|
|
|
|
# only return the templatized subtitle if it resolves to something that is not None
|
|
|
|
|
# otherwise fallback to the default
|
|
|
|
|
return templatized_subtitle
|
2022-11-23 15:56:43 +00:00
|
|
|
|
|
|
|
|
alert = alert_group.alerts.first()
|
|
|
|
|
templated_alert = AlertMobileAppTemplater(alert).render()
|
2024-02-08 17:23:15 -05:00
|
|
|
|
|
|
|
|
alert_title = _validate_fcm_length_limit(str_or_backup(templated_alert.title, "Alert Group"))
|
2023-01-05 16:16:43 +01:00
|
|
|
|
2023-05-26 14:52:09 +02:00
|
|
|
status_verbose = "Firing" # TODO: we should probably de-duplicate this text
|
|
|
|
|
if alert_group.resolved:
|
|
|
|
|
status_verbose = alert_group.get_resolve_text()
|
|
|
|
|
elif alert_group.acknowledged:
|
|
|
|
|
status_verbose = alert_group.get_acknowledge_text()
|
|
|
|
|
|
|
|
|
|
number_of_alerts = alert_group.alerts.count()
|
|
|
|
|
if number_of_alerts <= 10:
|
|
|
|
|
alerts_count_str = str(number_of_alerts)
|
|
|
|
|
else:
|
|
|
|
|
alert_count_rounded = (number_of_alerts // 10) * 10
|
|
|
|
|
alerts_count_str = f"{alert_count_rounded}+"
|
|
|
|
|
|
|
|
|
|
alert_status = f"Status: {status_verbose}, alerts: {alerts_count_str}"
|
|
|
|
|
|
|
|
|
|
subtitle = (
|
|
|
|
|
f"#{alert_group.inside_organization_number} {alert_title}\n"
|
|
|
|
|
+ f"via {alert_group.channel.short_name}"
|
|
|
|
|
+ f"\n{alert_status}"
|
2023-01-05 16:16:43 +01:00
|
|
|
)
|
2023-05-26 14:52:09 +02:00
|
|
|
|
Fix warnings when running backend tests (#2079)
# What this PR does
- update `make test` to always use `settings.ci-test`. Right now it will
use whatever the value of `DJANGO_SETTINGS_MODULE` is in
`./dev/.env.dev`, which causes ~45 tests to fail
- Fix several Python warnings that we see when running the tests
```bash
RemovedInDjango40Warning: The providing_args argument is deprecated. As it is purely documentational, it has no replacement. If you rely on this argument as documentation, you can move the text to a code comment or docstring.
alert_create_signal = django.dispatch.Signal(
```
```bash
PytestCollectionWarning: cannot collect test class 'TestOnlyBackend' because it has a __init__ constructor (from: apps/api/tests/test_alert_receive_channel_template.py)
class TestOnlyBackend(BaseMessagingBackend):
```
```bash
DeprecationWarning: The parameter 'use_aliases' in emoji.emojize() is deprecated and will be removed in version 2.0.0. Use language='alias' instead.
To hide this warning, pin/downgrade the package to 'emoji~=1.6.3'
return emoji.emojize(self.verbal_name, use_aliases=True)
```
```bash
DateTimeField CustomOnCallShift.start received a naive datetime (2023-06-01 12:53:12) while time zone support is active.
warnings.warn("DateTimeField %s received a naive datetime (%s)"
```
```bash
apps/twilioapp/tests/test_phone_calls.py::test_resolve_by_phone
/etc/app/apps/twilioapp/tests/test_phone_calls.py:173: DeprecationWarning: The 'text' argument to find()-type methods is deprecated. Use 'string' instead.
content = BeautifulSoup(content, features="html.parser").findAll(text=True)
```
```bash
apps/twilioapp/tests/test_phone_calls.py::test_resolve_by_phone
apps/twilioapp/tests/test_phone_calls.py::test_wrong_pressed_digit
/usr/local/lib/python3.11/site-packages/bs4/builder/__init__.py:545: XMLParsedAsHTMLWarning: It looks like you're parsing an XML document using an HTML parser. If this really is an HTML document (maybe it's XHTML?), you can ignore or filter this warning. If it's XML, you should know that using an XML parser will be more reliable. To parse this document as XML, make sure you have the lxml package installed, and pass the keyword argument `features="xml"` into the BeautifulSoup constructor.
```
```bash
apps/twilioapp/tests/test_phone_calls.py::test_forbidden_requests
/usr/local/lib/python3.11/site-packages/social_django/urls.py:15: RemovedInDjango40Warning: django.conf.urls.url() is deprecated in favor of django.urls.re_path().
url(r'^login/(?P<backend>[^/]+){0}$'.format(extra), views.auth,
```
```bash
apps/twilioapp/tests/test_phone_calls.py: 66 warnings
/usr/local/lib/python3.11/site-packages/debug_toolbar/utils.py:255: DeprecationWarning: currentThread() is deprecated, use current_thread() instead
thread = threading.currentThread()
```
## Checklist
- [x] Unit, integration, and e2e (if applicable) tests updated
- [x] Documentation added (or `pr:no public docs` PR label added if not
required)
- [ ] `CHANGELOG.md` updated (or `pr:no changelog` PR label added if not
required)
2023-06-06 20:38:00 +02:00
|
|
|
return emojize(subtitle, language="alias")
|