Skip to content

Commit

Permalink
add email opt-out setting (#415)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikkonie committed Apr 24, 2024
1 parent b91586b commit e91df57
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Added
- Flake8 rule in ``Makefile`` (#1387)
- **Adminalerts**
- Admin alert email sending (#415)
- ``notify_email_alert`` app setting (#415)
- **Filesfolders**
- Optional pagination for REST API list views (#1313)
- **Projectroles**
Expand Down
21 changes: 21 additions & 0 deletions adminalerts/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@
from django.utils import timezone

# Projectroles dependency
from projectroles.models import SODAR_CONSTANTS
from projectroles.plugins import SiteAppPluginPoint

from adminalerts.models import AdminAlert
from adminalerts.urls import urlpatterns


# SODAR constants
APP_SETTING_SCOPE_USER = SODAR_CONSTANTS['APP_SETTING_SCOPE_USER']


class SiteAppPlugin(SiteAppPluginPoint):
"""Projectroles plugin for registering the app"""

Expand All @@ -22,6 +27,22 @@ class SiteAppPlugin(SiteAppPluginPoint):
#: UI URLs
urls = urlpatterns

#: App settings definition
app_settings = {
'notify_email_alert': {
'scope': APP_SETTING_SCOPE_USER,
'type': 'BOOLEAN',
'default': True,
'label': 'Receive email for admin alerts',
'description': (
'Receive email for important administrator alerts regarding '
'e.g. site downtime.'
),
'user_modifiable': True,
'global': False,
}
}

#: Iconify icon
icon = 'mdi:alert'

Expand Down
25 changes: 23 additions & 2 deletions adminalerts/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@


# Local constants
APP_NAME = 'adminalerts'
ALERT_MSG = 'New alert'
ALERT_MSG_UPDATED = 'Updated alert'
ALERT_DESC = 'Description'
Expand Down Expand Up @@ -246,7 +247,17 @@ def test_post_alt_email_superuser(self):
[settings.EMAIL_SENDER, self.user_regular.email],
)

# TODO: Test with email notifications disabled
def test_post_email_disable(self):
"""Test POST with email notifications disabled"""
app_settings.set(
APP_NAME, 'notify_email_alert', False, user=self.user_regular
)
self.assertEqual(len(mail.outbox), 0)
data = self._get_post_data()
with self.login(self.superuser):
response = self.client.post(self.url, data)
self.assertEqual(response.status_code, 302)
self.assertEqual(len(mail.outbox), 0)

def test_post_expired(self):
"""Test POST with old expiry date (should fail)"""
Expand Down Expand Up @@ -332,7 +343,17 @@ def test_post_email_inactive(self):
self.assertEqual(response.status_code, 302)
self.assertEqual(len(mail.outbox), 0) # No email for inactive event

# TODO: Test with email set true and email notifications disabled
def test_post_email_disable(self):
"""Test POST with disabled email notifications"""
app_settings.set(
APP_NAME, 'notify_email_alert', False, user=self.user_regular
)
self.assertEqual(len(mail.outbox), 0)
data = self._get_post_data(active=True, send_email=True)
with self.login(self.superuser):
response = self.client.post(self.url, data)
self.assertEqual(response.status_code, 302)
self.assertEqual(len(mail.outbox), 0)

def test_post_user(self):
"""Test POST by different user"""
Expand Down
8 changes: 6 additions & 2 deletions adminalerts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@


# Local constants
APP_NAME = 'adminalerts'
DEFAULT_PAGINATION = 15

EMAIL_SUBJECT = '{state} admin alert: {message}'
EMAIL_BODY = r'''
An admin alert has been {action}d by {issuer}:
Expand Down Expand Up @@ -100,7 +100,8 @@ def _get_email_recipients(cls, alert):
'email'
)
for u in users:
# TODO: Add opt-out app settings check here
if not app_settings.get(APP_NAME, 'notify_email_alert', user=u):
continue
if not u.email:
logger.warning('No email set for user: {}'.format(u.username))
continue
Expand Down Expand Up @@ -138,6 +139,9 @@ def _send_email(self, alert, action):
)
recipients = self._get_email_recipients(alert)
# NOTE: Recipients go under bcc
# NOTE: If we have no recipients in bcc we cancel sending
if len(recipients) == 0:
return 0
return send_generic_mail(
subject,
body,
Expand Down

0 comments on commit e91df57

Please sign in to comment.