Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _raise_response_error(r):
# in case of a 500 error, the response might not be a JSON
try:
error_data = r.json()
except ValueError:
error_data = { "response": r }
raise MailChimpError(error_data)
def subscribe_user(self, list_id, email):
# subscribe the user to the newsletter if they're not already
# subscribed, with the status pending
try:
self.client.lists.members.create(
list_id, data={"email_address": email, "status": "pending"}
)
return True
# if the user is already subscribed, return False
except MailChimpError:
return False
if site_limits.daily_signups < site_limits.daily_signup_limit:
user.total_case_allowance = user.case_allowance_remaining = settings.API_CASE_DAILY_ALLOWANCE
user.save()
# sign them up for the mailing list if they selected the mailing_list checkbox.
if settings.MAILCHIMP['api_key'] and user.mailing_list:
try:
mc_client = MailChimp(mc_api=settings.MAILCHIMP['api_key'], mc_user=settings.MAILCHIMP['api_user'])
mc_client.lists.members.create(
settings.MAILCHIMP['id'], {
'email_address': user.email,
'merge_fields': {'LNAME': user.first_name, 'FNAME': user.last_name},
'status': 'subscribed'
})
mailing_list_message = "Also, thanks for signing up for our newsletter, Lawvocado."
except MailChimpError as e:
if e.args[0]['status'] == 400 and e.args[0]['title'] == 'Member Exists':
mailing_list_message = "Also, thanks for your continued interest in our newsletter, " \
"Lawvocado. We'll keep you on our list."
else:
logger.exception("Error adding user email %s to mailing list" % user.email)
return render(request, 'registration/verified.html', {
'error': error,
'mailing_list_message': mailing_list_message,
})
raise APIException("You must provide an active API token ID")
# We could be more aggressive with requirements checking here, but
# there could be use cases where you don't want to supply an email.
supplied_email_address = self.request.data.get('email_address', None)
email_ccdl_ok = self.request.data.get('email_ccdl_ok', False)
if supplied_email_address and MAILCHIMP_API_KEY \
and settings.RUNNING_IN_CLOUD and email_ccdl_ok:
try:
client = mailchimp3.MailChimp(mc_api=MAILCHIMP_API_KEY, mc_user=MAILCHIMP_USER)
data = {
"email_address": supplied_email_address,
"status": "subscribed"
}
client.lists.members.create(MAILCHIMP_LIST_ID, data)
except mailchimp3.mailchimpclient.MailChimpError as mc_e:
pass # This is likely an user-already-on-list error. It's okay.
except Exception as e:
# Something outside of our control has gone wrong. It's okay.
logger.exception("Unexpected failure trying to add user to MailChimp list.",
supplied_email_address=supplied_email_address,
mc_user=MAILCHIMP_USER
)
if not already_processing:
# Create and dispatch the new job.
processor_job = ProcessorJob()
processor_job.pipeline_applied = "SMASHER"
processor_job.ram_amount = 4096
processor_job.save()
pjda = ProcessorJobDatasetAssociation()
"FNAME": first_name,
"LNAME": last_name,
"MMERGE3": organisation,
"MMERGE4": job_title,
},
}
client = MailChimp(
mc_user=get_env_setting("MAILCHIMP_USER"),
mc_api=get_env_setting("MAILCHIMP_API_KEY"),
)
try:
client.lists.members.get(
list_id=settings.MAILCHIMP_LIST_ID, subscriber_hash=email_hash
)
return True
except MailChimpError:
try:
client.lists.members.create(list_id=settings.MAILCHIMP_LIST_ID, data=data)
return True
except MailChimpError:
# things like blacklisted emails, etc
logger.warn("Unable to subscribe %s to newsletter", email)
return False