Merge pull request #788 from Gluejar/fix-mailchimp

Fix mailchimp
pull/91/head
eshellman 2018-06-20 10:56:13 -04:00 committed by GitHub
commit 4dbf3ab725
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 15 deletions

View File

@ -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 mailchimp3 import MailChimp
from mailchimp3.mailchimpclient import MailChimpError
#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)
mc_client = MailChimp(mc_api=settings.MAILCHIMP_API_KEY)
logger = logging.getLogger(__name__)
@ -1256,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
@ -1267,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)
@ -1276,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

View File

@ -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

View File

@ -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)

View File

@ -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