-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[emails][m]: email admins re pending pkgs refs #13
- override package_create to send email if approval_pending - Email template including info about package, user and admin - tests
- Loading branch information
Showing
5 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} |