Skip to content

Commit 1a72ae9

Browse files
authored
Merge pull request #168 from Xpirix/send_email_to_approvers
Send plugin upload notifications to a specific group members only
2 parents f1c08cd + 6195433 commit 1a72ae9

File tree

5 files changed

+69
-11
lines changed

5 files changed

+69
-11
lines changed

qgis-app/fixtures/auth.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
[
2+
{
3+
"pk": 1,
4+
"model": "auth.group",
5+
"fields": {
6+
"name": "Plugin Notification Recipients",
7+
"permissions": []
8+
}
9+
},
210
{
311
"pk": 2,
412
"model": "auth.user",
@@ -52,5 +60,23 @@
5260
"email": "staff@staff.it",
5361
"date_joined": "2010-11-25 07:35:20"
5462
}
63+
},
64+
{
65+
"pk": 4,
66+
"model": "auth.user",
67+
"fields": {
68+
"username": "staff-recipient",
69+
"first_name": "",
70+
"last_name": "",
71+
"is_active": true,
72+
"is_superuser": false,
73+
"is_staff": true,
74+
"last_login": "2024-06-01 00:00:00",
75+
"groups": [1],
76+
"user_permissions": [],
77+
"password": "pbkdf2_sha256$150000$dummy$dummyhash",
78+
"email": "staff.recipient@example.com",
79+
"date_joined": "2024-06-01 00:00:00"
80+
}
5581
}
5682
]

qgis-app/plugins/tests/test_plugin_update.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,15 @@ def test_plugin_new_version(self):
9191
self.assertEqual(self.plugin.repository, "https://github.com/")
9292

9393
self.assertIn(
94+
'staff.recipient@example.com',
95+
mail.outbox[0].recipients(),
96+
)
97+
98+
self.assertNotIn(
9499
'admin@admin.it',
95100
mail.outbox[0].recipients(),
96101
)
97-
self.assertIn(
102+
self.assertNotIn(
98103
'staff@staff.it',
99104
mail.outbox[0].recipients()
100105
)
@@ -148,10 +153,15 @@ def test_plugin_version_update(self):
148153
self.assertEqual(self.plugin.repository, "https://github.com/")
149154

150155
self.assertIn(
156+
'staff.recipient@example.com',
157+
mail.outbox[0].recipients(),
158+
)
159+
160+
self.assertNotIn(
151161
'admin@admin.it',
152162
mail.outbox[0].recipients(),
153163
)
154-
self.assertIn(
164+
self.assertNotIn(
155165
'staff@staff.it',
156166
mail.outbox[0].recipients()
157167
)

qgis-app/plugins/tests/test_plugin_upload.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,15 @@ def test_plugin_upload_form(self):
6868
self.assertTrue(PluginVersion.objects.filter(plugin__name='Test Plugin', version='0.0.1').exists())
6969

7070
self.assertIn(
71+
'staff.recipient@example.com',
72+
mail.outbox[0].recipients(),
73+
)
74+
75+
self.assertNotIn(
7176
'admin@admin.it',
7277
mail.outbox[0].recipients(),
7378
)
74-
self.assertIn(
79+
self.assertNotIn(
7580
'staff@staff.it',
7681
mail.outbox[0].recipients()
7782
)

qgis-app/plugins/views.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
from plugins.validator import PLUGIN_REQUIRED_METADATA
3838
from django.contrib.gis.geoip2 import GeoIP2
3939
from plugins.utils import parse_remote_addr
40+
from django.conf import settings
4041

4142
from django.template.response import TemplateResponse
4243

@@ -55,6 +56,9 @@
5556
staff_required = user_passes_test(lambda u: u.is_staff)
5657
from plugins.tasks.generate_plugins_xml import generate_plugins_xml
5758

59+
# Plugin Notification Recipients Group Name
60+
NOTIFICATION_RECIPIENTS_GROUP_NAME = settings.NOTIFICATION_RECIPIENTS_GROUP_NAME
61+
5862

5963
def send_mail_wrapper(subject, message, mail_from, recipients, fail_silently=True):
6064
if settings.DEBUG:
@@ -65,13 +69,16 @@ def send_mail_wrapper(subject, message, mail_from, recipients, fail_silently=Tru
6569

6670
def plugin_notify(plugin):
6771
"""
68-
Sends a message to staff on new plugins
72+
Sends a message to staff that are in
73+
the notification recipients group on new plugins
6974
"""
7075
recipients = [
7176
u.email
72-
for u in User.objects.filter(is_staff=True, email__isnull=False).exclude(
73-
email=""
74-
)
77+
for u in User.objects.filter(
78+
groups__name=NOTIFICATION_RECIPIENTS_GROUP_NAME,
79+
is_staff=True,
80+
email__isnull=False
81+
).exclude(email="")
7582
]
7683

7784
if recipients:
@@ -98,15 +105,18 @@ def plugin_notify(plugin):
98105

99106
def version_notify(plugin_version):
100107
"""
101-
Sends a message to staff on new plugin versions
108+
Sends a message to staff that are in
109+
the notification recipients group on new plugin versions
102110
"""
103111
plugin = plugin_version.plugin
104112

105113
recipients = [
106114
u.email
107-
for u in User.objects.filter(is_staff=True, email__isnull=False).exclude(
108-
email=""
109-
)
115+
for u in User.objects.filter(
116+
groups__name=NOTIFICATION_RECIPIENTS_GROUP_NAME,
117+
is_staff=True,
118+
email__isnull=False
119+
).exclude(email="")
110120
]
111121

112122
if recipients:

qgis-app/settings_docker.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,13 @@
179179
# RPC2 Max upload size
180180
DATA_UPLOAD_MAX_MEMORY_SIZE = PLUGIN_MAX_UPLOAD_SIZE # same as max allowed plugin size
181181

182+
# Plugin Notification Recipients Group Name
183+
# used to send notifications when a new plugin
184+
# or a new version is uploaded. This group usually
185+
# contains the plugin approvers
186+
# but can be customized.
187+
NOTIFICATION_RECIPIENTS_GROUP_NAME = os.environ.get("NOTIFICATION_RECIPIENTS_GROUP_NAME", "Plugin Notification Recipients")
188+
182189
# Sentry
183190
SENTRY_DSN = os.environ.get("SENTRY_DSN", "")
184191
SENTRY_RATE = os.environ.get("SENTRY_RATE", 1.0)

0 commit comments

Comments
 (0)