Skip to content

Commit

Permalink
adds Slack notification for when an OrganizationInvitation is created…
Browse files Browse the repository at this point in the history
… (initially sent)
  • Loading branch information
jontsai committed Oct 14, 2023
1 parent cf5c3fa commit 9a97157
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
38 changes: 37 additions & 1 deletion apps/organizations/apps.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,37 @@
# Third Party (PyPI) Imports
import rollbar

# Django Imports
from django.apps import AppConfig
from django.conf import settings
from django.db.models import signals

# HTK Imports
from htk.app_config import HtkAppConfig
from htk.decorators.classes import disable_for_loaddata
from htk.utils import (
htk_setting,
resolve_model_dynamically,
)


# isort: off


@disable_for_loaddata
def organization_invitation_created(sender, instance, created, **kwargs):
"""Signal handler for when a new OrganizationInvitation object is created"""
if created:
invitation = instance

if not settings.TEST and htk_setting('HTK_SLACK_NOTIFICATIONS_ENABLED'):
try:
from htk.utils.notifications import slack_notify

msg = invitation.build_notification_message__created()
slack_notify(msg)
except:
rollbar.report_exc_info()


class HtkOrganizationAppConfig(HtkAppConfig):
Expand All @@ -12,4 +40,12 @@ class HtkOrganizationAppConfig(HtkAppConfig):
verbose_name = 'Organizations'

def ready(self):
pass
OrganizationInvitation = resolve_model_dynamically(
htk_setting('HTK_ORGANIZATION_INVITATION_MODEL')
)

signals.post_save.connect(
organization_invitation_created,
sender=OrganizationInvitation,
dispatch_uid='htk_organization_invitation_created',
)
15 changes: 15 additions & 0 deletions apps/organizations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,21 @@ def status(self) -> str:

return status

##
# Notifications

def build_notification_message__created(self):
"""Builds a message that will be displayed as an internal Slack notification
when this invitation object is created.
"""
msg = '{invited_by_name} ({invited_by_username}) has sent an invitation for organization {organization_name} to {email}'.format(
invited_by_name=self.invited_by.profile.get_full_name(),
invited_by_username=self.invited_by.username,
organization_name=self.organization.name,
email=self.email,
)
return msg


class BaseAbstractOrganizationTeam(HtkBaseModel):
name = models.CharField(max_length=128)
Expand Down

0 comments on commit 9a97157

Please sign in to comment.