diff --git a/README.rst b/README.rst index dca13dd..4b43d17 100644 --- a/README.rst +++ b/README.rst @@ -186,11 +186,11 @@ If you don't specify this setting, the default will be number-sequence. Send error mails when harvesting fails (optional) ================================================= -If you want to send and email when a Harvest Job fails, you can set the following configuration option in the ini file: +If you want to send an email when a Harvest Job fails, you can set the following configuration option in the ini file: ckan.harvest.status_mail.errored = True -That way, all CKAN Users who are declared as Sysadmins will receive the Error emails at their configured email address. +That way, all CKAN Users who are declared as Sysadmins will receive the Error emails at their configured email address. If the Harvest-Source of the failing Harvest-Job belongs to an organization, the error-mail will also be sent to the organization-members who have the admin-role if their E-Mail is configured. If you don't specify this setting, the default will be False. diff --git a/ckanext/harvest/logic/action/update.py b/ckanext/harvest/logic/action/update.py index f19b020..e56648a 100644 --- a/ckanext/harvest/logic/action/update.py +++ b/ckanext/harvest/logic/action/update.py @@ -621,14 +621,35 @@ def send_error_mail(context, source_id, status): msg += '\n--\n' msg += toolkit._('You are receiving this email because you are currently set-up as Administrator for {0}. Please do not reply to this email as it was sent from a non-monitored address.').format(config.get('ckan.site_title')) + recipients = [] + + # gather sysadmins model = context['model'] - sysadmins = model.Session.query(model.User).filter(model.User.sysadmin == True).all() + for sysadmin in sysadmins: + recipients.append({ + 'name': sysadmin.name, + 'email': sysadmin.email + }) - # for recipient in email_recipients: - for recipient in sysadmins: - email = {'recipient_name': recipient, - 'recipient_email': recipient, + # gather organization-admins + if source.get('organization'): + members = get_action('member_list')(context, { + 'id': source['organization']['id'], + 'object_type': 'user', + 'capacity': 'admin' + }) + for member in members: + member_details = get_action('user_show')(context, {'id': member[0]}) + if member_details['email']: + recipients.append({ + 'name': member_details['name'], + 'email': member_details['email'] + }) + + for recipient in recipients: + email = {'recipient_name': recipient['name'], + 'recipient_email': recipient['email'], 'subject': config.get('ckan.site_title') + ' - Harvesting Job - Error Notification', 'body': msg}