Skip to content

Commit

Permalink
[#1635] Rename ckan.email_notifications -> activity_streams_email_not…
Browse files Browse the repository at this point in the history
…ifications

This makes things more extensible
  • Loading branch information
Sean Hammond committed Dec 17, 2012
1 parent ba4d6df commit 08970c9
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 61 deletions.
2 changes: 1 addition & 1 deletion ckan/config/deployment.ini_tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ ckan.feeds.author_link =
#
# Uncomment this line to enable activity streams email notifications.
# You also need to setup a cron job to send the emails, see the documentation.
#ckan.email_notifications = True
#ckan.activity_streams_email_notifications = True

# Email notifications for events older than this time delta will not be sent.
# Accepted formats: '2 days', '14 days', '4:35:00' (hours, minutes, seconds),
Expand Down
6 changes: 3 additions & 3 deletions ckan/controllers/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ def edit(self, id=None, data=None, errors=None, error_summary=None):

c.is_myself = True
c.show_email_notifications = asbool(
config.get('ckan.email_notifications'))
config.get('ckan.activity_streams_email_notifications'))
c.form = render(self.edit_user_form, extra_vars=vars)

return render('user/edit.html')
Expand All @@ -260,8 +260,8 @@ def _save_edit(self, id, context):
data_dict['id'] = id

# MOAN: Do I really have to do this here?
if 'email_notifications' not in data_dict:
data_dict['email_notifications'] = False
if 'activity_streams_email_notifications' not in data_dict:
data_dict['activity_streams_email_notifications'] = False

user = get_action('user_update')(context, data_dict)
h.flash_success(_('Profile updated'))
Expand Down
59 changes: 28 additions & 31 deletions ckan/lib/email_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ def _notifications_for_activities(activities, user_dict):
if not activities:
return []

if not user_dict.get('activity_streams_email_notifications'):
return []

# We just group all activities into a single "new activity" email that
# doesn't say anything about _what_ new activities they are.
# TODO: Here we could generate some smarter content for the emails e.g.
Expand Down Expand Up @@ -177,37 +180,31 @@ def send_notification(user, email_dict):

def get_and_send_notifications_for_user(user):

if user['email_notifications']:

# Parse the email_notifications_since config setting, email
# notifications from longer ago than this time will not be sent.
email_notifications_since = pylons.config.get(
'ckan.email_notifications_since', '2 days')
email_notifications_since = string_to_timedelta(
email_notifications_since)
email_notifications_since = (datetime.datetime.now()
- email_notifications_since)

# FIXME: We are accessing model from lib here but I'm not sure what
# else to do unless we add a get_email_last_sent() logic function which
# would only be needed by this lib.
email_last_sent = model.Dashboard.get(user['id']).email_last_sent
activity_stream_last_viewed = (
model.Dashboard.get(user['id']).activity_stream_last_viewed)

since = max(email_notifications_since, email_last_sent,
activity_stream_last_viewed)

notifications = get_notifications(user, since)

# TODO: Handle failures from send_email_notification.
for notification in notifications:
send_notification(user, notification)

# Whether the user had har 'email_notifications' preference turned on or
# not, we still update her email_last_sent time. This prevents users from
# getting emails about old activities when they turn on email
# notifications.
# Parse the email_notifications_since config setting, email notifications
# from longer ago than this time will not be sent.
email_notifications_since = pylons.config.get(
'ckan.email_notifications_since', '2 days')
email_notifications_since = string_to_timedelta(
email_notifications_since)
email_notifications_since = (datetime.datetime.now()
- email_notifications_since)

# FIXME: We are accessing model from lib here but I'm not sure what
# else to do unless we add a get_email_last_sent() logic function which
# would only be needed by this lib.
email_last_sent = model.Dashboard.get(user['id']).email_last_sent
activity_stream_last_viewed = (
model.Dashboard.get(user['id']).activity_stream_last_viewed)

since = max(email_notifications_since, email_last_sent,
activity_stream_last_viewed)

notifications = get_notifications(user, since)

# TODO: Handle failures from send_email_notification.
for notification in notifications:
send_notification(user, notification)

