33from django .template .loader import render_to_string
44from django .utils .translation import gettext_lazy as _
55
6+ import requests
67from allauth .account .models import EmailAddress
78
89import common .models
910import InvenTree .helpers
1011import InvenTree .tasks
11- from plugin import InvenTreePlugin
12- from plugin .mixins import BulkNotificationMethod , SettingsMixin
12+ from plugin import InvenTreePlugin , registry
13+ from plugin .mixins import (BulkNotificationMethod , SettingsContentMixin ,
14+ SettingsMixin )
1315
1416
1517class PlgMixin :
@@ -23,7 +25,7 @@ def get_plugin(self):
2325 return CoreNotificationsPlugin
2426
2527
26- class CoreNotificationsPlugin (SettingsMixin , InvenTreePlugin ):
28+ class CoreNotificationsPlugin (SettingsContentMixin , SettingsMixin , InvenTreePlugin ):
2729 """Core notification methods for InvenTree."""
2830
2931 NAME = "CoreNotificationsPlugin"
@@ -39,8 +41,30 @@ class CoreNotificationsPlugin(SettingsMixin, InvenTreePlugin):
3941 'default' : False ,
4042 'validator' : bool ,
4143 },
44+ 'ENABLE_NOTIFICATION_SLACK' : {
45+ 'name' : _ ('Enable slack notifications' ),
46+ 'description' : _ ('Allow sending of slack channel messages for event notifications' ),
47+ 'default' : False ,
48+ 'validator' : bool ,
49+ },
50+ 'NOTIFICATION_SLACK_URL' : {
51+ 'name' : _ ('Slack incoming webhook url' ),
52+ 'description' : _ ('URL that is used to send messages to a slack channel' ),
53+ 'protected' : True ,
54+ },
4255 }
4356
57+ def get_settings_content (self , request ):
58+ """Custom settings content for the plugin."""
59+ return """
60+ <p>Setup for Slack:</p>
61+ <ol>
62+ <li>Create a new Slack app on <a href="https://api.slack.com/apps/new" target="_blank">this page</a></li>
63+ <li>Enable <i>Incoming Webhooks</i> for the channel you want the notifications posted to</li>
64+ <li>Set the webhook URL in the settings above</li>
65+ <li>Enable the plugin</li>
66+ """
67+
4468 class EmailNotification (PlgMixin , BulkNotificationMethod ):
4569 """Notificationmethod for delivery via Email."""
4670
@@ -94,3 +118,53 @@ def send_bulk(self):
94118 InvenTree .tasks .send_email (subject , '' , targets , html_message = html_message )
95119
96120 return True
121+
122+ class SlackNotification (PlgMixin , BulkNotificationMethod ):
123+ """Notificationmethod for delivery via Slack channel messages."""
124+
125+ METHOD_NAME = 'slack'
126+ METHOD_ICON = 'fa-envelope'
127+ GLOBAL_SETTING = 'ENABLE_NOTIFICATION_SLACK'
128+
129+ def get_targets (self ):
130+ """Not used by this method."""
131+ return self .targets
132+
133+ def send_bulk (self ):
134+ """Send the notifications out via slack."""
135+
136+ instance = registry .plugins .get (self .get_plugin ().NAME .lower ())
137+ url = instance .get_setting ('NOTIFICATION_SLACK_URL' )
138+
139+ if not url :
140+ return False
141+
142+ ret = requests .post (url , json = {
143+ 'text' : str (self .context ['message' ]),
144+ 'blocks' : [
145+ {
146+ "type" : "section" ,
147+ "text" : {
148+ "type" : "plain_text" ,
149+ "text" : str (self .context ['name' ])
150+ }
151+ },
152+ {
153+ "type" : "section" ,
154+ "text" : {
155+ "type" : "mrkdwn" ,
156+ "text" : str (self .context ['message' ])
157+ },
158+ "accessory" : {
159+ "type" : "button" ,
160+ "text" : {
161+ "type" : "plain_text" ,
162+ "text" : str (_ ("Open link" )), "emoji" : True
163+ },
164+ "value" : f'{ self .category } _{ self .obj .pk } ' ,
165+ "url" : self .context ['link' ],
166+ "action_id" : "button-action"
167+ }
168+ }]
169+ })
170+ return ret .ok
0 commit comments