Skip to content

Commit

Permalink
fix handling of additional monitoring email address (#1184)
Browse files Browse the repository at this point in the history
  • Loading branch information
marwoodandrew authored Feb 13, 2024
1 parent ab8d973 commit 096fad4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
8 changes: 4 additions & 4 deletions newsroom/monitoring/email_alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
7 changes: 3 additions & 4 deletions newsroom/monitoring/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')),
Expand Down Expand Up @@ -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
16 changes: 16 additions & 0 deletions tests/test_monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -985,3 +985,19 @@ def test_send_profile_email(client, app):
assert len(outbox[0].recipients) == 4
assert '[email protected]' in outbox[0].recipients
assert '[email protected]' 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, [email protected]'
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'] = '[email protected] , [email protected]'
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'] == '[email protected],[email protected]'

0 comments on commit 096fad4

Please sign in to comment.