Skip to content

Instantly share code, notes, and snippets.

@bkeating
Created January 3, 2012 23:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bkeating/1557542 to your computer and use it in GitHub Desktop.
Save bkeating/1557542 to your computer and use it in GitHub Desktop.
Push Django UserProfile data to an existing MailChimp list
"""Iterate over User Profiles and push some fields to MailChimp.
This script assumes you already have a (lenghty) list setup on
mailchimp.com. It also assumes that you have a Django project utilizing
the Auth app and the User object's UserProfile (MemberProfile here).
It's all too custom to drop in and go, but have a look. This is how I
managed to push our Membership data into MailChimp. Learned a bunch of
stuff in the process.
"""
from pychimp import PyChimp
api = PyChimp('xxxxxxxxxxxxxxxxxxxxxxxxxxx-us2')
api.ping()
members = 0
nonmembers = 0
cancelled = 0
list_id = "xxxxxxxxxxxxx"
subscriber_list = api.listMembers(list_id,limit='15000')
for subscriber in subscriber_list:
subscriber_email = subscriber['email']
try:
member = MemberProfile.objects.get(user__email=subscriber_email)
if member.user.is_active:
merge_dict = {
'FNAME': member.user.first_name,
'LNAME': member.user.last_name,
'DATEJOINED': member.user.date_joined,
'LASTLOGIN': member.user.last_login,
'USERTYPE': member.get_user_type_display(),
'MEMNUM': member.membership_number,
'MEMSTATUS': member.standing(),
'MEMLEVEL': member.membership_level.name,
'PAYMETHOD': member.get_payment_method_display(),
'STREETADDR': "%s %s %s %s %s" % (
member.billing_addr_line1,
member.billing_addr_city,
member.billing_addr_state,
member.billing_addr_zip,
member.billing_addr_country
),
'ZIPCODE': member.billing_addr_zip,
}
api.listUpdateMember(list_id, subscriber_email, merge_dict, 'html', False);
members = members+1
else:
print "CANCELLED: %s" % member.user.email
cancelled = cancelled + 1
except:
print "NOT FOUND: %s" % subscriber_email
nonmembers = nonmembers+1
print "Members found in list: %s" % members
print "Non-Members found in list: %s" % nonmembers
print "Cancelled Members found in list: %s" % cancelled
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment