From 042613b0ae3aeaa412f981cbdbc22f0047c8f7a8 Mon Sep 17 00:00:00 2001 From: eric Date: Tue, 19 Jun 2018 14:14:10 -0400 Subject: [PATCH 1/2] wip --- core/models/__init__.py | 5 +++-- requirements_versioned.pip | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/core/models/__init__.py b/core/models/__init__.py index 7136fd16..215ad4fd 100755 --- a/core/models/__init__.py +++ b/core/models/__init__.py @@ -13,7 +13,8 @@ from tempfile import SpooledTemporaryFile import requests from ckeditor.fields import RichTextField from notification import models as notification -from postmonkey import PostMonkey, MailChimpException +#from postmonkey import PostMonkey, MailChimpException +from mailchimp3 import MailChimp #django imports from django.apps import apps @@ -95,7 +96,7 @@ from .bibmodels import ( ) from .rh_models import Claim, RightsHolder -pm = PostMonkey(settings.MAILCHIMP_API_KEY) +pm = MailChimp(mc_api=settings.MAILCHIMP_API_KEY) logger = logging.getLogger(__name__) diff --git a/requirements_versioned.pip b/requirements_versioned.pip index b668a74d..f1e0f02d 100644 --- a/requirements_versioned.pip +++ b/requirements_versioned.pip @@ -58,7 +58,7 @@ oauth2==1.5.211 oauthlib==1.1.2 pandas==0.19.1 paramiko==1.14.1 -postmonkey==1.0b +mailchimp3==3.0.4 pycrypto==2.6 pymarc==3.0.2 pyoai==2.5.0 From 1d83e7af784f9984132e9ed443f031d44f18a7e3 Mon Sep 17 00:00:00 2001 From: eric Date: Wed, 20 Jun 2018 10:03:48 -0400 Subject: [PATCH 2/2] update to mailchimp 3.0 api --- core/models/__init__.py | 28 +++++++++++++++++++++------- core/tasks.py | 16 ++++++++++++---- core/tests.py | 7 ++++--- 3 files changed, 37 insertions(+), 14 deletions(-) diff --git a/core/models/__init__.py b/core/models/__init__.py index 215ad4fd..f3795655 100755 --- a/core/models/__init__.py +++ b/core/models/__init__.py @@ -13,8 +13,8 @@ from tempfile import SpooledTemporaryFile import requests from ckeditor.fields import RichTextField from notification import models as notification -#from postmonkey import PostMonkey, MailChimpException from mailchimp3 import MailChimp +from mailchimp3.mailchimpclient import MailChimpError #django imports from django.apps import apps @@ -96,7 +96,7 @@ from .bibmodels import ( ) from .rh_models import Claim, RightsHolder -pm = MailChimp(mc_api=settings.MAILCHIMP_API_KEY) +mc_client = MailChimp(mc_api=settings.MAILCHIMP_API_KEY) logger = logging.getLogger(__name__) @@ -1257,10 +1257,17 @@ class UserProfile(models.Model): # use @example.org email addresses for testing! return False try: - return settings.MAILCHIMP_NEWS_ID in pm.listsForEmail(email_address=self.user.email) - except MailChimpException, e: - if e.code != 215: # don't log case where user is not on a list + member = mc_client.lists.members.get( + list_id=settings.MAILCHIMP_NEWS_ID, + subscriber_hash=self.user.email + ) + if member['status'] == 'subscribed': + return 'True' + except MailChimpError, e: + if e[0]['status'] != 404: # don't log case where user is not on a list logger.error("error getting mailchimp status %s" % (e)) + except ValueError, e: + logger.error("bad email address %s" % (self.user.email)) except Exception, e: logger.error("error getting mailchimp status %s" % (e)) return False @@ -1268,7 +1275,7 @@ class UserProfile(models.Model): def ml_subscribe(self, **kwargs): if "@example.org" in self.user.email: # use @example.org email addresses for testing! - return True + return from regluit.core.tasks import ml_subscribe_task ml_subscribe_task.delay(self, **kwargs) @@ -1277,7 +1284,14 @@ class UserProfile(models.Model): # use @example.org email addresses for testing! return True try: - return pm.listUnsubscribe(id=settings.MAILCHIMP_NEWS_ID, email_address=self.user.email) + mc_client.lists.members.delete( + list_id=settings.MAILCHIMP_NEWS_ID, + subscriber_hash=self.user.email, + ) + return True + except MailChimpError, e: + if e[0]['status'] != 404: # don't log case where user is not on a list + logger.error("error getting mailchimp status %s" % (e)) except Exception, e: logger.error("error unsubscribing from mailchimp list %s" % (e)) return False diff --git a/core/tasks.py b/core/tasks.py index 8a10b2f1..7095d5d6 100644 --- a/core/tasks.py +++ b/core/tasks.py @@ -17,6 +17,10 @@ from django.utils.timezone import now from notification.engine import send_all from notification import models as notification +from mailchimp3 import MailChimp +from mailchimp3.mailchimpclient import MailChimpError + + """ regluit imports """ @@ -33,6 +37,7 @@ from regluit.core.parameters import RESERVE, REWARDS, THANKS from regluit.utils.localdatetime import date_today logger = logging.getLogger(__name__) +mc_client = MailChimp(mc_api=settings.MAILCHIMP_API_KEY) @task def populate_edition(isbn): @@ -183,14 +188,17 @@ def convert_to_mobi(input_url, input_format="application/epub+zip"): def generate_mobi_ebook_for_edition(edition): return mobigen.generate_mobi_ebook_for_edition(edition) -from postmonkey import PostMonkey, MailChimpException -pm = PostMonkey(settings.MAILCHIMP_API_KEY) - @task def ml_subscribe_task(profile, **kwargs): try: if not profile.on_ml: - return pm.listSubscribe(id=settings.MAILCHIMP_NEWS_ID, email_address=profile.user.email, **kwargs) + data = {"email_address": profile.user.email, "status_if_new": "pending"} + mc_client.lists.members.create_or_update( + list_id=settings.MAILCHIMP_NEWS_ID, + subscriber_hash=profile.user.email, + data=data, + ) + return True except Exception, e: logger.error("error subscribing to mailchimp list %s" % (e)) return False diff --git a/core/tests.py b/core/tests.py index d684a9ef..2b15e5e7 100755 --- a/core/tests.py +++ b/core/tests.py @@ -1010,10 +1010,11 @@ class MailingListTests(TestCase): #mostly to check that MailChimp account is setp correctly def test_mailchimp(self): - from postmonkey import PostMonkey - pm = PostMonkey(settings.MAILCHIMP_API_KEY) + from mailchimp3 import MailChimp + mc_client = MailChimp(settings.MAILCHIMP_API_KEY) if settings.TEST_INTEGRATION: - self.assertEqual(pm.ping(), "Everything's Chimpy!") + root = mc_client.root.get() + self.assertEqual(root[u'account_id'], u'15472878790f9faa11317e085') self.user = User.objects.create_user('chimp_test', 'eric@gluejar.com', 'chimp_test') self.assertTrue(self.user.profile.on_ml)