mirror of
https://github.com/inventree/InvenTree.git
synced 2025-07-11 15:34:15 +00:00
[FR] Slack notifications & notification method updates (#4114)
* [FR] Slack notification Add slack sending method Fixes #3843 * Add global panel with notification methods * add plugin information * fix plugin lookup and link * Add settings content mixin * Add instructions for Slack setup * fix rendering of custom settings content
This commit is contained in:
@ -3,13 +3,15 @@
|
||||
from django.template.loader import render_to_string
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
import requests
|
||||
from allauth.account.models import EmailAddress
|
||||
|
||||
import common.models
|
||||
import InvenTree.helpers
|
||||
import InvenTree.tasks
|
||||
from plugin import InvenTreePlugin
|
||||
from plugin.mixins import BulkNotificationMethod, SettingsMixin
|
||||
from plugin import InvenTreePlugin, registry
|
||||
from plugin.mixins import (BulkNotificationMethod, SettingsContentMixin,
|
||||
SettingsMixin)
|
||||
|
||||
|
||||
class PlgMixin:
|
||||
@ -23,7 +25,7 @@ class PlgMixin:
|
||||
return CoreNotificationsPlugin
|
||||
|
||||
|
||||
class CoreNotificationsPlugin(SettingsMixin, InvenTreePlugin):
|
||||
class CoreNotificationsPlugin(SettingsContentMixin, SettingsMixin, InvenTreePlugin):
|
||||
"""Core notification methods for InvenTree."""
|
||||
|
||||
NAME = "CoreNotificationsPlugin"
|
||||
@ -39,8 +41,30 @@ class CoreNotificationsPlugin(SettingsMixin, InvenTreePlugin):
|
||||
'default': False,
|
||||
'validator': bool,
|
||||
},
|
||||
'ENABLE_NOTIFICATION_SLACK': {
|
||||
'name': _('Enable slack notifications'),
|
||||
'description': _('Allow sending of slack channel messages for event notifications'),
|
||||
'default': False,
|
||||
'validator': bool,
|
||||
},
|
||||
'NOTIFICATION_SLACK_URL': {
|
||||
'name': _('Slack incoming webhook url'),
|
||||
'description': _('URL that is used to send messages to a slack channel'),
|
||||
'protected': True,
|
||||
},
|
||||
}
|
||||
|
||||
def get_settings_content(self, request):
|
||||
"""Custom settings content for the plugin."""
|
||||
return """
|
||||
<p>Setup for Slack:</p>
|
||||
<ol>
|
||||
<li>Create a new Slack app on <a href="https://api.slack.com/apps/new" target="_blank">this page</a></li>
|
||||
<li>Enable <i>Incoming Webhooks</i> for the channel you want the notifications posted to</li>
|
||||
<li>Set the webhook URL in the settings above</li>
|
||||
<li>Enable the plugin</li>
|
||||
"""
|
||||
|
||||
class EmailNotification(PlgMixin, BulkNotificationMethod):
|
||||
"""Notificationmethod for delivery via Email."""
|
||||
|
||||
@ -94,3 +118,53 @@ class CoreNotificationsPlugin(SettingsMixin, InvenTreePlugin):
|
||||
InvenTree.tasks.send_email(subject, '', targets, html_message=html_message)
|
||||
|
||||
return True
|
||||
|
||||
class SlackNotification(PlgMixin, BulkNotificationMethod):
|
||||
"""Notificationmethod for delivery via Slack channel messages."""
|
||||
|
||||
METHOD_NAME = 'slack'
|
||||
METHOD_ICON = 'fa-envelope'
|
||||
GLOBAL_SETTING = 'ENABLE_NOTIFICATION_SLACK'
|
||||
|
||||
def get_targets(self):
|
||||
"""Not used by this method."""
|
||||
return self.targets
|
||||
|
||||
def send_bulk(self):
|
||||
"""Send the notifications out via slack."""
|
||||
|
||||
instance = registry.plugins.get(self.get_plugin().NAME.lower())
|
||||
url = instance.get_setting('NOTIFICATION_SLACK_URL')
|
||||
|
||||
if not url:
|
||||
return False
|
||||
|
||||
ret = requests.post(url, json={
|
||||
'text': str(self.context['message']),
|
||||
'blocks': [
|
||||
{
|
||||
"type": "section",
|
||||
"text": {
|
||||
"type": "plain_text",
|
||||
"text": str(self.context['name'])
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "section",
|
||||
"text": {
|
||||
"type": "mrkdwn",
|
||||
"text": str(self.context['message'])
|
||||
},
|
||||
"accessory": {
|
||||
"type": "button",
|
||||
"text": {
|
||||
"type": "plain_text",
|
||||
"text": str(_("Open link")), "emoji": True
|
||||
},
|
||||
"value": f'{self.category}_{self.obj.pk}',
|
||||
"url": self.context['link'],
|
||||
"action_id": "button-action"
|
||||
}
|
||||
}]
|
||||
})
|
||||
return ret.ok
|
||||
|
Reference in New Issue
Block a user