2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-08-05 19:41:41 +00:00

[Refactor] Notification plugins (#9735)

* Refactor notification concept

- Notifications handled by plugins

* Cleanup

* Only send email if template provided in context

* Logic cleanup

* Fix log_error call

* Refactor error logging

- Ensure plugin slug is correctly attached
- Consistent format
- Logic fixes

* More robust plugin lookup

* Refactor calls to tringger_notification

* Tweak for build stock notification

* Low stock notification refactor

- Actually *use* the notification system
- Fix for email template

* Check stock only when build is issued

* Updated documentation

* Add PluginUserSetting class

- Allows plugins to define per-user settings

* Add API endpoints for PluginUserSetting model

* Placeholder for user-plugin-settings page

* Refactoring frontend code

* Placeholder panel

* Adds user interface for changing user-specific plugin settings

* Tweaks

* Remove old model

* Update documentation

* Playwright tests

* Update API version

* Fix unit test

* Fix removed arg

* Fixes for email notifications

- Track status of sending notifications
- Add helper "activate" method for plugin class
- Update unit tests

* Fix barcode tests

* More unit test fixes

* Test fixes

* Fix for settings models with extra fields

* Enhance unit test

* Remove old test file

* Check for null target_fnc

* Improve DB query efficiency

- Provide a flat list of active keys to plugin.is_active
- Prevents DB fetching (in certain circumstances)
- Add registry.active_plugins() method

* Bump query limit up for test

- In practice, this API endpoint is ~10 queries

* Handle potential errors

* Increase query limit for API test

* Increase query limit for some tests

* Bump API version

* Tweak unit test

* Tweak unit test

* Increased allowed queries

* fix user plugin settings

* Fix for unit test

* Update debug msg

* Tweak API

* Fix endpoint

* Remove "active plugin keys" code

* Restore previous behaviour

* Fix unit tests

* Tweak unit test

* Update src/backend/InvenTree/build/tasks.py

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update src/backend/InvenTree/plugin/base/integration/NotificationMixin.py

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Func updates

* Format

* Add notification settings

* Refactor plugin settings groups

* Fix func type

* Adjust message

* Additional unit tests

* Additional playwright tests

* Additional playwright test

---------

Co-authored-by: Matthias Mair <code@mjmair.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
Oliver
2025-07-26 13:05:59 +10:00
committed by GitHub
parent f862867e2b
commit 1085625af4
60 changed files with 1329 additions and 972 deletions

View File

@@ -0,0 +1,9 @@
---
title: Email Notification Plugin
---
## Email Notification Plugin
This plugin provides a mechanism to send email notifications to users when certain events occur in InvenTree. It implements the [NotificationMixin](../mixins/notification.md) mixin class, allowing it to send notifications based on events defined in the InvenTree system.
Emails are only sent to users who have a registered email address, and who have enabled email notifications in their user profile.

View File

@@ -19,8 +19,8 @@ The following builtin plugins are available in InvenTree:
| [BOM Exporter](./bom_exporter.md) | Custom [exporter](../mixins/export.md) for BOM data | Yes |
| [Currency Exchange](./currency_exchange.md) | Currency exchange rate plugin | Yes |
| [DigiKey](./barcode_digikey.md) | DigiKey barcode support | No |
| [Email Notification](./email_notification.md) | Email notification plugin | Yes |
| [InvenTree Barcode](./inventree_barcode.md) | Internal barcode support | Yes |
| [InvenTree Core Notifications](./notifications.md) | Core notification system | Yes |
| [InvenTree Exporter](./inventree_exporter.md) | Custom [exporter](../mixins/export.md) for InvenTree data | Yes |
| [Label Printer](./inventree_label.md) | Custom [label](../mixins/label.md) for InvenTree data | Yes |
| [Label Machine](./inventree_label_machine.md) | Custom [label](../mixins/label.md) for InvenTree data | Yes |
@@ -28,8 +28,10 @@ The following builtin plugins are available in InvenTree:
| [LCSC](./barcode_lcsc.md) | LCSC barcode support | No |
| [Mouser](./barcode_mouser.md) | Mouser barcode support | No |
| [Parameter Exporter](./part_parameter_exporter.md) | Custom [exporter](../mixins/export.md) for part parameter data | Yes |
| [Part Notifications](./part_notifications.md) | Notifications for part changes | No |
| [Part Update Notifications](./part_notifications.md) | Notifications for part changes | No |
| [Slack Notification](./slack_notification.md) | Slack notification plugin | No |
| [TME](./barcode_tme.md) | TME barcode support | No |
| [UI Notification](./ui_notification.md) | UI notification plugin | Yes |
### Plugin Table

View File

@@ -1,21 +0,0 @@
---
title: Notifications Plugin
---
## InvenTree Core Notifications
The **InvenTree Core Notifications** plugin provides a notification system for InvenTree. It allows users to receive notifications when certain events occur in the system.
### Activation
This plugin is a *mandatory* plugin, and is always enabled.
### Plugin Settings
The following settings are available for the notifications plugin:
{{ image("notification_settings.png", base="plugin/builtin", title="Notification Settings") }}
## Usage
Configure the plugin to enable the desired notification types. The plugin will then send notifications to users when the specified events occur.

View File

@@ -0,0 +1,11 @@
---
title: Slack Notification Plugin
---
## Slack Notification Plugin
This plugin provides a mechanism to send notifications to a Slack channel when certain events occur in InvenTree. It implements the [NotificationMixin](../mixins/notification.md) mixin class, allowing it to send notifications based on events defined in the InvenTree system.
### API Key
To use this plugin, you need to provide a Slack API key. This key is used to authenticate the plugin with the Slack API and send messages to the specified channel.

View File

@@ -0,0 +1,15 @@
---
title: UI Notification Plugin
---
## UI Notification Plugin
This plugin provides a mechanism to send notifications to users via the InvenTree User Interface (UI). It implements the [NotificationMixin](../mixins/notification.md) mixin class, allowing it to send notifications based on events defined in the InvenTree system.
## UI Display
Any notifications which are generated by the InvenTree core system will be sent to users via this plugin. The notifications will be displayed in the UI:
### Notification Indicator
A notification indicator will appear in the top right corner of the InvenTree UI, indicating the number of unread notifications.

View File

@@ -0,0 +1,45 @@
---
title: Notification Mixin
---
## NotificationMixin
The `NotificationMixin` class provides a plugin with the ability to send notifications to users when certain events occur in the system.
Any notification which is generated by the InvenTree core system can be sent to users via a custom plugin which implements this mixin class.
### send_notification
The `send_notification` method is used to send a notification to users:
::: plugin.base.integration.NotificationMixin.NotificationMixin.send_notification
options:
show_bases: False
show_root_heading: False
show_root_toc_entry: False
summary: False
members: []
extra:
show_sources: True
### filter_targets
If desired, the plugin can implement the `filter_targets` method to filter the list of users who will receive the notification. This allows for more granular control over which users are notified based on specific criteria.
::: plugin.base.integration.NotificationMixin.NotificationMixin.filter_targets
options:
show_bases: False
show_root_heading: False
show_root_toc_entry: False
summary: False
members: []
extra:
show_sources: True
## Built-in Notifications
The following built-in notifications plugins are available:
- [UI Notifications](../builtin/ui_notification.md)
- [Email Notifications](../builtin/email_notification.md)
- [Slack Notifications](../builtin/slack_notification.md)

View File

@@ -9,13 +9,69 @@ The *SettingsMixin* allows the plugin to save and load persistent settings to th
- Plugin settings are stored against the individual plugin, and thus do not have to be unique
- Plugin settings are stored using a "key:value" pair
Use the class constant `SETTINGS` for a dict of settings that should be added as global database settings.
## Plugin Settings
Use the class attribute `SETTINGS` for a dict of settings that should be added as global database settings.
The dict must be formatted similar to the following sample that shows how to use validator choices and default.
Take a look at the settings defined in `InvenTree.common.models.InvenTreeSetting` for all possible parameters.
### Example Plugin
### get_setting
Use the `get_setting` method to retrieve a setting value based on the provided key.
::: plugin.base.integration.SettingsMixin.SettingsMixin.get_setting
options:
show_bases: False
show_root_heading: False
show_root_toc_entry: False
summary: False
members: []
### set_setting
Use the `set_setting` method to set a value for a specific setting key.
::: plugin.base.integration.SettingsMixin.SettingsMixin.set_setting
options:
show_bases: False
show_root_heading: False
show_root_toc_entry: False
summary: False
members: []
## User Settings
Plugins may also define user-specific settings, which allow users to customize the behavior of the plugin on a per-user basis.
To add user-specific settings, use the `USER_SETTINGS` class attribute in a similar way to the `SETTINGS` attribute.
### get_user_setting
Use the `get_user_setting` method to retrieve a user-specific setting value based on the provided key and user.
::: plugin.base.integration.SettingsMixin.SettingsMixin.get_user_setting
options:
show_bases: False
show_root_heading: False
show_root_toc_entry: False
summary: False
members: []
### set_user_setting
Use the `set_user_setting` method to set a value for a specific user setting key.
::: plugin.base.integration.SettingsMixin.SettingsMixin.set_user_setting
options:
show_bases: False
show_root_heading: False
show_root_toc_entry: False
summary: False
members: []
## Example Plugin
Below is a simple example of how a plugin can implement settings:

View File

@@ -227,6 +227,7 @@ nav:
- Label Printing Mixin: plugins/mixins/label.md
- Locate Mixin: plugins/mixins/locate.md
- Navigation Mixin: plugins/mixins/navigation.md
- Notification Mixin: plugins/mixins/notification.md
- Report Mixin: plugins/mixins/report.md
- Schedule Mixin: plugins/mixins/schedule.md
- Settings Mixin: plugins/mixins/settings.md
@@ -238,24 +239,30 @@ nav:
- Label Printer: plugins/machines/label_printer.md
- Builtin Plugins:
- Builtin Plugins: plugins/builtin/index.md
- Auto Create Builds: plugins/builtin/auto_create_builds.md
- Auto Issue: plugins/builtin/auto_issue.md
- BOM Exporter: plugins/builtin/bom_exporter.md
- Currency Exchange: plugins/builtin/currency_exchange.md
- InvenTree Barcode: plugins/builtin/inventree_barcode.md
- InvenTree Exporter: plugins/builtin/inventree_exporter.md
- Label Printer: plugins/builtin/inventree_label.md
- Label Machine: plugins/builtin/inventree_label_machine.md
- Label Sheet: plugins/builtin/inventree_label_sheet.md
- Notification: plugins/builtin/notifications.md
- Parameter Exporter: plugins/builtin/part_parameter_exporter.md
- Part Notifications: plugins/builtin/part_notifications.md
- Barcode Plugins:
- Barcode Plugins: plugins/builtin/barcode_index.md
- InvenTree Barcode: plugins/builtin/inventree_barcode.md
- DigiKey Barcode Plugin: plugins/builtin/barcode_digikey.md
- LCSC Barcode Plugin: plugins/builtin/barcode_lcsc.md
- Mouser Barcode Plugin: plugins/builtin/barcode_mouser.md
- TME Barcode Plugin: plugins/builtin/barcode_tme.md
- Event Plugins:
- Auto Create Builds: plugins/builtin/auto_create_builds.md
- Auto Issue: plugins/builtin/auto_issue.md
- Part Update Notification: plugins/builtin/part_notifications.md
- Export Plugins:
- BOM Exporter: plugins/builtin/bom_exporter.md
- InvenTree Exporter: plugins/builtin/inventree_exporter.md
- Parameter Exporter: plugins/builtin/part_parameter_exporter.md
- Label Printing:
- Label Printer: plugins/builtin/inventree_label.md
- Label Machine: plugins/builtin/inventree_label_machine.md
- Label Sheet: plugins/builtin/inventree_label_sheet.md
- Notification Plugins:
- Email Notifications: plugins/builtin/email_notification.md
- Slack Notifications: plugins/builtin/slack_notification.md
- UI Notifications: plugins/builtin/ui_notification.md
- Currency Exchange: plugins/builtin/currency_exchange.md
- Third-Party: plugins/integrate.md
# Plugins