# FIXME: We are accessing model from lib here but I'm not sure what
# else to do unless we add a update_email_last_sent()
# logic function which would only be needed by this lib.
Expand Down
6 changes: 3 additions & 3 deletions ckan/logic/action/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -1018,9 +1018,9 @@ def send_email_notifications(context, data_dict):
_check_access('send_email_notifications', context, data_dict)

if not paste.deploy.converters.asbool(
pylons.config.get('ckan.email_notifications')):
raise logic.ParameterError(
'ckan.email_notifications is not enabled in config')
pylons.config.get('ckan.activity_streams_email_notifications')):
raise logic.ParameterError('ckan.activity_streams_email_notifications'
' is not enabled in config')

ckan.lib.email_notifications.get_and_send_notifications_for_all_users()

Expand Down
2 changes: 1 addition & 1 deletion ckan/logic/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ def default_user_schema():
'openid': [ignore_missing],
'apikey': [ignore],
'reset_key': [ignore],
'email_notifications': [ignore_missing],
'activity_streams_email_notifications': [ignore_missing],
}
return schema

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ def upgrade(migrate_engine):
metadata.bind = migrate_engine
migrate_engine.execute('''
ALTER TABLE public.user
ADD COLUMN email_notifications BOOLEAN DEFAULT FALSE;
ADD COLUMN activity_streams_email_notifications BOOLEAN DEFAULT FALSE;
''')
3 changes: 2 additions & 1 deletion ckan/model/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
Column('created', types.DateTime, default=datetime.datetime.now),
Column('reset_key', types.UnicodeText),
Column('about', types.UnicodeText),
Column('email_notifications', types.Boolean, default=False),
Column('activity_streams_email_notifications', types.Boolean,
default=False),
Column('sysadmin', types.Boolean, default=False),
)

Expand Down
2 changes: 1 addition & 1 deletion ckan/templates/user/edit_user_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<fieldset>
<legend>{{ _('Change your preferences') }}</legend>

{{ form.checkbox('email_notifications', label=_('Email Notifications'), id='field-email-notifications', value=True, checked=c.userobj.email_notifications, classes=['control-medium']) }}
{{ form.checkbox('email_notifications', label=_('Email Notifications'), id='field-email-notifications', value=True, checked=c.userobj.activity_streams_email_notifications, classes=['control-medium']) }}

</fieldset>
{% endif %}
Expand Down
30 changes: 17 additions & 13 deletions ckan/tests/functional/api/test_email_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def setup_class(cls):
cls.annafan = {'id': annafan.id,
'apikey': annafan.apikey,
}
pylons.config['ckan.email_notifications'] = True
pylons.config['ckan.activity_streams_email_notifications'] = True

@classmethod
def teardown_class(self):
Expand Down Expand Up @@ -76,7 +76,8 @@ def test_01_no_email_notifications_after_registration(self):
sara = tests.call_action_api(self.app, 'user_create',
apikey=self.testsysadmin['apikey'], name='sara',
email='sara@sararollins.com', password='sara',
fullname='Sara Rollins', email_notifications=True)
fullname='Sara Rollins',
activity_streams_email_notifications=True)

# Save the user for later tests to use.
TestEmailNotifications.sara = sara
Expand Down Expand Up @@ -223,10 +224,10 @@ def test_00_email_notifications_disabled_by_default(self):
TestEmailNotificationsUserPreference.sara = sara

# Email notifications should be disabled for the new user.
assert sara['email_notifications'] is False
assert sara['activity_streams_email_notifications'] is False
assert (tests.call_action_api(self.app, 'user_show',
apikey=self.sara['apikey'], id='sara')['email_notifications']
is False)
apikey=self.sara['apikey'], id='sara')[
'activity_streams_email_notifications'] is False)

def test_01_no_email_notifications_when_disabled(self):
'''Users with email notifications turned off should not get emails.'''
Expand Down Expand Up @@ -276,7 +277,7 @@ def test_02_enable_email_notifications(self):
assert len(self.get_smtp_messages()) == 0

