def subscribe_request_manage_code(context, data_dict): '''Request a code for managing existing subscriptions. Causes a email to be sent, containing a manage link. :param email: Email address to get a code for :returns: null ''' model = context['model'] _check_access(u'subscribe_request_manage_code', context, data_dict) email = data_dict['email'] # check they have a subscription subscription = model.Session.query(Subscription) \ .filter_by(email=email) \ .first() if not subscription: raise p.toolkit.ObjectNotFound( 'That email address does not have any subscriptions') # create and send a code manage_code = email_auth.create_code(subscription.email) try: email_auth.send_manage_email(manage_code, email=email) except MailerException as exc: log.error('Could not email manage code: {}'.format(exc)) raise return None
def test_no_object(self): code = email_auth.create_code('*****@*****.**') response = self._get_test_app().get('/subscribe/unsubscribe', params={ 'code': code, 'dataset': '' }, status=302) eq(response.location, 'http://test.ckan.net/?__no_cache__=True')
def test_no_subscription(self): Dataset() code = email_auth.create_code('*****@*****.**') response = self._get_test_app().get('/subscribe/unsubscribe-all', params={'code': code}, status=302) assert response.location.startswith('http://test.ckan.net/') response = response.follow() assert_in('Error unsubscribing: That user has no subscriptions', response.body.decode('utf8'))
def test_basic(self): dataset = Dataset() Subscription( dataset_id=dataset['id'], email='*****@*****.**', skip_verification=True, ) code = email_auth.create_code('*****@*****.**') response = self._get_test_app().get('/subscribe/manage', params={'code': code}, status=200) assert_in(dataset['title'], response.body.decode('utf8'))
def test_no_subscription(self): dataset = Dataset() code = email_auth.create_code('*****@*****.**') response = self._get_test_app().get('/subscribe/unsubscribe', params={ 'code': code, 'dataset': dataset['id'] }, status=302) assert response.location.startswith( 'http://test.ckan.net/dataset/{}'.format(dataset['name'])) response = response.follow() assert_in( 'Error unsubscribing: That user is not subscribed to that object', response.body.decode('utf8'))
def test_another_code(self): subscription = Subscription( email='*****@*****.**', frequency='WEEKLY', skip_verification=True, ) code = email_auth.create_code('*****@*****.**') response = self._get_test_app().post('/subscribe/update', params={ 'code': code, 'id': subscription['id'], 'frequency': 'daily' }, status=302) assert response.location.startswith( 'http://test.ckan.net/subscribe/request_manage_code')
def test_form_submit(self): Subscription( email='*****@*****.**', frequency='WEEKLY', skip_verification=True, ) code = email_auth.create_code('*****@*****.**') response = self._get_test_app().get('/subscribe/manage', params={'code': code}, status=200) form = response.forms["frequency-form"] form["frequency"] = u"IMMEDIATE" response = submit_and_follow(self._get_test_app(), form, {}, "save") assert_in('<option value="IMMEDIATE" selected>', response.body.decode('utf8'))
def test_basic(self): dataset = Dataset() Subscription( dataset_id=dataset['id'], email='*****@*****.**', skip_verification=True, ) code = email_auth.create_code('*****@*****.**') response = self._get_test_app().get('/subscribe/unsubscribe-all', params={'code': code}, status=302) assert_equal( response.location, 'http://test.ckan.net/?__no_cache__=True'.format(dataset['name'])) response = response.follow() assert_in('You are no longer subscribed to notifications from CKAN', response.body.decode('utf8'))
def test_org(self): org = Organization() Subscription( organization_id=org['id'], email='*****@*****.**', skip_verification=True, ) code = email_auth.create_code('*****@*****.**') response = self._get_test_app().get('/subscribe/unsubscribe', params={ 'code': code, 'organization': org['id'] }, status=302) assert_equal( response.location, 'http://test.ckan.net/organization/{}?__no_cache__=True'.format( org['name']))
def test_group(self): group = Group() Subscription( group_id=group['id'], email='*****@*****.**', skip_verification=True, ) code = email_auth.create_code('*****@*****.**') response = self._get_test_app().get('/subscribe/unsubscribe', params={ 'code': code, 'group': group['id'] }, status=302) assert_equal( response.location, 'http://test.ckan.net/group/{}?__no_cache__=True'.format( group['name']))
def test_basic(self): dataset = Dataset() Subscription( dataset_id=dataset['id'], email='*****@*****.**', skip_verification=True, ) code = email_auth.create_code('*****@*****.**') response = self._get_test_app().get('/subscribe/unsubscribe', params={ 'code': code, 'dataset': dataset['id'] }, status=302) assert_equal( response.location, 'http://test.ckan.net/dataset/{}?__no_cache__=True'.format( dataset['name']))
def subscribe_verify(context, data_dict): '''Verify (confirm) a subscription :param code: Verification code, supplied in the email sent on sign-up :returns: the updated subscription :rtype: dictionary ''' model = context['model'] user = context['user'] _check_access(u'subscribe_verify', context, data_dict) code = p.toolkit.get_or_bust(data_dict, 'code') subscription = model.Session.query(Subscription) \ .filter_by(verification_code=code) \ .first() if not subscription: raise p.toolkit.ValidationError( 'That validation code is not recognized') if subscription.verification_code_expires < datetime.datetime.now(): raise p.toolkit.ValidationError( 'That validation code has expired') # Verify the subscription if p.toolkit.check_ckan_version(max_version='2.8.99'): rev = model.repo.new_revision() rev.author = user subscription.verified = True subscription.verification_code = None # it can't be used again subscription.verification_code_expires = None if not context.get('defer_commit'): model.repo.commit() # Email the user confirmation and so they have a link to manage it manage_code = email_auth.create_code(subscription.email) email_auth.send_subscription_confirmation_email( manage_code, subscription=subscription) return dictization.dictize_subscription(subscription, context)
def test_submit(self): subscription = Subscription( email='*****@*****.**', frequency='WEEKLY', skip_verification=True, ) code = email_auth.create_code('*****@*****.**') response = self._get_test_app().post('/subscribe/update', params={ 'code': code, 'id': subscription['id'], 'frequency': 'daily' }, status=302) assert response.location.startswith( 'http://test.ckan.net/subscribe/manage?code=') response = response.follow() assert_in('<option value="DAILY" selected>', response.body.decode('utf8'))
def send_emails(notifications_by_email): for email, notifications in notifications_by_email.items(): code = email_auth.create_code(email) notification_email.send_notification_email(code, email, notifications)