def sync_data_from_mailchimp(**kwargs): """Endpoint that the mailchimp webhook sends its data to""" key = request.args.get("key") if key == settings.MAILCHIMP_WEBHOOK_SECRET_KEY: r = request action = r.values["type"] list_name = mailchimp_utils.get_list_name_from_id(list_id=r.values["data[list_id]"]) username = r.values["data[email]"] try: user = User.find_one(Q("username", "eq", username)) except NoResultsFound: sentry.log_exception() sentry.log_message("A user with this username does not exist.") raise HTTPError( 404, data=dict(message_short="User not found", message_long="A user with this username does not exist") ) if action == "unsubscribe": user.mailchimp_mailing_lists[list_name] = False user.save() elif action == "subscribe": user.mailchimp_mailing_lists[list_name] = True user.save() else: # TODO: get tests to pass with sentry logging # sentry.log_exception() # sentry.log_message("Unauthorized request to the OSF.") raise HTTPError(http.UNAUTHORIZED)
def sync_data_from_mailchimp(**kwargs): """Endpoint that the mailchimp webhook sends its data to""" key = request.args.get('key') if key == settings.MAILCHIMP_WEBHOOK_SECRET_KEY: r = request action = r.values['type'] list_name = mailchimp_utils.get_list_name_from_id( list_id=r.values['data[list_id]']) username = r.values['data[email]'] try: user = User.find_one(Q('username', 'eq', username)) except NoResultsFound: sentry.log_exception() sentry.log_message("A user with this username does not exist.") raise HTTPError( 404, data=dict( message_short='User not found', message_long='A user with this username does not exist')) if action == 'unsubscribe': user.mailchimp_mailing_lists[list_name] = False user.save() elif action == 'subscribe': user.mailchimp_mailing_lists[list_name] = True user.save() else: # TODO: get tests to pass with sentry logging # sentry.log_exception() # sentry.log_message("Unauthorized request to the OSF.") raise HTTPError(http.UNAUTHORIZED)
def sync_data_from_mailchimp(**kwargs): """Endpoint that the mailchimp webhook sends its data to""" key = request.args.get('key') if key == settings.MAILCHIMP_WEBHOOK_SECRET_KEY: r = request action = r.values['type'] list_name = mailchimp_utils.get_list_name_from_id(list_id=r.values['data[list_id]']) username = r.values['data[email]'] try: user = OSFUser.objects.get(username=username) except OSFUser.DoesNotExist: sentry.log_exception() sentry.log_message('A user with this username does not exist.') raise HTTPError(404, data=dict(message_short='User not found', message_long='A user with this username does not exist')) if action == 'unsubscribe': user.mailchimp_mailing_lists[list_name] = False user.save() elif action == 'subscribe': user.mailchimp_mailing_lists[list_name] = True user.save() else: # TODO: get tests to pass with sentry logging # sentry.log_exception() # sentry.log_message("Unauthorized request to the OSF.") raise HTTPError(http.UNAUTHORIZED)
def test_get_list_name_from_id(self, mock_get_mailchimp_api): list_id = '12345' mock_client = mock.MagicMock() mock_get_mailchimp_api.return_value = mock_client mock_client.lists.list.return_value = {'data': [{'id': list_id, 'name': 'foo'}]} list_name = mailchimp_utils.get_list_name_from_id(list_id) mock_client.lists.list.assert_called_with(filters={'list_id': list_id}) assert_equal(list_name, 'foo')