# Enable email notifications for Sara.
self.sara['email_notifications'] = True
self.sara['activity_streams_email_notifications'] = True
tests.call_action_api(self.app, 'user_update', **self.sara)

tests.call_action_api(self.app, 'send_email_notifications',
Expand Down Expand Up @@ -304,7 +305,7 @@ def test_02_enable_email_notifications(self):
def test_03_disable_email_notifications(self):
'''Users should be able to turn email notifications off.'''

self.sara['email_notifications'] = False
self.sara['activity_streams_email_notifications'] = False
tests.call_action_api(self.app, 'user_update', **self.sara)

tests.call_action_api(self.app, 'package_update',
Expand All @@ -322,15 +323,16 @@ def test_03_disable_email_notifications(self):
class TestEmailNotificationsIniSetting(
mock_mail_server.SmtpServerHarness,
pylons_controller.PylonsTestCase):
'''Tests for the ckan.email_notifications config setting.'''
'''Tests for the ckan.activity_streams_email_notifications config setting.
'''
@classmethod
def setup_class(cls):
config = paste.deploy.appconfig('config:test.ini',
relative_to=ckan.tests.conf_dir)

# Disable the email notifications feature.
config.local_conf['ckan.email_notifications'] = False
config.local_conf['ckan.activity_streams_email_notifications'] = False

wsgiapp = ckan.config.middleware.make_app(config.global_conf,
**config.local_conf)
Expand Down Expand Up @@ -368,10 +370,11 @@ def test_00_send_email_notifications_feature_disabled(self):
TestEmailNotificationsIniSetting.sara = sara

# Enable the new user's email notifications preference.
sara['email_notifications'] = True
sara['activity_streams_email_notifications'] = True
tests.call_action_api(self.app, 'user_update', **sara)
assert (tests.call_action_api(self.app, 'user_show',
apikey=self.sara['apikey'], id='sara')['email_notifications']
apikey=self.sara['apikey'], id='sara')[
'activity_streams_email_notifications']
is True)

# Make Sara follow something so she gets some new activity in her
Expand Down Expand Up @@ -452,10 +455,11 @@ def test_00_email_notifications_since(self):
TestEmailNotificationsSinceIniSetting.sara = sara

# Enable the new user's email notifications preference.
sara['email_notifications'] = True
sara['activity_streams_email_notifications'] = True
tests.call_action_api(self.app, 'user_update', **sara)
assert (tests.call_action_api(self.app, 'user_show',
apikey=self.sara['apikey'], id='sara')['email_notifications']
apikey=self.sara['apikey'], id='sara')[
'activity_streams_email_notifications']
is True)

# Make Sara follow something so she gets some new activity in her
Expand Down
2 changes: 1 addition & 1 deletion ckan/tests/lib/test_dictization.py
Original file line number Diff line number Diff line change
Expand Up @@ -923,7 +923,7 @@ def test_16_group_dictized(self):
'name': u'annafan',
'number_administered_packages': 1L,
'number_of_edits': 0L,
'email_notifications': False,
'activity_stream_email_notifications': False,
}],
'name': u'help',
'display_name': u'help',
Expand Down
8 changes: 4 additions & 4 deletions doc/email-notifications.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ notifications for a CKAN site, a sysadmin must:


2. CKAN will not send out any email notifications, nor show the email
notifications preference to users, unless the ``ckan.email_notifications``
option is set to ``True``, so put this line in the ``[app:main]`` section of
your CKAN config file::
notifications preference to users, unless the
``ckan.activity_streams_email_notifications`` option is set to ``True``, so
put this line in the ``[app:main]`` section of your CKAN config file::

ckan.email_notifications = True
ckan.activity_streams_email_notifications = True


3. Make sure that ``ckan.site_url`` is set correctly in the ``[app:main]``
Expand Down
2 changes: 1 addition & 1 deletion test-core.ini
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ openid_enabled = True

ckan.datasets_per_page = 20

ckan.email_notifications = True
ckan.activity_streams_email_notifications = True

ckan.activity_list_limit = 15

Expand Down

0 comments on commit 08970c9

Please sign in to comment.