From 096fad403d39426232d1ac2ebfc268a464632820 Mon Sep 17 00:00:00 2001 From: Andrew Marwood Date: Tue, 13 Feb 2024 12:29:49 +1100 Subject: [PATCH] fix handling of additional monitoring email address (#1184) --- newsroom/monitoring/email_alerts.py | 8 ++++---- newsroom/monitoring/forms.py | 7 +++---- tests/test_monitoring.py | 16 ++++++++++++++++ 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/newsroom/monitoring/email_alerts.py b/newsroom/monitoring/email_alerts.py index 123cec8d..b2d7c549 100644 --- a/newsroom/monitoring/email_alerts.py +++ b/newsroom/monitoring/email_alerts.py @@ -24,7 +24,6 @@ from .utils import get_monitoring_file, truncate_article_body, get_date_items_dict import base64 import os -import re try: from urllib.parse import urlparse @@ -255,9 +254,10 @@ def filter_users(self, m, company): u['is_enabled'] and u['company'] == company['_id']]] # append any addresses from the profile if m.get('email'): - for address in re.split(r'[, ]*', m.get('email')): - if address not in email_addresses: - email_addresses.append(address) + address_list = m.get('email').split(',') + for address in address_list: + if address.strip() not in email_addresses: + email_addresses.append(address.strip()) return email_addresses def send_alerts(self, monitoring_list, created_from, created_from_time, now): diff --git a/newsroom/monitoring/forms.py b/newsroom/monitoring/forms.py index d1c57cf2..616c1f87 100644 --- a/newsroom/monitoring/forms.py +++ b/newsroom/monitoring/forms.py @@ -4,7 +4,6 @@ from wtforms import SelectField from wtforms.validators import DataRequired, Email, Optional from copy import deepcopy -import re alert_types = [('full_text', gettext('Full text')), ('linked_text', gettext('Linked extract(s)'))] format_types = [('monitoring_pdf', gettext('PDF')), ('monitoring_rtf', gettext('RTF')), @@ -32,10 +31,10 @@ class Meta: query = TextAreaField(gettext('Query')) def validate_email(form, field): - address_list = re.split(r'[, ]*', field.data) + address_list = field.data.split(',') input_data = deepcopy(field.data) for address in address_list: - v = Email(message=field.gettext('Invalid email address: ') + address) - field.data = address + v = Email(message=field.gettext('Invalid email address: ') + address.strip()) + field.data = address.strip() v(form, field) field.data = input_data diff --git a/tests/test_monitoring.py b/tests/test_monitoring.py index 8253e121..9c5637ec 100644 --- a/tests/test_monitoring.py +++ b/tests/test_monitoring.py @@ -985,3 +985,19 @@ def test_send_profile_email(client, app): assert len(outbox[0].recipients) == 4 assert 'atest@a.com' in outbox[0].recipients assert 'btest@b.com' in outbox[0].recipients + + +def test_save_monitoring_email(client, app): + test_login_succeeds_for_admin(client) + m = app.data.find_one('monitoring', None, _id="5db11ec55f627d8aa0b545fb") + m['email'] = 'axb.com, a@b.com' + response = client.post('/monitoring/5db11ec55f627d8aa0b545fb', data=json.dumps(m), content_type='application/json') + data = json.loads(response.get_data()) + assert data['email'][0] == 'Invalid email address: axb.com' + m['email'] = 'a@b.com , d@e.com' + response = client.post('/monitoring/5db11ec55f627d8aa0b545fb', data=json.dumps(m), content_type='application/json') + data = json.loads(response.get_data()) + assert data['success'] is True + response = client.get('/monitoring/5db11ec55f627d8aa0b545fb') + data = json.loads(response.get_data()) + assert data['email'] == 'a@b.com,d@e.com'