def get_and_send_notifications_for_user(user): # Parse the email_notifications_since config setting, email notifications # from longer ago than this time will not be sent. email_notifications_since = email_notifications.pylons.config.get( 'ckan.email_notifications_since', '2 days') email_notifications_since = email_notifications.string_to_timedelta( email_notifications_since) email_notifications_since = (email_notifications.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 = email_notifications.model.Dashboard.get( user['id']).email_last_sent activity_stream_last_viewed = (email_notifications.model.Dashboard.get( user['id']).activity_stream_last_viewed) since = max(email_notifications_since, email_last_sent, activity_stream_last_viewed) notifications = email_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. dash = email_notifications.model.Dashboard.get(user['id']) dash.email_last_sent = email_notifications.datetime.datetime.now() email_notifications.model.repo.commit()
def get_config(key): global _config if not _config: _config['email_notifications_since'] = string_to_timedelta( toolkit.config.get('ckan.email_notifications_since', '2 days')) _config['weekly_notification_day'] = \ {'monday': 0, 'tuesday': 1, 'wednesday': 2, 'thursday': 3, 'friday': 4, 'saturday': 5, 'sunday': 6}[ toolkit.config.get( 'ckanext.subscribe.weekly_notification_day', 'friday')] _config['daily_and_weekly_notification_time'] = \ datetime.datetime.strptime( toolkit.config.get('daily_and_weekly_notification_time', '9:00'), '%H:%M') return _config[key]
def test_string_to_time_delta(): assert email_notifications.string_to_timedelta('1 day') == ( datetime.timedelta(days=1)) assert email_notifications.string_to_timedelta('1 day') == ( datetime.timedelta(days=1)) assert email_notifications.string_to_timedelta('2 days') == ( datetime.timedelta(days=2)) assert email_notifications.string_to_timedelta('2\tdays') == ( datetime.timedelta(days=2)) assert email_notifications.string_to_timedelta('14 days') == ( datetime.timedelta(days=14)) assert email_notifications.string_to_timedelta('4:35:00') == ( datetime.timedelta(hours=4, minutes=35, seconds=00)) assert email_notifications.string_to_timedelta('4:35:12.087465') == ( datetime.timedelta(hours=4, minutes=35, seconds=12, milliseconds=87, microseconds=465)) assert email_notifications.string_to_timedelta('1 day, 3:23:34') == ( datetime.timedelta(days=1, hours=3, minutes=23, seconds=34)) assert email_notifications.string_to_timedelta('1 day, 3:23:34') == ( datetime.timedelta(days=1, hours=3, minutes=23, seconds=34)) assert email_notifications.string_to_timedelta('7 days, 3:23:34') == ( datetime.timedelta(days=7, hours=3, minutes=23, seconds=34)) assert email_notifications.string_to_timedelta('7 days,\t3:23:34') == ( datetime.timedelta(days=7, hours=3, minutes=23, seconds=34)) assert email_notifications.string_to_timedelta( '7 days, 3:23:34.087465') == datetime.timedelta(days=7, hours=3, minutes=23, seconds=34, milliseconds=87, microseconds=465) assert email_notifications.string_to_timedelta('.123456') == ( datetime.timedelta(milliseconds=123, microseconds=456)) nose.tools.assert_raises(logic.ValidationError, email_notifications.string_to_timedelta, 'foobar')
def test_invalid_string_to_time_delta(): with pytest.raises(logic.ValidationError): email_notifications.string_to_timedelta("foobar")
def test_valid_string_to_time_delta(text, delta): assert email_notifications.string_to_timedelta(text) == delta
def test_string_to_time_delta(): assert email_notifications.string_to_timedelta("1 day") == ( datetime.timedelta(days=1) ) assert email_notifications.string_to_timedelta("1 day") == ( datetime.timedelta(days=1) ) assert email_notifications.string_to_timedelta("2 days") == ( datetime.timedelta(days=2) ) assert email_notifications.string_to_timedelta("2\tdays") == ( datetime.timedelta(days=2) ) assert email_notifications.string_to_timedelta("14 days") == ( datetime.timedelta(days=14) ) assert email_notifications.string_to_timedelta("4:35:00") == ( datetime.timedelta(hours=4, minutes=35, seconds=00) ) assert email_notifications.string_to_timedelta("4:35:12.087465") == ( datetime.timedelta( hours=4, minutes=35, seconds=12, milliseconds=87, microseconds=465 ) ) assert email_notifications.string_to_timedelta("1 day, 3:23:34") == ( datetime.timedelta(days=1, hours=3, minutes=23, seconds=34) ) assert email_notifications.string_to_timedelta("1 day, 3:23:34") == ( datetime.timedelta(days=1, hours=3, minutes=23, seconds=34) ) assert email_notifications.string_to_timedelta("7 days, 3:23:34") == ( datetime.timedelta(days=7, hours=3, minutes=23, seconds=34) ) assert email_notifications.string_to_timedelta("7 days,\t3:23:34") == ( datetime.timedelta(days=7, hours=3, minutes=23, seconds=34) ) assert email_notifications.string_to_timedelta( "7 days, 3:23:34.087465" ) == datetime.timedelta( days=7, hours=3, minutes=23, seconds=34, milliseconds=87, microseconds=465, ) assert email_notifications.string_to_timedelta(".123456") == ( datetime.timedelta(milliseconds=123, microseconds=456) ) with pytest.raises(logic.ValidationError): email_notifications.string_to_timedelta("foobar")