Skip to content

Commit

Permalink
[emails][m]: email admins re pending pkgs refs #13
Browse files Browse the repository at this point in the history
- override package_create to send email if approval_pending
- Email template including info about package, user and admin
- tests
  • Loading branch information
zelima committed Dec 4, 2018
1 parent 9f0d999 commit e384084
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 0 deletions.
16 changes: 16 additions & 0 deletions ckanext/ed/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
import zipfile

from ckan.controllers.admin import get_sysadmins
from ckan.lib.mailer import MailerException
from ckan.logic.action.create import package_create as core_package_create
from ckan.logic.action.get import package_show as core_package_show
from ckan.plugins import toolkit

from ckanext.ed import helpers
from ckanext.ed.mailer import mail_package_publish_request_to_admins


SUPPORTED_RESOURCE_MIMETYPES = [
'application/vnd.openxmlformats-officedocument.presentationml.presentation',
Expand Down Expand Up @@ -153,3 +157,15 @@ def package_show(context, data_dict):
if not can_edit and approval_pending:
raise toolkit.ObjectNotFound
return package


@toolkit.side_effect_free
def package_create(context, data_dict):
dataset_dict = core_package_create(context, data_dict)
if dataset_dict.get('approval_state') == 'approval_pending':
try:
mail_package_publish_request_to_admins(context, dataset_dict)
except MailerException:
message = '[email] Package Publishing request is not sent: {0}'
log.critical(message.format(data_dict.get('title')))
return dataset_dict
42 changes: 42 additions & 0 deletions ckanext/ed/mailer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import logging

from ckan import model
from ckan.common import config
from ckan.plugins import toolkit
from ckan.lib.mailer import mail_user
from ckan.lib.base import render_jinja2
from ckan.logic.action.get import member_list as core_member_list

log = logging.getLogger(__name__)


def mail_package_publish_request_to_admins(context, data_dict):
members = core_member_list(
context=context,
data_dict={'id': data_dict.get('owner_org')}
)
admin_ids = [i[0] for i in members if i[2] == 'Admin']
for admin_id in admin_ids:
user = model.User.get(admin_id)
if user.email:
subj = _compose_email_subj(data_dict, event='request')
body = _compose_email_body(data_dict, user, event='request')
mail_user(user, subj, body)
log.debug('[email] Pakcage publishing request email sent to {0}'.format(user.name))


def _compose_email_subj(data_dict, event='request'):
return '[US ED] Package Publishing {0}: {1}'.format(event.capitalize(), data_dict.get('title'))


def _compose_email_body(data_dict, user, event='request'):
pkg_link = toolkit.url_for('dataset_read', id=data_dict['name'], qualified=True)
return render_jinja2('emails/package_publish_{0}.txt'.format(event), {
'admin_name': user.fullname or user.name,
'site_title': config.get('ckan.site_title'),
'site_url': config.get('ckan.site_url'),
'package_title': data_dict.get('title'),
'package_description': data_dict.get('notes', ''),
'package_url': pkg_link,
'publisher_name': data_dict.get('contact_name')
})
1 change: 1 addition & 0 deletions ckanext/ed/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def get_helpers(self):
def get_actions(self):
return {
'ed_prepare_zip_resources': actions.prepare_zip_resources,
'package_create': actions.package_create,
'package_show': actions.package_show
}

Expand Down
18 changes: 18 additions & 0 deletions ckanext/ed/templates/emails/package_publish_request.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Dear {{ admin_name }},

A new package has been requested for publishing by {{ publisher_name }}:

{{ package_title }}

{{ package_description}}

To approve or reject the request, please visit the following page (logged in as a office administrator):

{{ package_url }}

Have a nice day.


--
Message sent by {{ site_title }} ({{ site_url }})
This is an automated message, please don't respond to this address.
51 changes: 51 additions & 0 deletions ckanext/ed/tests/test_mailer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import mock
from nose.tools import assert_raises, assert_equals

from ckan import model
from ckan.tests import factories as core_factories
from ckan.tests import helpers as test_helpers
from ckanext.ed.tests import factories

from ckanext.ed.mailer import mail_package_publish_request_to_admins


# Without jinja2 mocking it can't find a template in the test mode
class TestHelpers(test_helpers.FunctionalTestBase):
context = {'model': model, 'ignore_auth': True}

@mock.patch('ckanext.ed.mailer.mail_user')
@mock.patch('ckanext.ed.mailer.render_jinja2')
def test_mail_package_publish_request_to_admins(self, mock_render_jinja2, mock_mail_user):
admin_1 = core_factories.User()
admin_2 = core_factories.User()
editor = core_factories.User()
office = core_factories.Organization(
users=[
{'name': admin_1['name'], 'capacity': 'admin'},
{'name': admin_2['name'], 'capacity': 'admin'},
{'name': editor['name'], 'capacity': 'editor'},
],
name='us-ed',
id='us-ed'
)
mail_package_publish_request_to_admins(
self.context,
_create_dataset_dict('test', office['name'])
)
assert_equals(mock_mail_user.call_count, 2)
assert_equals(mock_render_jinja2.call_args[0][0], 'emails/package_publish_request.txt')


def _create_dataset_dict(package_name, office_name='us-ed'):
return {
'name': package_name,
'contact_name': 'Stu Shepard',
'program_code': '321',
'access_level': 'public',
'bureau_code': '123',
'contact_email': '%[email protected]' % package_name,
'notes': 'notes',
'owner_org': office_name,
'title': 'Title',
'identifier': 'identifier'
}

0 comments on commit e384084

Please sign in to comment.