2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-07-01 11:10:54 +00:00

user notification settings

This commit is contained in:
Matthias
2022-04-04 23:46:19 +02:00
parent 2fee6b02db
commit 1eb511e8a0
9 changed files with 153 additions and 3 deletions

View File

@ -42,6 +42,11 @@ class CoreNotificationsPlugin(SettingsMixin, IntegrationPluginBase):
('template', 'subject', ),
]
GLOBAL_SETTING = 'ENABLE_NOTIFICATION_EMAILS'
USER_SETTING = {
'name': _('Enable email notifications'),
'description': _('Allow sending of emails for event notifications'),
'default': True,
'validator': bool,
}
def get_targets(self):

View File

@ -0,0 +1,29 @@
# Generated by Django 3.2.12 on 2022-04-03 23:38
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('plugin', '0004_alter_pluginsetting_key'),
]
operations = [
migrations.CreateModel(
name='NotificationUserSetting',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('key', models.CharField(help_text='Settings key (must be unique - case insensitive)', max_length=50)),
('value', models.CharField(blank=True, help_text='Settings value', max_length=200)),
('method', models.CharField(max_length=255, verbose_name='Method')),
('user', models.ForeignKey(blank=True, help_text='User', null=True, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL, verbose_name='User')),
],
options={
'unique_together': {('method', 'user', 'key')},
},
),
]

View File

@ -7,6 +7,7 @@ from __future__ import unicode_literals
from django.utils.translation import gettext_lazy as _
from django.db import models
from django.contrib.auth.models import User
import common.models
@ -182,3 +183,71 @@ class PluginSetting(common.models.BaseInvenTreeSetting):
verbose_name=_('Plugin'),
on_delete=models.CASCADE,
)
class NotificationUserSetting(common.models.BaseInvenTreeSetting):
"""
This model represents notification settings for a user
"""
class Meta:
unique_together = [
('method', 'user', 'key'),
]
def clean(self, **kwargs):
kwargs['method'] = self.method
super().clean(**kwargs)
"""
We override the following class methods,
so that we can pass the method instance
"""
def is_bool(self, **kwargs):
kwargs['method'] = self.method
return super().is_bool(**kwargs)
@property
def name(self):
return self.__class__.get_setting_name(self.key, method=self.method)
@property
def default_value(self):
return self.__class__.get_setting_default(self.key, method=self.method)
@property
def description(self):
return self.__class__.get_setting_description(self.key, method=self.method)
@property
def units(self):
return self.__class__.get_setting_units(self.key, method=self.method)
def choices(self):
return self.__class__.get_setting_choices(self.key, method=self.method)
@classmethod
def get_setting_definition(cls, key, method, **kwargs):
from common.notifications import storage
kwargs['settings'] = storage.user_settings
return super().get_setting_definition(key, **kwargs)
method = models.CharField(
max_length=255,
verbose_name=_('Method'),
)
user = models.ForeignKey(
User,
on_delete=models.CASCADE,
blank=True, null=True,
verbose_name=_('User'),
help_text=_('User'),
)

View File

@ -8,7 +8,7 @@ from django.urls import reverse
from common.models import InvenTreeSetting
from plugin import registry
from common.notifications import storage
register = template.Library()
@ -73,3 +73,10 @@ def plugin_errors(*args, **kwargs):
All plugin errors in the current session
"""
return registry.errors
@register.simple_tag(takes_context=True)
def notification_settings_list(context, *args, **kwargs):
"""
List of all user notification settings
"""
return storage.get_usersettings(user=context.get('user', None))